-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.py
342 lines (245 loc) · 10.9 KB
/
app.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import datetime
from flask import Flask,jsonify, Response, request
from flask_cors import CORS, cross_origin
import re
import urllib.request
from bs4 import BeautifulSoup
from xml.etree import ElementTree as ET
import os
import json
def load_html(search_date=None):
url1 = "https://loteriasdominicanas.com/"
# esta Segunda Url es porque en la Pag. principal no aparecen las Loterias Anguila
url2 = "https://loteriasdominicanas.com/anguila"
# Agregar el parámetro date a la URL si existe
if search_date:
url1 += f"?date={search_date}"
url2 += f"?date={search_date}"
# Crea una lista para almacenar los elementos de ambos soups
games_blocks = []
try:
html1 = urllib.request.urlopen(url1).read()
html2 = urllib.request.urlopen(url2).read()
soup1 = BeautifulSoup(html1, "html.parser")
soup2 = BeautifulSoup(html2, "html.parser")
# Encuentra los elementos deseados del soup y agrégalos a la lista
blocks1 = soup1.find_all("div", class_="game-block")
games_blocks.extend(blocks1)
# Encuentra los elementos del soup y agrégalos a la lista
blocks2 = soup2.find_all("div", class_="game-block")
games_blocks.extend(blocks2)
except:
return []
return games_blocks
def load_html_name(search_name,search_date=None):
url1 = f"https://loteriasdominicanas.com/{search_name}"
# Agregar el parámetro date a la URL si existe
if search_date:
url1 += f"?date={search_date}"
# Crea una lista para almacenar los elementos de ambos soups
games_blocks = []
try:
html1 = urllib.request.urlopen(url1).read()
soup1 = BeautifulSoup(html1, "html.parser")
# Encuentra los elementos deseados del soup y agrégalos a la lista
blocks1 = soup1.find_all("div", class_="game-block")
games_blocks.extend(blocks1)
except:
return []
return games_blocks
def scraping(search_date=None, search_lotery=None):
data = []
loteries_parser = []
# Cargar JSON en un Archivo
with open('lottery.json', 'r', encoding='utf-8') as file:
json_data = file.read()
data = json.loads(json_data)
if search_lotery:
data = [item for item in data if search_lotery.lower() in item["name"].lower()]
if len(data) == 0:
return data
# Load HTML
games_blocks = load_html(search_date)
for game_block in games_blocks:
block = {}
title = game_block.find("a", "game-title").getText().strip().lower()
filtered_data = [item for item in data if item["name"].lower() == title]
if len(filtered_data) == 0:
continue
pather_score = game_block.find_all("span", "score")
pather_date = game_block.find("div", "session-date").getText().strip()
score = "-".join(span.text.strip() for span in pather_score)
block['id'] = filtered_data[0]["id"]
block['name'] = filtered_data[0]["name"]
block['date'] = pather_date
block['number'] = score
loteries_parser.append(block)
return sorted(loteries_parser, key=lambda k:k["id"])
def scrapingByName(search_name,search_date=None, search_lotery=None):
data = []
loteries_parser = []
# Cargar JSON en un Archivo
with open('lottery.json', 'r', encoding='utf-8') as file:
json_data = file.read()
data = json.loads(json_data)
if search_lotery:
data = [item for item in data if search_lotery.lower() in item["name"].lower()]
if len(data) == 0:
return data
# Load HTML
games_blocks = load_html_name(search_name,search_date)
for game_block in games_blocks:
block = {}
title = game_block.find("a", "game-title").getText().strip().lower()
filtered_data = [item for item in data if item["name"].lower() == title]
if len(filtered_data) == 0:
continue
pather_score = game_block.find_all("span", "score")
pather_date = game_block.find("div", "session-date").getText().strip()
score = "-".join(span.text.strip() for span in pather_score)
block['id'] = filtered_data[0]["id"]
block['name'] = filtered_data[0]["name"]
block['date'] = pather_date
block['number'] = score
loteries_parser.append(block)
return sorted(loteries_parser, key=lambda k:k["id"])
def JsonUFT8(data=None):
json_string = json.dumps(data,ensure_ascii = False)
return Response(json_string, content_type='application/json; charset=utf-8')
app = Flask(__name__)
CORS(app)
port = int(os.environ.get("PORT", 5000))
@app.route("/", methods=['GET'])
def search_lotery():
search_date = request.args.get('date', datetime.datetime.now().strftime("%d-%m-%Y"))
data = scraping(search_date)
return JsonUFT8(data)
@app.route("/search", methods=['GET'])
def search_lotery_by_name():
search_query = request.args.get('name', None)
search_date = request.args.get('date', datetime.datetime.now().strftime("%d-%m-%Y"))
if not search_query:
return jsonify({"error": "Missing 'name' parameter"}), 400
data = scraping(search_date, search_query)
return JsonUFT8(data)
@app.route("/loteria-gana-mas", methods=['GET'])
def search_lotery1():
search_date = request.args.get('date', datetime.datetime.now().strftime("%d-%m-%Y"))
data = scrapingByName("loteria-nacional/gana-mas",search_date,"Gana Más")
return JsonUFT8(data)
@app.route("/loteria-primera", methods=['GET'])
def search_lotery2():
search_query = request.args.get('name', "primera")
search_date = request.args.get('date', datetime.datetime.now().strftime("%d-%m-%Y"))
data = scrapingByName("la-primera",search_date, search_query)
return JsonUFT8(data)
@app.route("/loteria-primera-12am", methods=['GET'])
def search_lotery3():
search_date = request.args.get('date', datetime.datetime.now().strftime("%d-%m-%Y"))
data = scrapingByName("la-primera/quiniela-medio-dia",search_date, "la primera Día")
return JsonUFT8(data)
@app.route("/loteria-primera-noche", methods=['GET'])
def search_lotery4():
search_date = request.args.get('date', datetime.datetime.now().strftime("%d-%m-%Y"))
data = scrapingByName("la-primera/quiniela-noche",search_date, "Primera Noche")
return JsonUFT8(data)
@app.route("/loteria-la-suerte", methods=['GET'])
def search_lotery6():
search_query = request.args.get('name', "La Suerte")
search_date = request.args.get('date', datetime.datetime.now().strftime("%d-%m-%Y"))
data = scrapingByName("la-suerte-dominicana",search_date, search_query)
return JsonUFT8(data)
@app.route("/loteria-la-suerte-12am", methods=['GET'])
def search_lotery7():
search_date = request.args.get('date', datetime.datetime.now().strftime("%d-%m-%Y"))
data = scrapingByName("la-suerte-dominicana/quiniela",search_date, "La Suerte 12:30")
return JsonUFT8(data)
@app.route("/loteria-la-suerte-tarde", methods=['GET'])
def search_lotery8():
search_date = request.args.get('date', datetime.datetime.now().strftime("%d-%m-%Y"))
data = scrapingByName("la-suerte-dominicana/quiniela-tarde",search_date, "La Suerte 18:00")
return JsonUFT8(data)
@app.route("/loteria-lotedom", methods=['GET'])
def search_lotery9():
search_date = request.args.get('date', datetime.datetime.now().strftime("%d-%m-%Y"))
data = scrapingByName("lotedom",search_date, "Quiniela LoteDom")
return JsonUFT8(data)
@app.route("/loteria-anguila", methods=['GET'])
def search_lotery10():
search_query = request.args.get('name', "Anguila")
search_date = request.args.get('date', datetime.datetime.now().strftime("%d-%m-%Y"))
data = scrapingByName("anguila",search_date, search_query)
return JsonUFT8(data)
@app.route("/loteria-anguila-10am", methods=['GET'])
def search_loteryAng():
search_date = request.args.get('date', datetime.datetime.now().strftime("%d-%m-%Y"))
data = scrapingByName("anguila/anguila-manana",search_date, "Anguila Mañana")
return JsonUFT8(data)
@app.route("/loteria-anguila-12am", methods=['GET'])
def search_loteryAng12():
search_date = request.args.get('date', datetime.datetime.now().strftime("%d-%m-%Y"))
data = scrapingByName("anguila/anguila-medio-dia",search_date, "Anguila Medio Día")
return JsonUFT8(data)
@app.route("/loteria-anguila-6pm", methods=['GET'])
def search_loteryAng6pm():
search_date = request.args.get('date', datetime.datetime.now().strftime("%d-%m-%Y"))
data = scrapingByName("anguila/anguila-tarde",search_date, "Anguila Tarde")
return JsonUFT8(data)
@app.route("/loteria-anguila-9pm", methods=['GET'])
def search_loteryAng9pm():
search_date = request.args.get('date', datetime.datetime.now().strftime("%d-%m-%Y"))
data = scrapingByName("anguila/anguila-noche",search_date, "Anguila Noche")
return JsonUFT8(data)
@app.route("/loterias-nacionales", methods=['GET'])
def search_lotery11():
search_query = request.args.get('name', None)
search_date = request.args.get('date', datetime.datetime.now().strftime("%d-%m-%Y"))
data = scrapingByName("loteria-nacional",search_date, search_query)
return JsonUFT8(data)
@app.route("/loteria-nacional", methods=['GET'])
def search_lotery12():
search_date = request.args.get('date', datetime.datetime.now().strftime("%d-%m-%Y"))
data = scrapingByName("loteria-nacional/quiniela",search_date, "Lotería Nacional")
return JsonUFT8(data)
@app.route("/loteria-leidsa", methods=['GET'])
def search_lotery13():
search_date = request.args.get('date', datetime.datetime.now().strftime("%d-%m-%Y"))
data = scrapingByName("leidsa", search_date, "Quiniela Leidsa")
return JsonUFT8(data)
@app.route("/loteria-real", methods=['GET'])
def search_lotery14():
search_date = request.args.get('date', datetime.datetime.now().strftime("%d-%m-%Y"))
data = scrapingByName("loto-real", search_date, "Quiniela Real")
return JsonUFT8(data)
@app.route("/loteria-loteka", methods=['GET'])
def search_lotery15():
search_date = request.args.get('date', datetime.datetime.now().strftime("%d-%m-%Y"))
data = scrapingByName("loteka", search_date, "Quiniela Loteka")
return JsonUFT8(data)
@app.route("/loteria-americana", methods=['GET'])
def search_lotery16():
search_query = request.args.get('name', None)
search_date = request.args.get('date', datetime.datetime.now().strftime("%d-%m-%Y"))
data = scrapingByName("americanas",search_date, search_query)
return JsonUFT8(data)
@app.route("/loteria-florida-tarde", methods=['GET'])
def search_lotery17():
search_date = request.args.get('date', datetime.datetime.now().strftime("%d-%m-%Y"))
data = scrapingByName("americanas/florida-tarde",search_date, "Florida Día")
return JsonUFT8(data)
@app.route("/loteria-florida-noche", methods=['GET'])
def search_lotery18():
search_date = request.args.get('date', datetime.datetime.now().strftime("%d-%m-%Y"))
data = scrapingByName("americanas/florida-noche",search_date, "Florida Noche")
return JsonUFT8(data)
@app.route("/loteria-new-york-12am", methods=['GET'])
def search_lotery19():
search_date = request.args.get('date', datetime.datetime.now().strftime("%d-%m-%Y"))
data = scrapingByName("americanas/new-york-medio-dia",search_date, "New York Tarde")
return JsonUFT8(data)
@app.route("/loteria-new-york-noche", methods=['GET'])
def search_lotery20():
search_date = request.args.get('date', datetime.datetime.now().strftime("%d-%m-%Y"))
data = scrapingByName("americanas/new-york-noche",search_date, "New York Noche")
return JsonUFT8(data)
app.run(port=port)