2020-08-22 08:55:15 +00:00
|
|
|
import json, os, pickle
|
2020-08-21 10:32:43 +00:00
|
|
|
from os import path
|
2020-08-23 11:29:53 +00:00
|
|
|
from datetime import datetime
|
2020-08-21 10:32:43 +00:00
|
|
|
|
2020-08-21 10:52:40 +00:00
|
|
|
import functions
|
2020-08-23 11:29:53 +00:00
|
|
|
from listing import YahooAuctionsItem, JST
|
2020-08-23 11:18:11 +00:00
|
|
|
|
2020-08-21 10:32:43 +00:00
|
|
|
class BuypeebSettings:
|
|
|
|
def __init__(self, location: str):
|
2020-08-22 08:55:15 +00:00
|
|
|
self.updateInterval = 10 * 60
|
|
|
|
self.favouriteUpdateInterval = 5 * 60
|
|
|
|
self.updateIntervalCritical = 60
|
|
|
|
self.favouriteUpdateIntervalCritical = 30
|
2020-08-22 09:26:09 +00:00
|
|
|
self.watchlist = {}
|
|
|
|
self.jwatchlist = {}
|
2020-08-21 10:32:43 +00:00
|
|
|
self.location = location
|
|
|
|
|
2020-08-23 15:09:08 +00:00
|
|
|
def save(self, backup = False):
|
2020-08-22 09:26:09 +00:00
|
|
|
for id, item in self.watchlist.items():
|
|
|
|
self.jwatchlist[id] = {
|
2020-08-22 08:55:15 +00:00
|
|
|
"name": item.name,
|
|
|
|
"original_name": item.original_name,
|
|
|
|
"favourite": False
|
2020-08-22 09:26:09 +00:00
|
|
|
}
|
2020-08-22 08:55:15 +00:00
|
|
|
|
2020-08-23 15:09:08 +00:00
|
|
|
# create a shallow copy to avoid deleting the actual watchlist
|
|
|
|
out_dict = self.__dict__.copy()
|
|
|
|
out_dict['watchlist'] = {}
|
2020-08-22 08:55:15 +00:00
|
|
|
|
2020-08-23 15:14:54 +00:00
|
|
|
print(out_dict)
|
|
|
|
|
2020-08-23 15:09:08 +00:00
|
|
|
filename = "config.json"
|
|
|
|
if backup:
|
|
|
|
filename += ".bak"
|
|
|
|
json.dump(out_dict, open(self.location + filename, 'w'))
|
2020-08-21 10:32:43 +00:00
|
|
|
|
2020-08-23 15:14:54 +00:00
|
|
|
self.jwatchlist = {}
|
|
|
|
|
2020-08-21 10:32:43 +00:00
|
|
|
def load(self):
|
|
|
|
if not path.isfile(self.location + "config.json"):
|
2020-08-22 08:55:15 +00:00
|
|
|
os.makedirs(self.location, 0o755, True)
|
2020-08-21 10:32:43 +00:00
|
|
|
open(self.location + "config.json", 'x')
|
2020-08-23 15:09:08 +00:00
|
|
|
open(self.location + "config.json.bak", 'x')
|
2020-08-22 08:55:15 +00:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
j = json.load(open(self.location + "config.json", 'r'))
|
|
|
|
for key in j:
|
|
|
|
setattr(self, key, j[key])
|
2020-08-22 09:26:09 +00:00
|
|
|
for key in self.jwatchlist:
|
2020-08-23 15:00:05 +00:00
|
|
|
self.watchlist[key] = YahooAuctionsItem(url = None, from_json = self.jwatchlist[key], id = key)
|
2020-08-21 10:35:53 +00:00
|
|
|
|
2020-08-22 09:26:09 +00:00
|
|
|
self.jwatchlist = {}
|
2020-08-21 10:35:53 +00:00
|
|
|
|
2020-08-23 15:09:08 +00:00
|
|
|
self.save(backup = True)
|
|
|
|
|
2020-08-22 08:55:15 +00:00
|
|
|
except:
|
|
|
|
raise
|
|
|
|
print("Couldn't load settings - using defaults")
|
2020-08-21 10:52:40 +00:00
|
|
|
|
2020-08-23 14:47:40 +00:00
|
|
|
def watch(self, url: str, name: str = ""):
|
2020-08-22 09:26:09 +00:00
|
|
|
id = functions.id_from_url(url)
|
|
|
|
self.watchlist[id] = YahooAuctionsItem(url, id, name)
|
|
|
|
for item in self.watchlist.values():
|
2020-08-22 08:55:15 +00:00
|
|
|
print(item.name, item.price)
|
2020-08-23 11:18:11 +00:00
|
|
|
|
|
|
|
def outdated_items(self):
|
|
|
|
out = []
|
|
|
|
now = datetime.now(tz = JST).timestamp()
|
|
|
|
for item in self.watchlist.values():
|
|
|
|
time_difference = now - item.last_checked.timestamp()
|
|
|
|
ending_soon = False # TODO: this
|
|
|
|
|
2020-08-23 11:29:53 +00:00
|
|
|
if (item.favourite and time_difference >= self.favouriteUpdateInterval) or \
|
2020-08-23 11:18:11 +00:00
|
|
|
(item.favourite and ending_soon and time_difference >= self.favouriteUpdateIntervalCritical) or \
|
|
|
|
(time_difference >= self.updateInterval) or \
|
|
|
|
(ending_soon and time_difference >= self.updateIntervalCritical):
|
|
|
|
out.append(item)
|
|
|
|
|
|
|
|
return out
|