Skip to content

Instantly share code, notes, and snippets.

@streetgt
Created July 29, 2017 15:27
Show Gist options
  • Save streetgt/24939a4bfe25e28ae8f7ab429992139a to your computer and use it in GitHub Desktop.
Save streetgt/24939a4bfe25e28ae8f7ab429992139a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Bot for send you the results of your played keys.
# Works only for Friday.
# Addapt it if you want to.
import requests, datetime, smtplib, sys
from email.mime.text import MIMEText
from lxml import etree
def return_prize(fnums, fstars):
if fnums == 5 and fstars == 2:
return 1
elif fnums == 5 and fstars == 1:
return 2
elif fnums == 5 and fstars == 0:
return 3
elif fnums == 4 and fstars == 2:
return 4
elif fnums == 4 and fstars == 1:
return 5
elif fnums == 3 and fstars == 2:
return 6
elif fnums == 4 and fstars == 0:
return 7
elif fnums == 2 and fstars == 2:
return 8
elif fnums == 3 and fstars == 1:
return 9
elif fnums == 3 and fstars == 0:
return 10
elif fnums == 1 and fstars == 2:
return 11
elif fnums == 2 and fstars == 1:
return 12
elif fnums == 2 and fstars == 0:
return 13
else:
return 0
def sendemail(from_addr, to_addr_list, subject, message, login, password, smtpserver='smtp.gmail.com', smtpport=587): # split smtpserver and -port
msg = MIMEText(message)
msg['Subject'] = subject
msg['From'] = "Bot Euromilhoes <{0}>".format(from_addr)
msg['To'] = ", ".join(to_addr_list)
server = smtplib.SMTP(smtpserver, smtpport) # use both smtpserver and -port
server.starttls()
server.login(login,password)
problems = server.sendmail(from_addr, to_addr_list, msg.as_string())
server.quit()
if __name__ == '__main__':
reload(sys)
sys.setdefaultencoding('utf8')
f = open("results.txt","a+")
# Your keys!
mykeys = [
[
[6,12,26,44,48],[1,8]
],
[
[10,13,14,22,39],[1,5]
],
[
[6,7,10,21,46],[5,9]
],
[
[16,30,38,43,46],[5,11]
],
[
[8,13,22,26,31],[2,7]
]
]
result = []
prices = []
r = requests.get('https://www.jogossantacasa.pt/web/SCCartazResult/')
if r.status_code != 200:
print '{0} > [ERROR] GET From Websiste'.format(datetime.datetime.now())
sys.exit(1)
page = etree.HTML( r.content )
table_prices = page.xpath('//*[@id="frmContestSelection"]/div/div[2]/div[3]/div/div/div[5]/ul')
for elem in table_prices:
try:
prices.append(elem.xpath('li[5]/text()')[0].replace('\t','').replace('\n','').replace(' ',''))
except IndexError:
print '{0} > tabela de precos ainda nao disponivel'.format(datetime.datetime.now())
sys.exit(1)
splitter = page.xpath('//*[@id="frmContestSelection"]/div/div[2]/div[3]/div/div/div[3]/ul/li[1]/text()')[0].split('+')
key = {
'date': page.xpath('//*[@id="frmContestSelection"]/div/div[2]/div[3]/div/div/span/text()')[1][25:],
'numbers': splitter[0][:-1],
'stars' : splitter[1][1:]
}
last_result = open("last_result.txt","r")
lastgame = datetime.datetime.strptime(last_result.readline().replace('/',''), "%d%m%Y").date()
currentgame = datetime.datetime.strptime(key['date'].replace('/',''), "%d%m%Y").date()
last_result.close()
if lastgame == currentgame:
print '{0} [ERROR] That draw is already writen in the file '.format(datetime.datetime.now())
sys.exit(1)
last_result = open("last_result.txt","w")
last_result.write("{0}".format(key['date']))
last_result.close()
# So se for sexta feira
if currentgame.weekday() != 4:
print '{0} > [ERROR] Today is not Friday'.format(datetime.datetime.now())
sys.exit(1)
nums = list(map(int, key['numbers'].split(' ')))
stars = list(map(int, key['stars'].split(' ')))
#print key['date'] ,' | ', key['numbers'] , ' - ' , key['stars']
head_message = "\r\nDate: {0} - Key: {1} - {2} \r\n\r\n".format(key['date'],key['numbers'],key['stars'])
f.write(head_message)
for keys in range(5): # 5 Keys
total_nums = 0
total_stars = 0
for n in nums:
for a in range(5):
if mykeys[keys][0][a] == n:
total_nums = total_nums + 1
for n in stars:
for b in range(2):
if mykeys[keys][1][b] == n:
total_stars = total_stars + 1
result.append([mykeys[keys],total_nums, total_stars])
body_msg = []
for i in range(5): # 5 Keys
prize = return_prize(result[i][1],result[i][2])
if prize != 0:
string = "\t{0}\r\n\tKey with [ {1}º prize {2}] ({3}-{4})\r\n\r\n".format(result[i][0], prize, prices[prize-1],result[i][1],result[i][2])
body_msg.append(string)
f.write(string)
else:
string = "\t{0}\r\n\tKey without prize ({1}-{2})\r\n\r\n".format(result[i][0], result[i][1],result[i][2])
body_msg.append(string)
f.write(string)
f.close()
sendemail(from_addr = '[email protected]', # Email Address
to_addr_list = ['[email protected]'], # Addreses list
subject = 'Euromilhões - {0}'.format(key['date']),
message = "{0}\r\n{1}".format(head_message,''.join(body_msg)),
login = '[email protected]', # Email Address
password = 'mypassword') # Email Password
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment