Changeset 895cd87 for phonelux_scrappers/scrappers/mobelix_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/mobelix_scrapper.py
r48f3030 r895cd87 1 import json 2 import sys 1 3 import unicodedata 2 4 from datetime import datetime … … 8 10 9 11 # import sys 10 # 11 # file_path = 'outputfile.txt' 12 # sys.stdout = open(file_path, "w") 12 from classes.phoneoffer import PhoneOffer 13 13 14 # Call to read the configuration file and connect to database 15 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() 14 file_path = 'outputfile.txt' 15 sys.stdout = open(file_path, "w") 23 16 24 17 offer_shop = "Mobelix" # offer shop 25 18 last_updated = datetime.now().date() 26 19 is_validated = False 20 21 # Mobelix phone offers that are already in database 22 23 offers = json.loads(unicodedata.normalize('NFKD', requests.get('http://localhost:8080/phoneoffer/shop/mobelix').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 = [] 27 39 28 40 for i in range(1, 17): … … 77 89 back_camera = '' 78 90 cpu = None 91 offer_shop_code = None 92 offer_description = None 79 93 80 94 for table in tables: … … 120 134 back_camera = None 121 135 122 insert_script = 'INSERT INTO phone_offers (offer_shop, brand, offer_name, price, image_url, offer_url,' \ 123 'ram_memory, rom_memory, battery, back_camera, front_camera, color, cpu, chipset, ' \ 124 'operating_system, last_updated, is_validated)' \ 125 ' VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);' 126 insert_value = (offer_shop, brand, offer_name, price, image_url, offer_url, ram_memory, rom_memory, 127 battery, back_camera, front_camera, color, cpu, chipset, operating_system, 128 last_updated, is_validated) 129 cur.execute(insert_script, insert_value) 130 db_connection.commit() 136 new_offers.append(PhoneOffer(offer_shop, offer_name, price, ram_memory, rom_memory, 137 color, front_camera, back_camera, chipset, battery, operating_system, cpu, 138 image_url, 139 offer_url, last_updated, is_validated, offer_description, offer_shop_code)) 131 140 132 cur.close() 133 db_connection.close() 141 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.