Changeset 895cd87 for phonelux_scrappers/scrappers/ledikom_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/ledikom_scrapper.py
r48f3030 r895cd87 1 import json 1 2 import unicodedata 2 3 from datetime import datetime 3 4 4 import psycopg2 5 5 import config_read … … 10 10 import sys 11 11 12 from classes.phoneoffer import PhoneOffer 13 12 14 file_path = 'outputfile.txt' 13 15 sys.stdout = open(file_path, "w") 14 15 # Call to read the configuration file and connect to database16 cinfo = config_read.get_databaseconfig("../postgresdb.config")17 db_connection = psycopg2.connect(18 database=cinfo[0],19 host=cinfo[1],20 user=cinfo[2],21 password=cinfo[3]22 )23 cur = db_connection.cursor()24 16 25 17 offer_shop = "Ledikom" # offer shop 26 18 last_updated = datetime.now().date() 27 19 is_validated = False 20 21 # Ledikom phone offers that are already in database 22 23 offers = json.loads(unicodedata.normalize('NFKD', requests.get('http://localhost:8080/phoneoffer/shop/ledikom').text)) 24 25 database_offers = [] 26 27 for offer in offers: 28 phoneOffer = PhoneOffer(offer['id'], offer['offer_shop'], offer['offer_name'], offer['price'], 29 offer['ram_memory'], 30 offer['rom_memory'], offer['color'], offer['front_camera'], offer['back_camera'], 31 offer['chipset'], offer['battery'], offer['operating_system'], offer['cpu'], 32 offer['image_url'], 33 offer['offer_url'], offer['last_updated'], offer['is_validated'], 34 offer['offer_description'], 35 offer['offer_shop_code']) 36 database_offers.append(phoneOffer) 37 38 new_offers = [] 28 39 29 40 ledikom_phone_urls = [ … … 65 76 offer_name = ' '.join(temp_offer_name.split()) 66 77 brand = offer_name.split(' ')[0] 67 price = int(phone.find('span', {'class': 'price'}).get_text().replace('ден.', '').replace('.', '').strip()) 78 price = int(phone.find('span', {'class': 'price'}).get_text().replace('ден.', '') 79 .replace('ден', '') 80 .replace('.', '').strip()) 68 81 69 82 driver1 = webdriver.Safari(executable_path='/usr/bin/safaridriver') … … 82 95 rom_memory = None 83 96 ram_memory = None 97 back_camera = None 98 operating_system = None 99 chipset = None 100 battery = None 101 cpu = None 102 front_camera = None 103 offer_shop_code = None 104 offer_description = None 84 105 85 106 if len(specifications) != 0: … … 114 135 color = temp 115 136 116 insert_script = 'INSERT INTO phone_offers (offer_shop, brand, offer_name, price, image_url, offer_url,' \ 117 'ram_memory, rom_memory, color, last_updated, is_validated)' \ 118 ' VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);' 119 insert_value = (offer_shop, brand, offer_name, price, image_url, offer_url, ram_memory, 120 rom_memory, color, last_updated, is_validated) 121 cur.execute(insert_script, insert_value) 122 db_connection.commit() 137 new_offers.append(PhoneOffer(offer_shop, offer_name, price, ram_memory, rom_memory, 138 color, front_camera, back_camera, chipset, battery, operating_system, cpu, 139 image_url, 140 offer_url, last_updated, is_validated, offer_description, offer_shop_code)) 123 141 124 cur.close() 125 db_connection.close() 142 for new_offer in new_offers: 143 flag = False 144 flag_price = False 145 offer_id = None 146 147 for old_offer in database_offers: 148 149 if new_offer.offer_name == old_offer.offer_name: 150 flag = True 151 if new_offer.price != old_offer.price: 152 flag_price = True 153 offer_id = old_offer.offer_id 154 155 if flag: 156 # print('ALREADY IN DATABASE') 157 # print(new_offer) 158 # if it's already in database, check PRICE and if it's changed, change it !!!!!! 159 if flag_price: 160 print('PRICE CHANGED!') # CHANGE PRICE 161 print('offer id: ' + str(offer_id)) 162 headers = {'Content-type': 'application/json'} 163 requests.put('http://localhost:8080/phoneoffer/' + str(offer_id) + '/changeprice/' + str(new_offer.price), 164 headers=headers) 165 else: 166 print('ADDED') # ADD OFFER 167 print(new_offer) 168 headers = {'Content-type': 'application/json'} 169 requests.post('http://localhost:8080/phoneoffer/addoffer', 170 headers=headers, data=json.dumps(new_offer.__dict__, default=str)) 171 172 print('------------------------------------') 173 174 for old_offer in database_offers: 175 flag = False 176 for new_offer in new_offers: 177 if old_offer.offer_name == new_offer.offer_name: 178 flag = True 179 180 if not flag: 181 print('OFFER DELETED') 182 print(old_offer) 183 # DELETE OFFER 184 requests.delete('http://localhost:8080/phoneoffer/deleteoffer/' + str(old_offer.offer_id))
Note:
See TracChangeset
for help on using the changeset viewer.