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

Refactored code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • phonelux_scrappers/lowest_price_setter.py

    r48f3030 r895cd87  
    1 
     1import json
    22import psycopg2
     3import requests
    34import config_read
    4 
    55import sys
    66
     
    88sys.stdout = open(file_path, "w")
    99
    10 # Call to read the configuration file and connect to database
    11 cinfo = config_read.get_databaseconfig("postgresdb.config")
    12 db_connection = psycopg2.connect(
    13     database=cinfo[0],
    14     host=cinfo[1],
    15     user=cinfo[2],
    16     password=cinfo[3]
    17 )
    18 cur = db_connection.cursor()
    19 
    20 cur.execute('SELECT id,model FROM phones ORDER BY id;')
    21 
    22 phones = cur.fetchall()
     10phones = json.loads(requests.get('http://localhost:8080/phones').text)
    2311
    2412for phone in phones:
    25     phone_id = str(phone[0])
    26     print('id: '+phone_id)
    27     cur.execute('SELECT offer_name,price FROM phone_offers WHERE phone_id=' + phone_id + ' ORDER BY price;')
    28     details = cur.fetchone()
    29     lowestPrice = None
     13    phone_id = str(phone['id'])
     14    print(phone['model'])
    3015
    31     if details is not None:
    32         lowestPrice = details[1]
    33         cur.execute('UPDATE phones SET lowest_price='+str(lowestPrice)+' WHERE id='+phone_id+';')
    34         db_connection.commit()
     16    offers = list(json.loads(requests.get('http://localhost:8080/phones/offers/' + phone_id).text))
     17    offers.sort(key=lambda o: o['price'])
     18    lowest_price = str(0)
     19    if len(offers) > 0:
     20        lowest_price = str(offers[0]['price'])
     21    print(lowest_price)
    3522
    36 cur.close()
    37 db_connection.close()
     23    # UPDATE DATABASE WITH NEW LOWEST PRICE
     24    requests.put('http://localhost:8080/setlowestprice/' + phone_id + '/' + lowest_price)
Note: See TracChangeset for help on using the changeset viewer.