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

    r48f3030 r895cd87  
     1import json
    12
    23import psycopg2
     4import requests
    35import config_read
    46import sys
     
    79sys.stdout = open(file_path, "w")
    810
    9 # Call to read the configuration file and connect to database
    10 cinfo = config_read.get_databaseconfig("postgresdb.config")
    11 db_connection = psycopg2.connect(
    12     database=cinfo[0],
    13     host=cinfo[1],
    14     user=cinfo[2],
    15     password=cinfo[3]
    16 )
    1711
    18 cur = db_connection.cursor()
    19 
    20 cur.execute('SELECT id, model FROM phones;')
    21 phones = cur.fetchall()
     12phones = json.loads(requests.get('http://localhost:8080/phones').text)
    2213
    2314for phone in phones:
    24     cur.execute('SELECT COUNT(*) FROM phone_offers WHERE phone_id='+str(phone[0])+';')
    2515
    26     total_offers = cur.fetchone()[0]
     16    phone_id = str(phone['id'])
     17    totaloffers = requests.get('http://localhost:8080/totaloffers/'+str.join('*', phone['model'].split(' '))).text
    2718
    28     cur.execute('UPDATE phones SET total_offers='+str(total_offers)+' WHERE id='+str(phone[0])+';')
    29     db_connection.commit()
     19    print(phone_id+'  -  '+totaloffers)
    3020
    31 
    32 cur.close()
    33 db_connection.close()
     21    # UPDATE DATABASE WITH NEW TOTAL OFFERS
     22    requests.put('http://localhost:8080/settotaloffers/' + phone_id + '/' + totaloffers)
Note: See TracChangeset for help on using the changeset viewer.