Created
January 14, 2014 08:45
-
-
Save FrankGrimm/8415174 to your computer and use it in GitHub Desktop.
starscape scripts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import mechanize | |
from bs4 import BeautifulSoup | |
import smtplib | |
from email.mime.text import MIMEText | |
def main(): | |
#starbucks account | |
mail = "username" | |
pw = "password" | |
#report mail | |
r_from = 'from@mail' | |
r_to = 'to@mail' | |
r_subject = 'Balance Report' | |
r_host = 'mail.hostname' | |
send_mail = True | |
s = Starscrape(mail, pw) | |
if s.login(): | |
s.printBalance() | |
if send_mail: | |
s.mailResults(r_from, r_to, r_subject, r_host) | |
#print "All done" | |
class Starscrape: | |
def __init__(self, mail, pw): | |
#print "Initializing" | |
self.mail = mail | |
self.pw = pw | |
def mailText(self): | |
return "%s\r\nPrimary (%s): %s (updated: %s)\r\n\r\nAccount Type: %s\r\nStars until free drink: %s\r\nFree drinks:%s\r\n\r\nCards:\r\n%s\r\n" % (self.m_hello, self.m_pri_card, self.m_pri_balance, self.m_pri_update, self.m_account_status, self.m_stars_until, self.m_freedrinks, self.m_cards) | |
def mailResults(self, r_from, r_to, r_subject, r_host): | |
msg = MIMEText(self.mailText().encode('utf-8'), 'plain', 'utf-8') | |
msg['Subject'] = r_subject | |
msg['From'] = r_from | |
msg['To'] = r_to | |
s = smtplib.SMTP(r_host) | |
print s.sendmail(r_from, [r_to], msg.as_string()) | |
print s.quit() | |
return | |
def login(self): | |
success = False | |
#print "Logging in" | |
loginURI = "https://www.starbucks.de/account/signin?ReturnUrl=%2faccount%2fhome" | |
self.browser = mechanize.Browser() | |
self.browser.set_handle_robots(False) | |
self.browser.set_handle_refresh(False) | |
self.browser.addheaders = [('User-agent', 'Firefox 3.14')] | |
self.browser.open(loginURI) | |
#print "Got page data" | |
for frm in self.browser.forms(): | |
if frm.attrs['id'] == 'accountForm': | |
#print "Identified login form" | |
self.browser.form = frm | |
break | |
self.browser["Account.UserName"] = self.mail | |
self.browser["Account.PassWord"] = self.pw | |
self.browser.method = "POST" | |
self.browser.action = loginURI | |
#print "POSTing credentials" | |
response = self.browser.submit() | |
#print "Got response" | |
restext = response.read() | |
if "account_hello" in restext: | |
#print "Login success" | |
success = True | |
self.printRewardInfo(restext) | |
else: | |
print "Login failed" | |
success = False | |
return success | |
def printRewardInfo(self, restext): | |
soup = BeautifulSoup(restext) | |
starinfo = soup.find('p', attrs={'class': 'star-info'}) | |
stage_classes = ['rewards_cup_black', 'rewards_cup_green', 'rewards_cup_gold', 'rewards_bday_black', 'rewards_bday_green', 'rewards_bday_gold'] | |
account_stage = None | |
for current_try_stage in stage_classes: | |
stage_span = starinfo.find('span', attrs={'class': current_try_stage}) | |
if not stage_span is None: | |
account_stage = current_try_stage | |
if account_stage is None: | |
print "Failed to gather reward info" | |
return | |
print "\r\n" | |
print "Account status: %s" % (account_stage.replace('rewards_', '').replace('cup_', '').replace('bday_', '').strip()) | |
self.m_account_status = account_stage.replace('rewards_', '').replace('cup_', '').replace('bday_', '').strip() | |
print "Stars until next free drink: %s" % (starinfo.find('span', attrs={'class': account_stage}).get_text().strip()) | |
self.m_stars_until = starinfo.find('span', attrs={'class': account_stage}).get_text().strip() | |
freedrinks = starinfo.find('span', attrs={'class': 'stars-until'}) | |
if not freedrinks is None: | |
print "Number of free drinks: %s" % (freedrinks.get_text().strip()) | |
self.m_freedrinks = "Number of free drinks: %s" % (freedrinks.get_text().strip()) | |
else: | |
self.m_freedrinks = "" | |
def printBalance(self): | |
cardURI = "https://www.starbucks.de/account/card" | |
#print "Retrieving card data" | |
response = self.browser.open(cardURI) | |
restext = response.read() | |
#print "Parsing response\r\n" | |
soup = BeautifulSoup(restext) | |
for link in soup.find('a', attrs={'class': 'account_hello'}): | |
print "\r\n\"%s\"" % link | |
self.m_hello = "%s" % link | |
break | |
ccar = soup.find('div', attrs={'id': 'card-carousel'}) | |
if ccar is None: | |
print "Could not find card info" | |
return False | |
print "Cards:" | |
self.m_cards = "" | |
card_idx = 0 | |
for card_li in ccar.find_all('li'): | |
cardname = card_li.find('input', attrs={'type': 'image'}).attrs['alt'] | |
cardmsg = "" | |
if card_idx == 0: | |
cardmsg = "Primary card: %s" % cardname | |
else: | |
cardmsg = " Card: %s" % cardname | |
print cardmsg | |
self.m_cards = "%s%s\r\n" % (self.m_cards, cardmsg) | |
card_idx = card_idx + 1 | |
primary_balance = "Error" | |
balance_update = "Never" | |
cardinfo = soup.find('p', attrs={'id': 'card-properties'}) | |
primary_cardname = cardinfo.find('span', attrs={'class': 'nickname'}).get_text().strip() | |
primary_balance = cardinfo.find('span', attrs={'class': 'balance'}).get_text() | |
balance_update = cardinfo.find('span', attrs={'class': 'date'}).get_text() | |
print "" | |
print "Balance of primary card (%s): %s" % (primary_cardname, primary_balance) | |
print "(Updated: %s)" % (balance_update) | |
print "" | |
print "" | |
self.m_pri_card = primary_cardname | |
self.m_pri_balance = primary_balance | |
self.m_pri_update = balance_update | |
return True | |
if __name__ == "__main__": | |
main() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import mechanize | |
from bs4 import BeautifulSoup | |
def main(): | |
mail = "username" | |
pw = "password" | |
s = Starscrape(mail, pw) | |
if s.login(): | |
s.printBalance() | |
#print "All done" | |
class Starscrape: | |
def __init__(self, mail, pw): | |
#print "Initializing" | |
self.mail = mail | |
self.pw = pw | |
def login(self): | |
success = False | |
#print "Logging in" | |
loginURI = "https://www.starbucks.de/account/signin?ReturnUrl=%2faccount%2fhome" | |
self.browser = mechanize.Browser() | |
self.browser.set_handle_robots(False) | |
self.browser.set_handle_refresh(False) | |
self.browser.addheaders = [('User-agent', 'Firefox 3.14')] | |
self.browser.open(loginURI) | |
#print "Got page data" | |
for frm in self.browser.forms(): | |
if frm.attrs['id'] == 'accountForm': | |
#print "Identified login form" | |
self.browser.form = frm | |
break | |
self.browser["Account.UserName"] = self.mail | |
self.browser["Account.PassWord"] = self.pw | |
self.browser.method = "POST" | |
self.browser.action = loginURI | |
#print "POSTing credentials" | |
response = self.browser.submit() | |
#print "Got response" | |
restext = response.read() | |
if "account_hello" in restext: | |
#print "Login success" | |
success = True | |
self.printRewardInfo(restext) | |
else: | |
print "Login failed" | |
success = False | |
return success | |
def printRewardInfo(self, restext): | |
soup = BeautifulSoup(restext) | |
starinfo = soup.find('p', attrs={'class': 'star-info'}) | |
stage_classes = ['rewards_cup_black', 'rewards_cup_green', 'rewards_cup_gold', 'rewards_bday_black', 'rewards_bday_green', 'rewards_bday_gold'] | |
account_stage = None | |
for current_try_stage in stage_classes: | |
stage_span = starinfo.find('span', attrs={'class': current_try_stage}) | |
if not stage_span is None: | |
account_stage = current_try_stage | |
if account_stage is None: | |
print "Failed to gather reward info" | |
return | |
print "\r\n" | |
print "Account status: %s" % (account_stage.replace('rewards_', '').replace('cup_', '').replace('bday_', '').strip()) | |
print "Stars until next free drink: %s" % (starinfo.find('span', attrs={'class': account_stage}).get_text().strip()) | |
freedrinks = starinfo.find('span', attrs={'class': 'stars-until'}) | |
if not freedrinks is None: | |
print "Number of free drinks: %s" % (freedrinks.get_text().strip()) | |
def printBalance(self): | |
cardURI = "https://www.starbucks.de/account/card" | |
#print "Retrieving card data" | |
response = self.browser.open(cardURI) | |
restext = response.read() | |
#print "Parsing response\r\n" | |
soup = BeautifulSoup(restext) | |
for link in soup.find('a', attrs={'class': 'account_hello'}): | |
print "\r\n\"%s\"" % link | |
break | |
ccar = soup.find('div', attrs={'id': 'card-carousel'}) | |
if ccar is None: | |
print "Could not find card info" | |
return False | |
print "Cards:" | |
card_idx = 0 | |
for card_li in ccar.find_all('li'): | |
cardname = card_li.find('input', attrs={'type': 'image'}).attrs['alt'] | |
if card_idx == 0: | |
print "Primary card: %s" % cardname | |
else: | |
print " Card: %s" % cardname | |
card_idx = card_idx + 1 | |
primary_balance = "Error" | |
balance_update = "Never" | |
cardinfo = soup.find('p', attrs={'id': 'card-properties'}) | |
primary_cardname = cardinfo.find('span', attrs={'class': 'nickname'}).get_text().strip() | |
primary_balance = cardinfo.find('span', attrs={'class': 'balance'}).get_text() | |
balance_update = cardinfo.find('span', attrs={'class': 'date'}).get_text() | |
print "" | |
print "Balance of primary card (%s): %s" % (primary_cardname, primary_balance) | |
print "(Updated: %s)" % (balance_update) | |
print "" | |
print "" | |
return True | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment