0%

simple script monitor

Python Script

This script is used to monitor whether switch(game console) is available or not.

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
#!/usr/bin/python

import urllib2
import smtplib
import textwrap

SOLD_OUT_MAIL = 0
AVAILABLE_MAIL = 1

def sendMail( FROM, TO, SUBJECT, TEXT):

message = textwrap.dedent("""\
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT))
# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP('localhost')
s.sendmail(FROM, [TO], message)
s.quit()

websites = {
'bestbuy' : {
'web' : 'http://www.bestbuy.com/site/nintendo-switch-32gb-console-gray-joy-con/5670003.p?skuId=5670003',
'keyword' : 'sold out'
},
'target' : {
'web' : 'http://www.target.com/p/nintendo-switch-with-gray-joy-con/-/A-52052007',
'keyword' : 'product not available'
},
'toysrus' : {
'web' : 'http://www.toysrus.com/product/index.jsp?productId=119513636&cp=2255974.119659196&parentPage=family',
'keyword' : 'out of stock'
},
'gamestop' : {
'web' : '',
'keyword' : ''
}
}

for name, content in websites.iteritems():
# handle empty entry
if not content['web']:
continue

# handle network failure
try:
response = urllib2.urlopen(content['web'])
except Exception, e:
print '%-10s %s\n' % (name, e)
continue

html = response.read().lower()
if html.find(content['keyword']) != -1:
print '%-10s sold out\n' % name
if SOLD_OUT_MAIL == 1:
sendMail('gfxcc@ubuntu.com', 'yong_stevens@outlook.com', 'switch sold out', name)
else:
print '%-10s accessible\n' % name
if AVAILABLE_MAIL == 1:
sendMail('gfxcc@ubuntu.com', 'yong_stevens@outlook.com', 'switch might available now', name)