Last active
December 5, 2016 18:24
-
-
Save julianwachholz/9c29219ecd611b776afabb939d4ea4d1 to your computer and use it in GitHub Desktop.
Check if you have won with your "Millionenlos"!
This file contains 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/env python | |
# -*- encoding: utf-8 -*- | |
from __future__ import unicode_literals, print_function | |
""" | |
Usage: python millionenlos.py <symbols.txt> | |
Shows today's winning symbols from Millionenlos.ch; | |
Or shows your winnings when given a file | |
containing winning symbols for each day. | |
""" | |
import logging | |
import codecs | |
import requests | |
import sys | |
from builtins import str as text | |
from bs4 import BeautifulSoup | |
from datetime import date, datetime | |
MILLIONENLOS_URL = 'https://www.swisslos.ch/de/informationen/spiele/millionenlos/gewinnsymbole/symbole.html' | |
logger = logging.getLogger(__name__) | |
def check_date(html, day, symbols=None): | |
logger.info('Finding winning symbols for {!r}'.format(day)) | |
if symbols is None: | |
print('Winning symbols for {}'.format(day.strftime('%Y-%m-%d'))) | |
else: | |
logger.info('My symbols for this date: {}'.format(', '.join(symbols))) | |
div = html.find(**{'data-id': day.strftime('%Y%m%d')}) | |
matches = 0 | |
for winner in div.find_all('div', class_='teaser__lot'): | |
symbol = winner.find(class_='teaser__lot-symbol').img['alt'].upper() | |
amount = winner.find(class_='teaser__lot-text').text.strip().split('\n')[-1] | |
logger.info('Found symbol: {} - CHF {}'.format(symbol, amount)) | |
if symbols is None or symbol in symbols: | |
matches += 1 | |
print('{}: CHF {}'.format(symbol, amount)) | |
return matches | |
def main(file=None, verbose=False): | |
html = BeautifulSoup(requests.get(MILLIONENLOS_URL).text, 'html.parser') | |
today = datetime.now().date() | |
day_symbols = [] | |
if file is not None: | |
with codecs.open(file, 'r', encoding='utf-8') as f: | |
for line in f: | |
symbols = list(map(text.upper, map(text.strip, line.split(',')))) | |
if symbols: | |
day_symbols.append(symbols) | |
logger.info('Have symbols for the first {} days'.format(len(day_symbols))) | |
for i, symbols in enumerate(day_symbols): | |
day = date(2016, 12, i + 1) | |
if day > today: | |
continue | |
matches = check_date(html, day, symbols) | |
if not day_symbols: | |
matches = check_date(html, today) | |
print('Done, {} winning symbols.'.format(matches)) | |
if __name__ == '__main__': | |
level = logging.WARN | |
for arg in sys.argv: | |
if arg == '-v' or arg == '--verbose': | |
level = logging.INFO | |
else: | |
file = arg | |
logging.basicConfig(level=level) | |
main(file) |
This file contains 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
Symbol1, Symbol2, Symbol3 | |
Symbol4 | |
Symbol5, Symbol6 | |
etc... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment