forked from itsbilal/NexusNotifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
59 lines (46 loc) · 2.38 KB
/
main.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
#
# Python script to check if Google's Nexus 4 is in stock.
#
# Copyright (C) 2012 Bilal Akhtar <me@itsbilal.com>
# Licensed under the MIT License, see COPYING
#
import argparse
from datetime import datetime
from email.mime.text import MIMEText
import smtplib
import urllib2
LOGFILENAME="nexus.log"
# ArgParse part
parser = argparse.ArgumentParser(description="Check stock of a Nexus device on Google Play")
parser.add_argument("--model", type=int, nargs="?", default=4, help="The type of Nexus to search for (Enter 4 for Nexus 4, 7 for Nexus 7, etc)")
parser.add_argument("--storage", type=int, nargs="?", default=8, help="The storage size of the nexus device you're searching for (enter 8 for 8gb, 16 for 16gb and so on)")
parser.add_argument("--email", type=str, nargs="?", default="", help="The e-mail address to which an e-mail should be sent if the Nexus is in stock")
cmdargs = parser.parse_args()
NEXUS_TYPE = cmdargs.model
NEXUS_STORAGE_SIZE = "%dgb" % cmdargs.storage
email_address = cmdargs.email
try:
gplay_url = "https://play.google.com/store/devices/details?id=nexus_%d_%s" % (NEXUS_TYPE, NEXUS_STORAGE_SIZE)
response = urllib2.urlopen(gplay_url)
http = response.read()
if http.find("Add to Cart") >= 0:
logstring = "%s: FOUND: Add to Cart button detected on Google Play. Nexus %d %s is in stock." % (datetime.now(), NEXUS_TYPE, NEXUS_STORAGE_SIZE)
if len(email_address) > 0:
# Send mail
msg = MIMEText("Nexus %d %s IS IN STOCK! \n\n Get it now! %s" % (NEXUS_TYPE, NEXUS_STORAGE_SIZE, gplay_url))
msg['Subject'] = "Nexus %d in stock" % NEXUS_TYPE
msg['From'] = email_address
msg['To'] = email_address
smtp = smtplib.SMTP("localhost")
smtp.sendmail(email_address, email_address, msg.as_string())
smtp.quit()
elif http.find("Sold out") >= 0:
logstring = "%s: Nexus %d %s is out of stock and sold out" % (datetime.now(), NEXUS_TYPE, NEXUS_STORAGE_SIZE)
else:
logstring = "%s: Not sure about stock status, Nexus %d %s is most probably out of stock" % (datetime.now(), NEXUS_TYPE, NEXUS_STORAGE_SIZE)
except (urllib2.HTTPError):
logstring = "%s: Google Play returned HTTP 404. Are you searching for a valid Nexus device?" % datetime.now()
print logstring
logfile = open(LOGFILENAME, "a")
logfile.write(logstring+"\n")
logfile.close()