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

    r48f3030 r895cd87  
     1import json
    12import unicodedata
    23from datetime import datetime
     
    67from selenium import webdriver
    78import requests
     9import sys
    810
    9 import sys
     11from classes.phoneoffer import PhoneOffer
    1012
    1113file_path = 'outputfile.txt'
    1214sys.stdout = open(file_path, "w")
    1315
    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()
    23 
    2416offer_shop = "Mobile Zone"  # offer shop
    2517last_updated = datetime.now().date()
    2618is_validated = False
     19
     20# Mobile Zone phone offers that are already in database
     21
     22offers = json.loads(unicodedata.normalize('NFKD', requests.get('http://localhost:8080/phoneoffer/shop/mobilezone').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 = []
    2738
    2839for i in range(1, 3):
     
    5465            offer_name = brand + ' ' + offer_name
    5566
    56         price = int(unicodedata.normalize('NFKD', phone.find('span', {'class': 'woocommerce-Price-amount amount'})
    57                                           .find('bdi').get_text().replace(',', '').replace('ден', '').strip()))
     67        price_tag = phone.find('span', {'class': 'woocommerce-Price-amount amount'})
     68        price = None
     69
     70        if price_tag is not None:
     71            price = int(unicodedata.normalize('NFKD', price_tag.find('bdi').get_text()
     72                                          .replace(',', '')
     73                                          .replace('ден', '').strip()))
     74        else:
     75            continue
    5876
    5977        response2 = requests.get(offer_url)
     
    6583        front_camera = None
    6684        rom_memory = None
     85        ram_memory = None
     86        operating_system = None
     87        cpu = None
     88        chipset = None
     89        offer_description = None
     90        offer_shop_code = None
    6791        battery = None
    6892        color = None
     
    84108                color = specification.find('td').get_text().strip()
    85109
     110        new_offers.append(PhoneOffer(offer_shop, offer_name, price, ram_memory, rom_memory,
     111                                     color, front_camera, back_camera, chipset, battery, operating_system, cpu,
     112                                     image_url,
     113                                     offer_url, last_updated, is_validated, offer_description, offer_shop_code))
    86114
     115for new_offer in new_offers:
     116    flag = False
     117    flag_price = False
     118    offer_id = None
    87119
    88         insert_script = 'INSERT INTO phone_offers (offer_shop, brand, offer_name , price, offer_url, image_url, ' \
    89                         'rom_memory, battery, color, front_camera, back_camera, last_updated, is_validated)' \
    90                                 ' VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);'
    91         insert_value = (offer_shop, brand, offer_name, price, offer_url, image_url, rom_memory, battery, color,
    92                         front_camera, back_camera, last_updated, is_validated)
    93         cur.execute(insert_script, insert_value)
    94         db_connection.commit()
     120    for old_offer in database_offers:
    95121
    96 cur.close()
    97 db_connection.close()
     122        if new_offer.offer_name == old_offer.offer_name:
     123            flag = True
     124            if new_offer.price != old_offer.price:
     125                flag_price = True
     126                offer_id = old_offer.offer_id
     127
     128    if flag:
     129        # print('ALREADY IN DATABASE')
     130        # print(new_offer)
     131        # if it's already in database, check PRICE and if it's changed, change it !!!!!!
     132        if flag_price:
     133            print('PRICE CHANGED!')  # CHANGE PRICE
     134            print('offer id: ' + str(offer_id))
     135            headers = {'Content-type': 'application/json'}
     136            requests.put('http://localhost:8080/phoneoffer/' + str(offer_id) + '/changeprice/' + str(new_offer.price),
     137                         headers=headers)
     138    else:
     139        print('ADDED')  # ADD OFFER
     140        print(new_offer)
     141        headers = {'Content-type': 'application/json'}
     142        requests.post('http://localhost:8080/phoneoffer/addoffer',
     143                      headers=headers, data=json.dumps(new_offer.__dict__, default=str))
     144
     145print('------------------------------------')
     146
     147for old_offer in database_offers:
     148    flag = False
     149    for new_offer in new_offers:
     150        if old_offer.offer_name == new_offer.offer_name:
     151            flag = True
     152
     153    if not flag:
     154        print('OFFER DELETED')
     155        print(old_offer)
     156        # DELETE OFFER
     157        requests.delete('http://localhost:8080/phoneoffer/deleteoffer/' + str(old_offer.offer_id))
Note: See TracChangeset for help on using the changeset viewer.