-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprize_bond_service.py
120 lines (107 loc) · 3.7 KB
/
prize_bond_service.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
"""
Prizebond Services
"""
import requests
from datetime import datetime
from bs4 import BeautifulSoup
from enum import Enum
class PrizeBondStatus(Enum):
"""
Prizebond status codes
"""
WON = 1
LOSE = 0
NOT_FOUND = -1
class PrizeBondService:
"""
Service used to search prizebonds
"""
url = "https://prizebond.result.pk/pb-ajax.php"
def __init__(self, year, month, draw_values=["100", "200", "750", "1500"]):
self.year = year
self.month = month
self.draw_values = draw_values
self.draws = self.get_draws(year, month, draw_values)
def _convert_draws_to_dict(self, draw_dates):
"""
Parse list of draws to dict based on year and month
Arguments:
draw_dates: list(str) i.e ['01/2022-08-01', '03/2022-09-15', '03/2021-08-13']
Returns:
parsed values i.es
{
2022: {
8: '01/2022-08-01',
9: '03/2022-09-15',
},
2021: {
8: '03/2021-08-13'
}
}
"""
dates_dict = {}
for draw_str in draw_dates:
try:
date_time_str = draw_str.split("/")[1]
draw_date = datetime.strptime(date_time_str, "%Y-%m-%d")
if draw_date.year in dates_dict:
dates_dict[draw_date.year][draw_date.month] = draw_str
else:
dates_dict[draw_date.year] = {draw_date.month: draw_str}
except:
continue
return dates_dict
def _get_draw_dates(self, draw_value):
"""
Call an API and returns all draws
Arguments:
draw_value: (str) i.e 100, 200, 750, 1500
Returns:
list of draws i.e ['01/2022-08-01', '03/2022-09-15', '03/2021-08-13']
"""
draw_results = requests.post(self.url, data={"draw": draw_value})
soup = BeautifulSoup(draw_results.text, "html.parser")
dates = [option["value"] for option in soup.find_all("option")]
return dates
def get_draws(self, year, month, draw_values):
"""
Search all draws held in a month of a year
Arguments:
year: (int) i.e 2021, 2022
month: (int) values from 1-12
draw_values: list(str) draws to search in ['200', '750']
Returns:
{
'750': '03/2022-09-15',
'200': '03/2022-09-01''
}
"""
current_draws = {}
for draw in draw_values:
draws = self._get_draw_dates(draw)
draws_dict = self._convert_draws_to_dict(draws)
if year in draws_dict and month in draws_dict[year]:
current_draws[draw] = draws_dict[year][month]
return current_draws
def _parse_prizebond_response(self, response):
"""
Parse response
"""
soup = BeautifulSoup(response.text, "html.parser")
if len(soup.find_all("tr")) >= 3:
return {"status": PrizeBondStatus.WON, "results": response.text}
return {"status": PrizeBondStatus.LOSE, "results": response.text}
def check_bonds(self, start, end, draw_name):
"""
Check prizebond
"""
if draw_name in self.draws:
pb_data = {
"number1": ", ".join(start),
"number2": ", ".join(end),
"pb_draw_detail": self.draws[draw_name],
"draw_name": draw_name,
}
response = requests.post(self.url, data=pb_data)
return self._parse_prizebond_response(response)
return {"status": PrizeBondStatus.NOT_FOUND, "results": None}