forked from bbskreets/switch-snatcher-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebsite.py
85 lines (68 loc) · 2.51 KB
/
Website.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
"""
------------------------------------------------------------------------
Website.py
Where website info is stored
------------------------------------------------------------------------
Author: bb $kreetz
Email: [email protected]
__updated__ = "2020-04-12"
------------------------------------------------------------------------
"""
#internal imports
from CONSTANTS import *
from SETTINGS import *
#imports
from datetime import datetime
import os
import json
class Website():
def __init__(self, uuid, url=None, site_type=None, max_price=None):
"""
:param url: url for link to check
:param max_price: maximum price willing to pay
"""
self.uuid = uuid
if os.path.exists(f'{WEBSITES_PATH}/{self.uuid}.json'):
with open(f'{WEBSITES_PATH}/{self.uuid}.json', 'r') as fh:
file_data = json.loads(fh.read())
self.url = file_data['url']
self.site_type = file_data['site_type']
self.max_price = float(file_data['max_price'])
self.name = file_data['name']
self.cur_price = float(file_data['cur_price'])
self.in_stock = file_data['in_stock']
self.last_checked = datetime.strptime(file_data['last_checked'], DATETIME_STR)
else:
self.url = url
self.site_type = site_type
self.max_price = max_price
self.name = None
self.cur_price = None
self.in_stock = None
self.last_checked = None
def __str__(self):
return '{:^9}|{:^10}|{:^55}|{:^8}|{:^6}|'.format(
self.last_checked.strftime("%H:%M:%S"),self.site_type.upper(),self.name,self.cur_price, str(self.in_stock)
)
def _save(self):
file_data = {
"url":self.url,
'site_type':self.site_type,
"max_price":self.max_price,
"name":self.name,
"cur_price":self.cur_price,
"in_stock":self.in_stock,
"last_checked":self.last_checked.strftime(DATETIME_STR)
}
with open(f'{WEBSITES_PATH}/{self.uuid}.json', 'w') as fh:
json.dump(file_data, fh, indent=2)
return
def update_website(self, cur_price, in_stock, name):
self.name = name
self.cur_price = cur_price
self.in_stock = in_stock
self.last_checked = datetime.now()
self._save()
@staticmethod
def remove_website(uuid):
os.remove(f'{WEBSITES_PATH}/{uuid}.json')