Changeset 895cd87 for phonelux_scrappers/phones_image_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/phones_image_setter.py
r48f3030 r895cd87 1 import unicodedata 2 from datetime import datetime 1 import json 2 import requests 3 import classes.phone 4 import sys 3 5 4 import psycopg25 import config_read6 from bs4 import BeautifulSoup7 import requests8 9 import sys10 6 11 7 file_path = 'outputfile.txt' 12 8 sys.stdout = open(file_path, "w") 13 9 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() 10 phones = json.loads(requests.get('http://localhost:8080/phones').text) 11 for phone in phones: 12 phone_id = str(phone['id']) 13 offers = list(json.loads(requests.get('http://localhost:8080/phones/offers/' + phone_id).text)) 23 14 24 cur.execute('SELECT * FROM PHONES ORDER BY id')15 offers = list(filter(lambda offer: offer['image_url'] is not None, offers)) 25 16 26 db_connection.commit() 27 phones = cur.fetchall() 17 image_url = None 28 18 29 for phone in phones: 30 print(phone) 31 phone_id = phone[0] 32 cur.execute('SELECT phone_offers.image_url FROM phone_offers JOIN phones ON' 33 ' phone_offers.phone_id = phones.id WHERE ' 34 'phones.id='+str(phone_id)+' AND phone_offers.image_url IS NOT NULL LIMIT 1') 35 tuple_image = cur.fetchone() 36 if tuple_image is not None: 37 print(tuple_image[0]) 38 cur.execute('UPDATE phones SET image_url = \''+tuple_image[0]+'\' WHERE id='+str(phone_id)); 39 db_connection.commit() 40 else: 41 print('None'); 19 if len(offers) > 0: 20 image_url = offers[0]['image_url'] 42 21 22 phone['image_url'] = image_url 43 23 44 cur.close() 45 db_connection.close() 24 # UPDATE DATABASE WITH NEW IMAGE URLS FOR PHONES 25 headers = {'Content-type': 'application/json'} 26 requests.put('http://localhost:8080/setimageurl/' + phone_id, headers=headers, data=phone['image_url'])
Note:
See TracChangeset
for help on using the changeset viewer.