Browse Source

added files

master
Raphael Roberts 7 years ago
parent
commit
9ca91045db
  1. 72
      interface/price.py
  2. 71
      interface/quad_dialog.py
  3. BIN
      interface/quadparts.db

72
interface/price.py

@ -0,0 +1,72 @@
import copy
import sys
import os
sys.path.insert(0,r"X:\Users\Ralphie\Documents\local_repo\price_finder")
dirname = os.path.dirname(__file__)
sys.path.insert(0,dirname)
# print(dirname)
sys.path.insert(0,dirname)
from batch_process import get_prices
from quad_dialog_new import message,dicter
import datetime
import sqlite3
import pandas
from decimal import Decimal as D
import argparse
# print('imports done')
# print(os.getcwd())
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--stdout',action = 'store')
parser.add_argument('--stderr',action = 'store')
args = parser.parse_args()
# print('args parsed')
time = str(datetime.datetime.today())
# breakpoint()
if args.stdout:
with open(args.stdout,'w') as file:
file.write(time)
# print("stdout written")
sys.stdout = open(args.stdout,'a')
if args.stderr:
with open(args.stderr,'w') as file:
file.write(time)
# print("stderr written")
sys.stderr = open(args.stderr,'a')
con = sqlite3.connect(os.path.join(dirname,'quadparts.db'))
cur = con.cursor()
date = str(datetime.date.today())
result = list(cur.execute('SELECT * FROM parts WHERE date == ? LIMIT 1',(date,)))
quad_links = list(row[0] for row in cur.execute('SELECT link FROM links ORDER BY id'))
if not result:
print('Executing')
quad_parts_base = pandas.read_sql('SELECT * FROM names',con).to_dict('records')
products = get_prices(quad_links,use_proxies=True)
current = copy.deepcopy(quad_parts_base)
complete = list({'product_name':product.info_product['product_name'],
'price': float(product.info_product['price']),
'date': product.time.strftime('%Y-%m-%d'),
'time': product.time.strftime('%H:%M:%S.%f'),
'location':product.info_url.netloc,
'link_id': quad_links.index(product.url)} for product in products)
df = pandas.DataFrame(complete)
df.to_sql('parts',con,if_exists='append',index=False)
for i in range(len(complete)):
complete[i]['price'] = round(D(complete[i]['price']),2)
for item in current:
name = item['name']
link = item['link']
item['product_name'] = complete[link]['product_name']
item['price'] = complete[link]['price']
item['time'] = complete[link]['time']
bought = message(quad_parts_base,current)
quad_parts_base = dicter(quad_parts_base)
for key,is_purchased in bought.items():
if is_purchased:
if quad_parts_base[key]['count'] > 1:
cur.execute('UPDATE names SET count = count - 1 WHERE name == ?',(key,))
else:
cur.execute('UPDATE names SET purchased = 1 WHERE name == ?',(key,))
con.commit()
con.close()

71
interface/quad_dialog.py

@ -0,0 +1,71 @@
# import saveto
import datetime
from decimal import Decimal as D
from tkinter import *
def dicter(l):
ret = {}
for thing in l:
name = thing['name']
ret[name] = thing
return ret
def message(qpb,qpd):
qpb,qpd = map(dicter,(qpb,qpd))
def button_callback(arg):
check_boxes[arg].toggle()
prods = list(filter(lambda item: not item[1]['purchased'],qpb.items()))
root = Tk()
checks = dict(
(prod[0],BooleanVar()) for prod in prods)
root.wm_title("Today's Prices")
labels = ["","Product name","Price","Count","Total"]
for i,label in enumerate(labels):
Label(root,text = label).grid(row =0,column = i)
row = 1
total = 0
single_total = 0
buttons = []
check_boxes = []
for prod,data in prods:
check_boxes.append(
Checkbutton(root,variable=checks[prod],state = DISABLED)
)
check_boxes[-1].grid(row = row,column = labels.index(""),stick=E)
buttons.append(
Button(
root,text=prod,command = lambda row=row-1: button_callback(row)
)
)
buttons[-1].grid(row=row,column = labels.index("Product name"),sticky = E+W)
price = D(qpd[prod]['price'])
row_total = data['count']*price
Label(root,text = '${}'.format(price),relief = "sunken").grid(row = row,column=labels.index('Price'),sticky=E+W)
Label(root,text = data['count'],relief = "sunken").grid(row=row,column=labels.index('Count'),sticky = E+W)
Label(root,text = '${}'.format(row_total),relief = "sunken").grid(row = row,column=labels.index("Total"),sticky=E+W)
total += row_total
single_total += price
row += 1
Label(root,text = "Total:").grid(row = row,column=labels.index("Count"))
Label(root,text = "Individual:").grid(row = row,column=labels.index("Product name"),sticky = E)
Label(root,text = "${}".format(total)).grid(row=row,column = labels.index("Total"))
Label(root,text = "${}".format(single_total)).grid(row=row,column=labels.index("Price"))
Button(root,text = "OK", command = root.quit).grid(row=row+2)
root.overrideredirect(True)
root.wm_attributes("-topmost", True)
mainloop()
root.destroy()
for key in checks.keys():
checks[key] = checks[key].get()
return checks

BIN
interface/quadparts.db

Loading…
Cancel
Save