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

    r48f3030 r895cd87  
     1import json
    12import unicodedata
    23from datetime import datetime
    3 
    44import psycopg2
    55import config_read
     
    1010import sys
    1111
     12from classes.phoneoffer import PhoneOffer
     13
    1214file_path = 'outputfile.txt'
    1315sys.stdout = open(file_path, "w")
    14 
    15 # Call to read the configuration file and connect to database
    16 cinfo = config_read.get_databaseconfig("../postgresdb.config")
    17 db_connection = psycopg2.connect(
    18     database=cinfo[0],
    19     host=cinfo[1],
    20     user=cinfo[2],
    21     password=cinfo[3]
    22 )
    23 cur = db_connection.cursor()
    2416
    2517offer_shop = "Ledikom"  # offer shop
    2618last_updated = datetime.now().date()
    2719is_validated = False
     20
     21# Ledikom phone offers that are already in database
     22
     23offers = json.loads(unicodedata.normalize('NFKD', requests.get('http://localhost:8080/phoneoffer/shop/ledikom').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 = []
    2839
    2940ledikom_phone_urls = [
     
    6576        offer_name = ' '.join(temp_offer_name.split())
    6677        brand = offer_name.split(' ')[0]
    67         price = int(phone.find('span', {'class': 'price'}).get_text().replace('ден.', '').replace('.', '').strip())
     78        price = int(phone.find('span', {'class': 'price'}).get_text().replace('ден.', '')
     79                    .replace('ден', '')
     80                    .replace('.', '').strip())
    6881
    6982        driver1 = webdriver.Safari(executable_path='/usr/bin/safaridriver')
     
    8295        rom_memory = None
    8396        ram_memory = None
     97        back_camera = None
     98        operating_system = None
     99        chipset = None
     100        battery = None
     101        cpu = None
     102        front_camera = None
     103        offer_shop_code = None
     104        offer_description = None
    84105
    85106        if len(specifications) != 0:
     
    114135            color = temp
    115136
    116         insert_script = 'INSERT INTO phone_offers (offer_shop, brand, offer_name, price, image_url, offer_url,' \
    117                         'ram_memory, rom_memory, color, last_updated, is_validated)' \
    118                         ' VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);'
    119         insert_value = (offer_shop, brand, offer_name, price, image_url, offer_url, ram_memory,
    120                         rom_memory, color, last_updated, is_validated)
    121         cur.execute(insert_script, insert_value)
    122         db_connection.commit()
     137        new_offers.append(PhoneOffer(offer_shop, offer_name, price, ram_memory, rom_memory,
     138                                     color, front_camera, back_camera, chipset, battery, operating_system, cpu,
     139                                     image_url,
     140                                     offer_url, last_updated, is_validated, offer_description, offer_shop_code))
    123141
    124 cur.close()
    125 db_connection.close()
     142for 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
     172print('------------------------------------')
     173
     174for 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.