-
Notifications
You must be signed in to change notification settings - Fork 0
/
old_and_ugly_ui.py
142 lines (115 loc) · 4.83 KB
/
old_and_ugly_ui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import tkinter as tk
from tkinter import ttk
from ttkwidgets.autocomplete import AutocompleteCombobox
from requests import get
from pymongo import MongoClient
from decouple import config
BASE_URL = 'https://www.albion-online-data.com/api/v2/stats/Prices/'
############################# MONGO #############################
# Conectar a la base de datos
client = config("MONGOAPI")
client = MongoClient(client)
db = client['AlbionMarketplace']
items = db['items']
world = db['world']
############################# TKINTER #############################
# Ventana principal
root = tk.Tk()
root.title("Albion Marketplace")
# NOTEBOOKS ---------------------
# Create a notebook for the tabs
notebook = ttk.Notebook(root)
notebook.pack(fill="both", expand=True)
# FRAMES ---------------------
# Create a frame for the comparision tab
comparision_frame = ttk.Frame(notebook, padding=10)
notebook.add(comparision_frame, text="Comparación")
# Create a frame for the items tab
allitems_frame = ttk.Frame(notebook, padding=10)
notebook.add(allitems_frame, text="Items")
# Frame de los ítems
items_frame = tk.Frame(root)
items_frame.pack(side=tk.TOP)
# Frame de los precios
prices_frame = tk.Frame(root)
prices_frame.pack(side=tk.TOP)
# Crear una frame para la combobox en la pestaña "Comparación"
combobox_frame = ttk.Frame(comparision_frame, padding=10)
combobox_frame.pack(side=tk.TOP)
# LISTBOX ---------------------
# Lista desplegable de ítems en la comparación
item_var = tk.StringVar()
item_combobox = AutocompleteCombobox(
combobox_frame,
textvariable=item_var,
completevalues=sorted([i['UniqueName'] for i in items.find()]))
item_combobox.pack(side=tk.LEFT, padx=5)
item_combobox.config(width=60)
# Crear un scrollbar
scrollbar = tk.Scrollbar(allitems_frame)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
# Crear la lista desplegable y asociarla con el scrollbar
items_listbox = tk.Listbox(allitems_frame, width=40, height=10, yscrollcommand=scrollbar.set)
items_listbox.pack(side=tk.LEFT, padx=5)
# Configurar el scrollbar para controlar la lista
scrollbar.config(command=items_listbox.yview)
# Agregar los elementos de la lista
for item in items.find():
items_listbox.insert(tk.END, item['UniqueName'])
# Frame de los precios
prices_frame = tk.Frame(comparision_frame)
prices_frame.pack(side=tk.LEFT, padx=5, fill=tk.BOTH, expand=True)
# FUNCTIONS ---------------------
def search():
item = item_var.get()
for child in prices_frame.winfo_children():
child.destroy()
prices = [] # Lista para almacenar los precios
for i, city_doc in enumerate(world.find()):
city_name = city_doc['UniqueName']
label = tk.Label(prices_frame, text=city_name + ": ")
label.grid(row=i, column=0, sticky=tk.E, padx=5, pady=5)
params = {
'item': item,
'locations': city_name,
'qualities': '1'
}
response = get(f'{BASE_URL}{item}.json', params=params)
# Comprobación de éxito en la api
if response.status_code == 200:
response_data = response.json()[0]
price = response_data['sell_price_min']
if price is not None and price != 0: # Excluir valores en 0 y None
price = int(price)
prices.append(price) # Añadir precio a la lista
price_label = tk.Label(prices_frame, text=str(price) + " Plata")
price_label.grid(row=i, column=1, sticky=tk.W, padx=5, pady=5)
else:
price_label = tk.Label(prices_frame, text="No hay existencias")
price_label.grid(row=i, column=1, sticky=tk.W, padx=5, pady=5)
else:
price_label = tk.Label(prices_frame, text="Error al obtener precios")
price_label.grid(row=i, column=1, sticky=tk.W, padx=5, pady=5)
# Encontrar el mínimo y el máximo precio, si hay precios
if prices:
min_price = min(prices)
max_price = max(prices)
else:
min_price = 0
max_price = 0
# Asignar el color de acuerdo al precio mínimo o máximo
for i, child in enumerate(prices_frame.winfo_children()):
if isinstance(child, tk.Label) and "Plata" in child.cget("text"):
price = int(child.cget("text").replace(" Plata", ""))
if price == min_price:
child.config(fg='green')
elif price == max_price:
child.config(fg='red')
# Autocompletados
item_combobox.bind("<<ComboboxSelected>>", lambda event: item_var.set(item_combobox.get()))
item_combobox.bind("<<ComboboxSelected>>", lambda event: item_combobox.config(completevalues=sorted([i['UniqueName'] for i in items.find()])))
# BUTTONS ---------------------
search_button = tk.Button(comparision_frame, text="Buscar", command=search)
search_button.pack(side=tk.TOP, padx=5)
# Mostrar la ventana principal
root.mainloop()