[47f4eaf] | 1 | import traceback
|
---|
[b68ae8d] | 2 | import unicodedata
|
---|
| 3 | from datetime import datetime
|
---|
[895cd87] | 4 | import json
|
---|
[b68ae8d] | 5 | import psycopg2
|
---|
| 6 | import config_read
|
---|
| 7 | from bs4 import BeautifulSoup
|
---|
| 8 | import requests
|
---|
| 9 | import sys
|
---|
[895cd87] | 10 | import unicodedata
|
---|
| 11 |
|
---|
| 12 | from classes.phoneoffer import PhoneOffer
|
---|
[b68ae8d] | 13 |
|
---|
| 14 | file_path = 'outputfile.txt'
|
---|
| 15 | sys.stdout = open(file_path, "w")
|
---|
| 16 |
|
---|
| 17 | offer_shop = "A1" # offer shop
|
---|
| 18 | last_updated = datetime.now().date()
|
---|
| 19 | is_validated = False
|
---|
| 20 |
|
---|
[47f4eaf] | 21 | # Call to read the configuration file and connect to database
|
---|
| 22 | cinfo = config_read.get_databaseconfig("../postgresdb.config")
|
---|
| 23 | db_connection = psycopg2.connect(
|
---|
| 24 | database=cinfo[0],
|
---|
| 25 | host=cinfo[1],
|
---|
| 26 | user=cinfo[2],
|
---|
| 27 | password=cinfo[3]
|
---|
| 28 | )
|
---|
| 29 | cur = db_connection.cursor()
|
---|
[895cd87] | 30 |
|
---|
[47f4eaf] | 31 | try:
|
---|
| 32 | # A1 phone offers that are already in database
|
---|
| 33 | offers = json.loads(unicodedata.normalize('NFKD', requests.get('http://localhost:8080/phoneoffer/shop/a1').text))
|
---|
[895cd87] | 34 |
|
---|
[47f4eaf] | 35 | database_offers = []
|
---|
[895cd87] | 36 |
|
---|
[47f4eaf] | 37 | for offer in offers:
|
---|
| 38 | phoneOffer = PhoneOffer(offer['id'], offer['offer_shop'], offer['offer_name'], offer['price'],
|
---|
| 39 | offer['ram_memory'],
|
---|
| 40 | offer['rom_memory'], offer['color'], offer['front_camera'], offer['back_camera'],
|
---|
| 41 | offer['chipset'], offer['battery'], offer['operating_system'], offer['cpu'],
|
---|
| 42 | offer['image_url'],
|
---|
| 43 | offer['offer_url'], offer['last_updated'], offer['is_validated'],
|
---|
| 44 | offer['offer_description'],
|
---|
| 45 | offer['offer_shop_code'])
|
---|
| 46 | database_offers.append(phoneOffer)
|
---|
[895cd87] | 47 |
|
---|
[47f4eaf] | 48 | a1_url = 'https://www.a1.mk/webshop/mk/phones'
|
---|
[b68ae8d] | 49 |
|
---|
[47f4eaf] | 50 | response1 = requests.get(a1_url)
|
---|
| 51 | soup1 = BeautifulSoup(response1.content, 'html.parser')
|
---|
[b68ae8d] | 52 |
|
---|
[47f4eaf] | 53 | phones = soup1.find('main', {'class', 'gsm-advisor-grid phones'}).find('div', {'class', 'd-flex'}) \
|
---|
| 54 | .find_all('div', {'class', 'dvc-idtfr by4'})
|
---|
[b68ae8d] | 55 |
|
---|
[47f4eaf] | 56 | new_offers = []
|
---|
[895cd87] | 57 |
|
---|
[47f4eaf] | 58 | for phone in phones:
|
---|
| 59 | brand = phone.get('data-brand').strip()
|
---|
| 60 | offer_name = brand + " " + phone.get('data-model').strip()
|
---|
[b68ae8d] | 61 |
|
---|
[47f4eaf] | 62 | # if brand not in offer_name:
|
---|
| 63 | # offer_name = brand+" "+offer_name
|
---|
[b68ae8d] | 64 |
|
---|
[47f4eaf] | 65 | offer_shop_code = phone.get('data-productid').strip()
|
---|
| 66 | offer_url = phone.find('a', {'class', 'device-link'}).get('href')
|
---|
| 67 | image_url = phone.get('data-image')
|
---|
[b68ae8d] | 68 |
|
---|
[47f4eaf] | 69 | response2 = requests.get(offer_url)
|
---|
| 70 | soup2 = BeautifulSoup(response2.content, 'html.parser')
|
---|
[b68ae8d] | 71 |
|
---|
[47f4eaf] | 72 | temp_prices = soup2.find('div', {'class': 'ured-tabs-content'}) \
|
---|
| 73 | .find('div', {'class': 'cenovnik-secondary d-flex justify-content-between'}).find_all('div')
|
---|
[b68ae8d] | 74 |
|
---|
[47f4eaf] | 75 | # offer price
|
---|
| 76 | price = None
|
---|
| 77 | for temp_price in temp_prices:
|
---|
| 78 | if 'Цена само за уред' in temp_price.get_text().strip():
|
---|
| 79 | price = int(temp_price.get_text().replace('Цена само за уред', '')
|
---|
| 80 | .replace('Одбери', '').replace('денари', '').replace('.', '').strip())
|
---|
[b68ae8d] | 81 |
|
---|
[47f4eaf] | 82 | colors_section = soup2.find('div', {'id': 'hero'}).find('div', {'class': 'widget'}).find_all('label')
|
---|
[b68ae8d] | 83 |
|
---|
[47f4eaf] | 84 | temp_colors = []
|
---|
| 85 | for color_section in colors_section:
|
---|
| 86 | temp_colors.append(color_section.get('data-content'))
|
---|
[b68ae8d] | 87 |
|
---|
[47f4eaf] | 88 | color = ','.join(temp_colors) # colors available for the offer
|
---|
[b68ae8d] | 89 |
|
---|
[47f4eaf] | 90 | phone_description = soup2.find('div', {'class': 'desc section'}).find('p').get_text().strip()
|
---|
[b68ae8d] | 91 |
|
---|
[47f4eaf] | 92 | table_rows = soup2.find('table', {'class': 'table karakteristiki'}).find_all('tr')
|
---|
[b68ae8d] | 93 |
|
---|
[47f4eaf] | 94 | back_camera = None
|
---|
| 95 | operating_system = None
|
---|
| 96 | cpu = None
|
---|
| 97 | rom_memory = None
|
---|
| 98 | ram_memory = None
|
---|
| 99 | battery = None
|
---|
| 100 | front_camera = None
|
---|
| 101 | chipset = None
|
---|
| 102 | offer_description = None
|
---|
[b68ae8d] | 103 |
|
---|
[47f4eaf] | 104 | for row in table_rows:
|
---|
| 105 | if 'Камера' in row.get_text().strip():
|
---|
| 106 | back_camera = row.get_text().replace('Камера', '').strip()
|
---|
[b68ae8d] | 107 |
|
---|
[47f4eaf] | 108 | if 'Оперативен систем' in row.get_text().strip():
|
---|
| 109 | operating_system = row.get_text().replace('Оперативен систем', '').strip()
|
---|
[b68ae8d] | 110 |
|
---|
[47f4eaf] | 111 | if 'CPU' in row.get_text().strip():
|
---|
| 112 | cpu = row.get_text().replace('CPU', '').strip()
|
---|
[b68ae8d] | 113 |
|
---|
[47f4eaf] | 114 | if 'Вградена меморија' in row.get_text().strip():
|
---|
| 115 | rom_memory = row.get_text().replace('Вградена меморија', '').strip()
|
---|
[b68ae8d] | 116 |
|
---|
[47f4eaf] | 117 | if 'RAM меморија' in row.get_text().strip():
|
---|
| 118 | ram_memory = row.get_text().replace('RAM меморија', '').strip()
|
---|
[b68ae8d] | 119 |
|
---|
[47f4eaf] | 120 | if 'Батерија' in row.get_text().strip():
|
---|
| 121 | battery = row.get_text().replace('Батерија', '').strip()
|
---|
[b68ae8d] | 122 |
|
---|
[47f4eaf] | 123 | if 'Предна камера' in row.get_text().strip():
|
---|
| 124 | front_camera = row.get_text().replace('Предна камера', '').strip()
|
---|
[b68ae8d] | 125 |
|
---|
[47f4eaf] | 126 | new_offers.append(PhoneOffer(offer_shop, offer_name, price, ram_memory, rom_memory,
|
---|
| 127 | color, front_camera, back_camera, chipset, battery, operating_system, cpu,
|
---|
| 128 | image_url,
|
---|
| 129 | offer_url, last_updated, is_validated, offer_description, offer_shop_code))
|
---|
[895cd87] | 130 |
|
---|
[47f4eaf] | 131 | for new_offer in new_offers:
|
---|
| 132 | flag = False
|
---|
| 133 | flag_price = False
|
---|
| 134 | offer_id = None
|
---|
| 135 |
|
---|
| 136 | for old_offer in database_offers:
|
---|
| 137 |
|
---|
| 138 | if new_offer.offer_shop_code == old_offer.offer_shop_code:
|
---|
| 139 | flag = True
|
---|
| 140 | if new_offer.price != old_offer.price:
|
---|
| 141 | flag_price = True
|
---|
| 142 | offer_id = old_offer.offer_id
|
---|
| 143 |
|
---|
| 144 | if flag:
|
---|
| 145 | # print('ALREADY IN DATABASE')
|
---|
| 146 | # print(new_offer)
|
---|
| 147 | # if it's already in database, check PRICE and if it's changed, change it !!!!!!
|
---|
| 148 | if flag_price:
|
---|
| 149 | print('PRICE CHANGED!') # CHANGE PRICE
|
---|
| 150 | print('offer id: ' + str(offer_id))
|
---|
| 151 | headers = {'Content-type': 'application/json'}
|
---|
| 152 | requests.put(
|
---|
| 153 | 'http://localhost:8080/phoneoffer/' + str(offer_id) + '/changeprice/' + str(new_offer.price),
|
---|
| 154 | headers=headers)
|
---|
| 155 | else:
|
---|
| 156 | print('ADDED') # ADD OFFER
|
---|
| 157 | print(new_offer)
|
---|
| 158 | headers = {'Content-type': 'application/json'}
|
---|
| 159 | requests.post('http://localhost:8080/phoneoffer/addoffer', headers=headers,
|
---|
| 160 | data=json.dumps(new_offer.__dict__,
|
---|
| 161 | default=str))
|
---|
[895cd87] | 162 |
|
---|
[47f4eaf] | 163 | print('------------------------------------')
|
---|
[895cd87] | 164 |
|
---|
[47f4eaf] | 165 | for old_offer in database_offers:
|
---|
| 166 | flag = False
|
---|
| 167 | for new_offer in new_offers:
|
---|
| 168 | if old_offer.offer_shop_code == new_offer.offer_shop_code:
|
---|
| 169 | flag = True
|
---|
| 170 |
|
---|
| 171 | if not flag:
|
---|
| 172 | print('OFFER DELETED')
|
---|
| 173 | print(old_offer)
|
---|
| 174 | # DELETE OFFER
|
---|
| 175 | requests.delete('http://localhost:8080/phoneoffer/deleteoffer/' + str(old_offer.offer_id))
|
---|
| 176 | except Exception:
|
---|
| 177 | traceback.print_exc()
|
---|
| 178 | insert_script = 'INSERT INTO scrapper_info (store, recieved_at, status)' \
|
---|
| 179 | ' VALUES (%s, %s, %s);'
|
---|
| 180 | insert_value = (offer_shop, last_updated, 'failed')
|
---|
| 181 | cur.execute(insert_script, insert_value)
|
---|
| 182 | db_connection.commit()
|
---|
| 183 | cur.close()
|
---|
| 184 | db_connection.close()
|
---|
| 185 | else:
|
---|
| 186 | insert_script = 'INSERT INTO scrapper_info (store, recieved_at, status)' \
|
---|
| 187 | ' VALUES (%s, %s, %s);'
|
---|
| 188 | insert_value = (offer_shop, last_updated, 'success')
|
---|
| 189 | cur.execute(insert_script, insert_value)
|
---|
| 190 | db_connection.commit()
|
---|
| 191 | cur.close()
|
---|
| 192 | db_connection.close()
|
---|