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/akcija_scrapper.py

    r48f3030 r895cd87  
     1import json
    12from datetime import datetime
    23
     
    78import unicodedata
    89import sys
     10from classes.phoneoffer import PhoneOffer
    911
    10 # file_path = '../outputfile.txt'
    11 # sys.stdout = open(file_path, "w")
     12file_path = 'outputfile.txt'
     13sys.stdout = open(file_path, "w")
    1214
    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()
    2215
    2316offer_shop = "Akcija"  # offer shop
    2417last_updated = datetime.now().date()
    2518is_validated = False
     19
     20# Akcija phone offers that are already in database
     21
     22offers = json.loads(unicodedata.normalize('NFKD', requests.get('http://localhost:8080/phoneoffer/shop/akcija').text))
     23
     24database_offers = []
     25
     26for 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
     37new_offers = []
    2638
    2739i = 0
     
    5163        soup2 = BeautifulSoup(response2.text, 'html.parser')
    5264
     65        back_camera = None
     66        operating_system = None
     67        chipset = None
     68        battery = None
     69        ram_memory = None
     70        rom_memory = None
     71        cpu = None
     72        front_camera = None
     73        color = None
     74        offer_shop_code = None
     75
    5376        specifications = soup2.find('main', {'id': 'content'}) \
    5477            .find_all('div', {'class', 'container'})[1].find('div', {'class', 'mb-14'}) \
     
    6285                                                       str(specification.get_text(separator='\n').strip())) + "\n"
    6386
    64         insert_script = 'INSERT INTO phone_offers (offer_shop, brand,' \
    65                         ' offer_name, price, image_url, offer_url, last_updated, is_validated, offer_description) ' \
    66                         'VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s);'
    67         insert_value = (offer_shop, brand, offer_name, price, image_url, offer_url,
    68                         last_updated, is_validated, offer_description)
    69         cur.execute(insert_script, insert_value)
    70         db_connection.commit()
     87        new_offers.append(PhoneOffer(offer_shop, offer_name, price, ram_memory, rom_memory,
     88                                     color, front_camera, back_camera, chipset, battery, operating_system, cpu,
     89                                     image_url,
     90                                     offer_url, last_updated, is_validated, offer_description, offer_shop_code))
    7191    i += 20
    7292
    73 cur.close()
    74 db_connection.close()
     93for new_offer in new_offers:
     94    flag = False
     95    flag_price = False
     96    offer_id = None
     97
     98    for old_offer in database_offers:
     99
     100        if new_offer.offer_name == old_offer.offer_name:
     101            flag = True
     102            if new_offer.price != old_offer.price:
     103                flag_price = True
     104                offer_id = old_offer.offer_id
     105
     106    if flag:
     107        # print('ALREADY IN DATABASE')
     108        # print(new_offer)
     109        # if it's already in database, check PRICE and if it's changed, change it !!!!!!
     110        if flag_price:
     111            print('PRICE CHANGED!')  # CHANGE PRICE
     112            print('offer id: ' + str(offer_id))
     113            headers = {'Content-type': 'application/json'}
     114            requests.put('http://localhost:8080/phoneoffer/' + str(offer_id) + '/changeprice/' + str(new_offer.price),
     115                         headers=headers)
     116    else:
     117        print('ADDED')  # ADD OFFER
     118        print(new_offer)
     119        headers = {'Content-type': 'application/json'}
     120        requests.post('http://localhost:8080/phoneoffer/addoffer',
     121                      headers=headers, data=json.dumps(new_offer.__dict__, default=str))
     122
     123print('------------------------------------')
     124
     125for old_offer in database_offers:
     126    flag = False
     127    for new_offer in new_offers:
     128        if old_offer.offer_name == new_offer.offer_name:
     129            flag = True
     130
     131    if not flag:
     132        print('OFFER DELETED')
     133        print(old_offer)
     134        # DELETE OFFER
     135        requests.delete('http://localhost:8080/phoneoffer/deleteoffer/' + str(old_offer.offer_id))
Note: See TracChangeset for help on using the changeset viewer.