-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlotto.py
executable file
·128 lines (97 loc) · 3.08 KB
/
lotto.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
#!/usr/bin/env python3
"""A simple script does two things:
1. Crawls lottery results from ketqua1.net
2. Checks daily lottery as arguments win or not
(2 last digit on lottery ticket).
If there is no argument then:
Print all the winning numbers.
Usage:
$ python3 lotto.py [NUMBER1] [NUMBER2] [...]
"""
import argparse
from typing import Any, List, Tuple
from bs4 import BeautifulSoup
import requests
def get_tree() -> Any:
sess = requests.Session()
with sess:
resp = sess.get('https://ketqua1.net/', timeout=5)
return BeautifulSoup(resp.text, 'html.parser')
def find_daily_drawings(tree: Any) -> Tuple[str, List[str]]:
"""
Get the results of daily lottery
- Crawl the drawing's day-date.
- List all the div tags containing results.
- With each div tag, collects all result's IDs.
e.g. ['rs_8_0', 'rs_0_0', 'rs_1_0', 'rs_2_0',... ]
- With each result's ID, scrape the drawings.
:param tree: BeautifulSoup4 tree
:rtype Tuple:
"""
date = tree.find("tr", class_="title_row").text.strip("\n")
div_tags = tree.find("table", id="result_tab_mb").find_all("div")
rs_id = [div.get('id') for div in div_tags if div.get('id')]
daily_drawings = [tree.find(id=i).text for i in rs_id]
del daily_drawings[0] # Delete the jackpot-characters
return date, daily_drawings
def show_result(date: str, drawings: List[str]) -> str:
result = """{}
JACKPOT: {}
1: {}
2: {} - {}
3: {} - {} - {}
{} - {} - {}
4: {} - {} - {} - {}
5: {} - {} - {}
{} - {} - {}
6: {} - {} - {}
7: {} - {} - {} - {}
""".format(date, *drawings)
return result
def check_user_ticket(tree: Any, user_numbers: List[str]) -> None:
"""Checks whether user win or not
:param tree: BeautifulSoup4 tree
:param user_numbers: user ticket numbers
:rtype None:
"""
date, drawings = find_daily_drawings(tree)
if not user_numbers:
# Without arguments
print(show_result(date, drawings))
else:
# With arguments
lotto = [number[-2:] for number in drawings]
win = []
for number in user_numbers:
if number[-2:] in lotto:
win.append(number[-2:])
if not win:
print("Good Luck Next Time!")
else:
print("Congratulation! You win: " + " - ".join(win))
def solve(tree: Any, user: List[str]) -> None:
"""Function `solve` for `test` purpose
Return the same value in both `solve` and `check_user_ticket`
:param tree: BeautifulSoup4 tree
:param user: user ticket numbers
:rtype None:
"""
return check_user_ticket(tree, user)
def main() -> None:
# Parse arguments
parser = argparse.ArgumentParser(
description="Check numbered tickets lotto"
)
parser.add_argument(
"lotto_numbers",
type=str,
nargs="*",
help="Numbers on your lottery ticket."
)
args = parser.parse_args()
user = args.lotto_numbers
# Show lottery results
tree = get_tree()
solve(tree, user)
if __name__ == "__main__":
main()