-
Notifications
You must be signed in to change notification settings - Fork 129
/
main.py
executable file
·137 lines (112 loc) · 3.94 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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python
#
# developed by Sergey Markelov (2013)
#
import HTMLParser
import getopt
import sys
import urllib2
import xml.etree.ElementTree as et
from bingAuth import BingAuth, AuthenticationError
from bingRewards import BingRewards
import bingCommon
import bingFlyoutParser as bfp
import helpers
verbose = False
def earnRewards(authType, login, password):
"""Earns Bing! reward points and returnes how many points has been earned"""
pointsEarned = 0
noException = False
try:
if authType is None: raise ValueError("authType is None")
if login is None: raise ValueError("login is None")
if password is None: raise ValueError("password is None")
bingRewards = BingRewards()
bingAuth = BingAuth(bingRewards.opener)
bingAuth.authenticate(authType, login, password)
points = bingRewards.getRewardsPoints()
rewards = bfp.parseFlyoutPage(bingRewards.requestFlyoutPage(), bingCommon.BING_URL)
if verbose:
bingRewards.printRewards(rewards)
results = bingRewards.process(rewards)
if verbose:
print
print "-" * 80
print
bingRewards.printResults(results, verbose)
newPoints = bingRewards.getRewardsPoints()
lifetimeCredits = bingRewards.getLifetimeCredits()
pointsEarned = newPoints - points
print
print "%s - %s" % (authType, login)
print
print "Points before: %6d" % points
print "Points after: %6d" % newPoints
print "Points earned: %6d" % pointsEarned
print "Lifetime Credits: %6d" % lifetimeCredits
print
print "-" * 80
noException = True
except AuthenticationError, e:
print "AuthenticationError:\n%s" % e
except HTMLParser.HTMLParseError, e:
print "HTMLParserError: %s" % e
except urllib2.HTTPError, e:
print "The server couldn't fulfill the request."
print "Error code: ", e.code
except urllib2.URLError, e:
print "Failed to reach the server."
print "Reason: ", e.reason
finally:
if not noException:
print
print "For: %s - %s" % (authType, login)
print
print "-" * 80
return pointsEarned
def usage():
print "Usage:"
print " -h, --help show this help"
print " -f, --configFile=file use specific config file. Default is config.xml"
print " -v, --verbose print verbose output"
if __name__ == "__main__":
try:
opts, args = getopt.getopt(sys.argv[1:], "hf:v", ["help", "configFile=", "verbose"])
except getopt.GetoptError, e:
print "getopt.GetoptError: %s" % e
usage()
sys.exit(1)
configFile = "config.xml"
for o, a in opts:
if o in ("-h", "--help"):
usage()
sys.exit()
elif o in ("-f", "--configFile"):
configFile = a
elif o in ("-v", "--verbose"):
verbose = True
else:
raise NotImplementedError("option '" + o + "' is not implemented")
print "%s - script started" % helpers.getLoggingTime()
print "-" * 80
print
helpers.createResultsDir(__file__)
try:
tree = et.parse(configFile)
except IOError, e:
print "IOError: %s" % e
sys.exit(2)
totalPoints = 0
root = tree.getroot()
for accounts in root.findall("accounts"):
for account in accounts.findall("account"):
isDisabled = True if account.get("disabled", "false").lower() == "true" else False
if isDisabled:
continue
accountType = account.get("type")
login = account.find("login").text
password = account.find("password").text
totalPoints += earnRewards(accountType, login, password)
print "Total points earned: %d" % totalPoints
print
print "%s - script ended" % helpers.getLoggingTime()