Changeset 895cd87 for phonelux_scrappers/scrappers/mobilezone_scrapper.py
- Timestamp:
- 10/01/22 22:55:27 (2 years ago)
- Branches:
- master
- Children:
- fd5b100
- Parents:
- 48f3030
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
phonelux_scrappers/scrappers/mobilezone_scrapper.py
r48f3030 r895cd87 1 import json 1 2 import unicodedata 2 3 from datetime import datetime … … 6 7 from selenium import webdriver 7 8 import requests 9 import sys 8 10 9 import sys 11 from classes.phoneoffer import PhoneOffer 10 12 11 13 file_path = 'outputfile.txt' 12 14 sys.stdout = open(file_path, "w") 13 15 14 # Call to read the configuration file and connect to database15 cinfo = config_read.get_databaseconfig("../postgresdb.config")16 db_connection = psycopg2.connect(17 database=cinfo[0],18 host=cinfo[1],19 user=cinfo[2],20 password=cinfo[3]21 )22 cur = db_connection.cursor()23 24 16 offer_shop = "Mobile Zone" # offer shop 25 17 last_updated = datetime.now().date() 26 18 is_validated = False 19 20 # Mobile Zone phone offers that are already in database 21 22 offers = json.loads(unicodedata.normalize('NFKD', requests.get('http://localhost:8080/phoneoffer/shop/mobilezone').text)) 23 24 database_offers = [] 25 26 for offer in offers: 27 phoneOffer = PhoneOffer(offer['id'], offer['offer_shop'], offer['offer_name'], offer['price'], 28 offer['ram_memory'], 29 offer['rom_memory'], offer['color'], offer['front_camera'], offer['back_camera'], 30 offer['chipset'], offer['battery'], offer['operating_system'], offer['cpu'], 31 offer['image_url'], 32 offer['offer_url'], offer['last_updated'], offer['is_validated'], 33 offer['offer_description'], 34 offer['offer_shop_code']) 35 database_offers.append(phoneOffer) 36 37 new_offers = [] 27 38 28 39 for i in range(1, 3): … … 54 65 offer_name = brand + ' ' + offer_name 55 66 56 price = int(unicodedata.normalize('NFKD', phone.find('span', {'class': 'woocommerce-Price-amount amount'}) 57 .find('bdi').get_text().replace(',', '').replace('ден', '').strip())) 67 price_tag = phone.find('span', {'class': 'woocommerce-Price-amount amount'}) 68 price = None 69 70 if price_tag is not None: 71 price = int(unicodedata.normalize('NFKD', price_tag.find('bdi').get_text() 72 .replace(',', '') 73 .replace('ден', '').strip())) 74 else: 75 continue 58 76 59 77 response2 = requests.get(offer_url) … … 65 83 front_camera = None 66 84 rom_memory = None 85 ram_memory = None 86 operating_system = None 87 cpu = None 88 chipset = None 89 offer_description = None 90 offer_shop_code = None 67 91 battery = None 68 92 color = None … … 84 108 color = specification.find('td').get_text().strip() 85 109 110 new_offers.append(PhoneOffer(offer_shop, offer_name, price, ram_memory, rom_memory, 111 color, front_camera, back_camera, chipset, battery, operating_system, cpu, 112 image_url, 113 offer_url, last_updated, is_validated, offer_description, offer_shop_code)) 86 114 115 for new_offer in new_offers: 116 flag = False 117 flag_price = False 118 offer_id = None 87 119 88 insert_script = 'INSERT INTO phone_offers (offer_shop, brand, offer_name , price, offer_url, image_url, ' \ 89 'rom_memory, battery, color, front_camera, back_camera, last_updated, is_validated)' \ 90 ' VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);' 91 insert_value = (offer_shop, brand, offer_name, price, offer_url, image_url, rom_memory, battery, color, 92 front_camera, back_camera, last_updated, is_validated) 93 cur.execute(insert_script, insert_value) 94 db_connection.commit() 120 for old_offer in database_offers: 95 121 96 cur.close() 97 db_connection.close() 122 if new_offer.offer_name == old_offer.offer_name: 123 flag = True 124 if new_offer.price != old_offer.price: 125 flag_price = True 126 offer_id = old_offer.offer_id 127 128 if flag: 129 # print('ALREADY IN DATABASE') 130 # print(new_offer) 131 # if it's already in database, check PRICE and if it's changed, change it !!!!!! 132 if flag_price: 133 print('PRICE CHANGED!') # CHANGE PRICE 134 print('offer id: ' + str(offer_id)) 135 headers = {'Content-type': 'application/json'} 136 requests.put('http://localhost:8080/phoneoffer/' + str(offer_id) + '/changeprice/' + str(new_offer.price), 137 headers=headers) 138 else: 139 print('ADDED') # ADD OFFER 140 print(new_offer) 141 headers = {'Content-type': 'application/json'} 142 requests.post('http://localhost:8080/phoneoffer/addoffer', 143 headers=headers, data=json.dumps(new_offer.__dict__, default=str)) 144 145 print('------------------------------------') 146 147 for old_offer in database_offers: 148 flag = False 149 for new_offer in new_offers: 150 if old_offer.offer_name == new_offer.offer_name: 151 flag = True 152 153 if not flag: 154 print('OFFER DELETED') 155 print(old_offer) 156 # DELETE OFFER 157 requests.delete('http://localhost:8080/phoneoffer/deleteoffer/' + str(old_offer.offer_id))
Note:
See TracChangeset
for help on using the changeset viewer.