-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.py
62 lines (48 loc) · 1.74 KB
/
parse.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
#!/usr/bin/python3
import configparser
import urllib.request
import json
import re
# Import smtplib for the actual sending function
import smtplib
# Import the email modules we'll need
from email.message import EmailMessage
config = configparser.ConfigParser()
config.read('config.ini')
phones = {
"iphone13": 'https://shop.theclub.com.hk/iphone-13.html',
"iphone13pro": 'https://shop.theclub.com.hk/iphone-13-pro.html',
"iphone13promax": 'https://shop.theclub.com.hk/iphone13-promax.html'
}
http_headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4644.0 Safari/537.36 Edg/96.0.1028.0'
}
global_has_stock = False
smtp_server = config['DEFAULT']['SMTP_Server']
msg = EmailMessage()
msg['Subject'] = config['DEFAULT']['Subject']
msg['From'] = config['DEFAULT']['From']
msg['To'] = config['DEFAULT']['To']
msg_text = ''
for phone, phone_url in phones.items():
rq = urllib.request.Request(phone_url, headers=http_headers, method='GET')
fp = urllib.request.urlopen(rq)
mybytes = fp.read()
html = mybytes.decode("utf8")
fp.close()
JSON = re.compile(
'\"\#product_addtocart_form\"\:\s+({.*?}),\s+\"\*\"', flags=re.DOTALL | re.MULTILINE)
matches = JSON.search(html)
inventory = json.loads(matches.group(1))
salable = inventory["configurable"]["spConfig"]["isSalableOptions"]
model_has_stock = False
for key, values in salable.items():
if values["is_salable"]:
global_has_stock = True
model_has_stock = True
msg_text += phone + ", " + phone_url + " ," + str(model_has_stock) + '\n'
if global_has_stock:
msg.set_content(msg_text)
s = smtplib.SMTP(smtp_server)
s.send_message(msg)
s.quit()