forked from kapravel/amazondailyapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amazon.py
executable file
·86 lines (74 loc) · 2.69 KB
/
amazon.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
86
#!/usr/bin/python
import sys
from optparse import OptionParser
import mechanize
from BeautifulSoup import BeautifulSoup
import time,random
from keyring import Keyring
import os
didIbuy = False
def getOptions():
arguments = OptionParser()
arguments.add_options(["--username", "--password", "--firstyear"])
return arguments.parse_args()[0]
def find_appstore(html):
soup = BeautifulSoup(html)
for link in soup.findAll("a", {"class":"nav_a nav_item"}):
if link.find(text="Apps"):
return link["href"]
return None
def isappfree(soup):
if soup.find("span", {"class":"new-price"}, text="Free") == "Free":
return True
return False
def getfreeapp(html, br):
global didIbuy
soup = BeautifulSoup(html)
for widget in soup.findAll("div", {"class":"fad-widget-large"}):
if isappfree(widget):
br.select_form(name="handleBuy")
appurchached = br.submit().read()
#print appurchached
didIbuy = True
else:
print "Free app not free!"
if __name__ == '__main__':
kr = Keyring()
#time.sleep(random.randint(1,120))
options = getOptions()
br = mechanize.Browser()
# br.set_proxies({"http":"localhost:8080"})
br.set_handle_robots(False)
br.addheaders = [("User-agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13")]
sign_in = br.open("http://www.amazon.com/gp/flex/sign-out.html")
br.select_form(name="sign-in")
if options.username != None:
br["email"] = options.username
br["password"] = options.password
else:
while kr.get_user("Amazon") is None:
user = raw_input('Amazon user name:')
password = raw_input('Amazon password:')
ok = raw_input('Save (y/N)?').lower().startswith('y')
if ok:
kr.set_password("Amazon", user, password)
# set your static credentials here if you want
br["email"] = kr.get_user("Amazon")
br["password"] = kr.get_password("Amazon")
logged_in = br.submit().read()
error_str = "The e-mail address and password you entered do not match any accounts on record."
if error_str in logged_in:
print error_str
sys.exit(1)
#print "Successfully logged in!"
appstore_url = find_appstore(logged_in)
if appstore_url == None:
print "Appstore not found. Amazon site has changed!"
sys.exit(0)
for i in range(0,5):
if appstore_url:
appstore_html = br.open(appstore_url).read()
getfreeapp(appstore_html,br)
if didIbuy:
sys.exit(0)
print "I didn't buy the free app today! What's wrong with me? :("