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/handy_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")
    1316
    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()
    2317
    2418offer_shop = "Handy"  # offer shop
    2519last_updated = datetime.now().date()
    2620is_validated = False
     21
     22# Handy phone offers that are already in database
     23offers = json.loads(unicodedata.normalize('NFKD', requests.get('http://localhost:8080/phoneoffer/shop/handy').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 = []
    2739
    2840handy_url = 'https://www.handy.mk/telefoni?page=6'
     
    4456    soup2 = BeautifulSoup(response2.text, 'html.parser')
    4557
     58    back_camera = None
     59    operating_system = None
     60    chipset = None
     61    battery = None
     62    ram_memory = None
     63    rom_memory = None
     64    cpu = None
     65    front_camera = None
     66    offer_shop_code = None
     67    color = None
     68    image_url = None
     69
    4670    color_section = soup2.find('section', {'data-hook': 'product-colors-title-section'})
    47 
    48     color = None
    4971    if color_section is not None:
    50         temp_colors = color_section.find('fieldset', {'class': 'ColorPickerbase3563640754__container'})\
     72        temp_colors = color_section.find('fieldset', {'class': 'ColorPickerbase3548966286__container'})\
    5173            .find_all('input', {'type': 'radio'})
    5274        colors_list = []
     
    6789    offer_description = '\n'.join(specifications)
    6890
    69     insert_script = 'INSERT INTO phone_offers (offer_shop, brand, offer_name , price, offer_url, ' \
    70                     'offer_description, last_updated, is_validated)' \
    71                             ' VALUES (%s, %s, %s, %s, %s, %s, %s, %s);'
    72     insert_value = (offer_shop, brand, offer_name, price, offer_url, offer_description,
    73                             last_updated, is_validated)
    74     cur.execute(insert_script, insert_value)
    75     db_connection.commit()
     91    new_offers.append(PhoneOffer(offer_shop, offer_name, price, ram_memory, rom_memory,
     92                                 color, front_camera, back_camera, chipset, battery, operating_system, cpu,
     93                                 image_url,
     94                                 offer_url, last_updated, is_validated, offer_description, offer_shop_code))
    7695
    77 cur.close()
    78 db_connection.close()
     96for new_offer in new_offers:
     97    flag = False
     98    flag_price = False
     99    offer_id = None
     100
     101    for old_offer in database_offers:
     102
     103        if new_offer.offer_name == old_offer.offer_name:
     104            flag = True
     105            if new_offer.price != old_offer.price:
     106                flag_price = True
     107                offer_id = old_offer.offer_id
     108
     109    if flag:
     110        # print('ALREADY IN DATABASE')
     111        # print(new_offer)
     112        # if it's already in database, check PRICE and if it's changed, change it !!!!!!
     113        if flag_price:
     114            print('PRICE CHANGED!')  # CHANGE PRICE
     115            print('offer id: ' + str(offer_id))
     116            headers = {'Content-type': 'application/json'}
     117            requests.put('http://localhost:8080/phoneoffer/' + str(offer_id) + '/changeprice/' + str(new_offer.price),
     118                         headers=headers)
     119    else:
     120        print('ADDED')  # ADD OFFER
     121        print(new_offer)
     122        headers = {'Content-type': 'application/json'}
     123        requests.post('http://localhost:8080/phoneoffer/addoffer',
     124                      headers=headers, data=json.dumps(new_offer.__dict__, default=str))
     125
     126print('------------------------------------')
     127
     128for old_offer in database_offers:
     129    flag = False
     130    for new_offer in new_offers:
     131        if old_offer.offer_name == new_offer.offer_name:
     132            flag = True
     133
     134    if not flag:
     135        print('OFFER DELETED')
     136        print(old_offer)
     137        # DELETE OFFER
     138        requests.delete('http://localhost:8080/phoneoffer/deleteoffer/' + str(old_offer.offer_id))
     139
     140
Note: See TracChangeset for help on using the changeset viewer.