Changeset 895cd87 for phonelux_scrappers/scrappers/mobigo_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/mobigo_scrapper.py
r48f3030 r895cd87 1 import json 2 import unicodedata 1 3 from datetime import datetime 2 4 … … 5 7 from bs4 import BeautifulSoup 6 8 import requests 9 import sys 7 10 8 # import sys 9 # 10 # file_path = 'outputfile.txt' 11 # sys.stdout = open(file_path, "w") 11 from classes.phoneoffer import PhoneOffer 12 12 13 # Call to read the configuration file and connect to database 14 cinfo = config_read.get_databaseconfig("../postgresdb.config") 15 db_connection = psycopg2.connect( 16 database=cinfo[0], 17 host=cinfo[1], 18 user=cinfo[2], 19 password=cinfo[3] 20 ) 21 cur = db_connection.cursor() 13 file_path = 'outputfile.txt' 14 sys.stdout = open(file_path, "w") 22 15 23 16 offer_shop = "Mobi Go" # offer shop 24 17 last_updated = datetime.now().date() 25 18 is_validated = False 19 20 # Mobi Go phone offers that are already in database 21 22 offers = json.loads(unicodedata.normalize('NFKD', requests.get('http://localhost:8080/phoneoffer/shop/mobigo').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 = [] 38 39 26 40 for i in range(1, 6): 27 41 mobigo_url = "https://mobigo.mk/page/" + str(i) + "/" … … 55 69 specifications = soup2.find('table', {'id': 'singlet'}).find_all('tr') 56 70 57 ram_memory = "" 58 rom_memory = "" 59 battery = "" 60 back_camera = "" 61 front_camera = "" 62 chipset = "" 63 operating_system = "" 71 ram_memory = None 72 rom_memory = None 73 battery = None 74 back_camera = None 75 front_camera = None 76 chipset = None 77 operating_system = None 78 cpu = None 79 offer_shop_code = None 80 offer_description = None 81 color = None 64 82 65 83 for specification in specifications: … … 111 129 battery = None 112 130 113 insert_script = 'INSERT INTO phone_offers (offer_shop, brand, offer_name, price, image_url, offer_url, ram_memory,' \ 114 ' rom_memory, battery, back_camera, front_camera, chipset, operating_system, last_updated, is_validated)' \ 115 ' VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);' 116 insert_value = (offer_shop, brand, offer_name, price, image_url, offer_url, ram_memory, 117 rom_memory, battery, back_camera, front_camera, chipset, operating_system, last_updated, is_validated) 118 cur.execute(insert_script, insert_value) 119 db_connection.commit() 131 new_offers.append(PhoneOffer(offer_shop, offer_name, price, ram_memory, rom_memory, 132 color, front_camera, back_camera, chipset, battery, operating_system, cpu, 133 image_url, 134 offer_url, last_updated, is_validated, offer_description, offer_shop_code)) 120 135 121 cur.close() 122 db_connection.close() 136 137 for new_offer in new_offers: 138 flag = False 139 flag_price = False 140 offer_id = None 141 142 for old_offer in database_offers: 143 144 if new_offer.offer_name == old_offer.offer_name: 145 flag = True 146 if new_offer.price != old_offer.price: 147 flag_price = True 148 offer_id = old_offer.offer_id 149 150 if flag: 151 print('ALREADY IN DATABASE') 152 print(new_offer) 153 # if it's already in database, check PRICE and if it's changed, change it !!!!!! 154 if flag_price: 155 print('PRICE CHANGED!') # CHANGE PRICE 156 print('offer id: ' + str(offer_id)) 157 headers = {'Content-type': 'application/json'} 158 requests.put('http://localhost:8080/phoneoffer/' + str(offer_id) + '/changeprice/' + str(new_offer.price), 159 headers=headers) 160 else: 161 print('ADDED') # ADD OFFER 162 print(new_offer) 163 headers = {'Content-type': 'application/json'} 164 requests.post('http://localhost:8080/phoneoffer/addoffer', 165 headers=headers, data=json.dumps(new_offer.__dict__, default=str)) 166 167 print('------------------------------------') 168 169 for old_offer in database_offers: 170 flag = False 171 for new_offer in new_offers: 172 if old_offer.offer_name == new_offer.offer_name: 173 flag = True 174 175 if not flag: 176 print('OFFER DELETED') 177 print(old_offer) 178 # DELETE OFFER 179 requests.delete('http://localhost:8080/phoneoffer/deleteoffer/' + str(old_offer.offer_id))
Note:
See TracChangeset
for help on using the changeset viewer.