Ignore:
Timestamp:
10/01/22 22:55:27 (21 months ago)
Author:
Marko <Marko@…>
Branches:
master
Children:
fd5b100
Parents:
48f3030
Message:

Refactored code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • phonelux_scrappers/scrappers/neptun_scrapper.py

    r48f3030 r895cd87  
     1import json
    12import unicodedata
    23from datetime import datetime
     
    910import sys
    1011
     12from classes.phoneoffer import PhoneOffer
     13
    1114file_path = 'outputfile.txt'
    1215sys.stdout = open(file_path, "w")
    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()
    2316
    2417offer_shop = "Neptun"  # offer shop
     
    2619is_validated = False
    2720
     21# Neptun phone offers that are already in database
     22
     23offers = json.loads(unicodedata.normalize('NFKD', requests.get('http://localhost:8080/phoneoffer/shop/neptun').text))
     24
     25database_offers = []
     26
     27for 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
     38new_offers = []
     39
    2840for i in range(1, 11):
    29     neptun_url = 'https://www.neptun.mk/mobilni_telefoni.nspx?page='+str(i)
     41    neptun_url = 'https://www.neptun.mk/mobilni_telefoni.nspx?page=' + str(i)
    3042
    3143    # selenium is used because of the dynamic content of the page
     
    7284        offer_description = specifications_table.get_text(separator='\n').strip()
    7385
     86        back_camera = None
    7487        operating_system = None
    7588        chipset = None
     
    7891        rom_memory = None
    7992        cpu = None
     93        front_camera = None
     94        color = None
     95
    8096        for specification in specifications:
    8197            if 'Батерија:' in specification:
     
    105121                operating_system = specification
    106122
    107         insert_script = 'INSERT INTO phone_offers (offer_shop, brand, offer_name , price, image_url, offer_url,' \
    108                         'offer_shop_code, operating_system, battery, chipset, cpu, ram_memory, rom_memory, ' \
    109                         'offer_description, last_updated, is_validated)' \
    110                         ' VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);'
    111         insert_value = (offer_shop, brand, offer_name, price, image_url, offer_url,
    112                         offer_shop_code, operating_system, battery, chipset, cpu, ram_memory, rom_memory, offer_description,
    113                         last_updated, is_validated)
    114         cur.execute(insert_script, insert_value)
    115         db_connection.commit()
     123        new_offers.append(PhoneOffer(offer_shop, offer_name, price, ram_memory, rom_memory,
     124                                     color, front_camera, back_camera, chipset, battery, operating_system, cpu,
     125                                     image_url,
     126                                     offer_url, last_updated, is_validated, offer_description, offer_shop_code))
    116127
    117 cur.close()
    118 db_connection.close()
     128for new_offer in new_offers:
     129    flag = False
     130    flag_price = False
     131    offer_id = None
     132
     133    for old_offer in database_offers:
     134
     135        if new_offer.offer_shop_code == old_offer.offer_shop_code:
     136            flag = True
     137            if new_offer.price != old_offer.price:
     138                flag_price = True
     139                offer_id = old_offer.offer_id
     140
     141    if flag:
     142        # print('ALREADY IN DATABASE')
     143        # print(new_offer)
     144        # if it's already in database, check PRICE and if it's changed, change it !!!!!!
     145        if flag_price:
     146            print('PRICE CHANGED!')  # CHANGE PRICE
     147            print('offer id: ' + str(offer_id))
     148            headers = {'Content-type': 'application/json'}
     149            requests.put('http://localhost:8080/phoneoffer/' + str(offer_id) + '/changeprice/' + str(new_offer.price),
     150                         headers=headers)
     151    else:
     152        print('ADDED')  # ADD OFFER
     153        print(new_offer)
     154        headers = {'Content-type': 'application/json'}
     155        requests.post('http://localhost:8080/phoneoffer/addoffer',
     156                      headers=headers, data=json.dumps(new_offer.__dict__, default=str))
     157
     158print('------------------------------------')
     159
     160for old_offer in database_offers:
     161    flag = False
     162    for new_offer in new_offers:
     163        if old_offer.offer_shop_code == new_offer.offer_shop_code:
     164            flag = True
     165
     166    if not flag:
     167        print('OFFER DELETED')
     168        print(old_offer)
     169        # DELETE OFFER
     170        requests.delete('http://localhost:8080/phoneoffer/deleteoffer/' + str(old_offer.offer_id))
Note: See TracChangeset for help on using the changeset viewer.