Changeset 895cd87 for phonelux_scrappers/lowest_price_setter.py
- Timestamp:
- 10/01/22 22:55:27 (2 years ago)
- Branches:
- master
- Children:
- fd5b100
- Parents:
- 48f3030
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
phonelux_scrappers/lowest_price_setter.py
r48f3030 r895cd87 1 1 import json 2 2 import psycopg2 3 import requests 3 4 import config_read 4 5 5 import sys 6 6 … … 8 8 sys.stdout = open(file_path, "w") 9 9 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() 10 phones = json.loads(requests.get('http://localhost:8080/phones').text) 23 11 24 12 for 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']) 30 15 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) 35 22 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.