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

    r48f3030 r895cd87  
    1 import unicodedata
    2 from datetime import datetime
     1import json
     2import requests
     3import classes.phone
     4import sys
    35
    4 import psycopg2
    5 import config_read
    6 from bs4 import BeautifulSoup
    7 import requests
    8 
    9 import sys
    106
    117file_path = 'outputfile.txt'
    128sys.stdout = open(file_path, "w")
    139
    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()
     10phones = json.loads(requests.get('http://localhost:8080/phones').text)
     11for phone in phones:
     12    phone_id = str(phone['id'])
     13    offers = list(json.loads(requests.get('http://localhost:8080/phones/offers/' + phone_id).text))
    2314
    24 cur.execute('SELECT * FROM PHONES ORDER BY id')
     15    offers = list(filter(lambda offer: offer['image_url'] is not None, offers))
    2516
    26 db_connection.commit()
    27 phones = cur.fetchall()
     17    image_url = None
    2818
    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']
    4221
     22    phone['image_url'] = image_url
    4323
    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.