|
2 | 2 | from flask import Flask, jsonify
|
3 | 3 | from flask_cors import CORS, cross_origin
|
4 | 4 | from threading import Thread
|
5 |
| -from duckduckgo_search import ddg |
| 5 | +import os |
| 6 | +import asyncio |
| 7 | + |
| 8 | +os.system('pip install duckduckgo-search') |
| 9 | +from duckduckgo_search import AsyncDDGS |
6 | 10 | from bs4 import BeautifulSoup
|
7 | 11 | import dateparser
|
| 12 | + |
8 | 13 | app = Flask('')
|
9 | 14 | cors = CORS(app)
|
10 | 15 | app.config['CORS_HEADERS'] = 'Content-Type'
|
11 | 16 | app.config['JSON_SORT_KEYS'] = False
|
12 | 17 |
|
13 |
| -@app.route('/') |
| 18 | + |
| 19 | +async def aget_results(query): |
| 20 | + addgs = AsyncDDGS(proxies=None) |
| 21 | + results = await addgs.text(query, max_results=2) |
| 22 | + return results |
| 23 | + |
| 24 | + |
| 25 | +@app.route('/') |
14 | 26 | def home():
|
15 |
| - return "I'm alive" |
| 27 | + return "I'm alive" |
| 28 | + |
16 | 29 |
|
17 | 30 | @app.route('/api/<string:s>', methods=['GET'])
|
18 | 31 | @cross_origin(origin='*')
|
19 | 32 | def prayer(s):
|
| 33 | + loop = asyncio.new_event_loop() |
| 34 | + asyncio.set_event_loop(loop) |
| 35 | + |
20 | 36 | query = str(s + " prayer time site:muslimpro.com")
|
21 | 37 | data = {}
|
22 |
| - urls = ddg(query, max_results=1) |
23 |
| - try : |
| 38 | + |
| 39 | + # Run the asynchronous function in the event loop |
| 40 | + urls = loop.run_until_complete(aget_results(query)) |
| 41 | + |
| 42 | + try: |
24 | 43 | url = urls[0]['href']
|
25 | 44 | response = requests.get(url)
|
26 | 45 | soup = BeautifulSoup(response.content, "html.parser")
|
27 |
| - city = soup.find("p", attrs ={"class": "location"}) |
28 |
| - dates = soup.find("div", attrs ={"class": "prayer-daily-title-location"}) |
| 46 | + city = soup.find("p", attrs={"class": "location"}) |
| 47 | + dates = soup.find("div", attrs={"class": "prayer-daily-title-location"}) |
29 | 48 | data["city"] = city.get_text()
|
30 | 49 | data["date"] = dates.get_text()
|
31 | 50 | data["today"] = {}
|
32 | 51 | data["tomorrow"] = {}
|
33 |
| - waktu = soup.find_all("span", attrs ={"class": "waktu-solat"}) |
34 |
| - jam = soup.find_all("span", attrs ={"class": "jam-solat"}) |
35 |
| - for x,y in zip(waktu,jam): |
| 52 | + waktu = soup.find_all("span", attrs={"class": "waktu-solat"}) |
| 53 | + jam = soup.find_all("span", attrs={"class": "jam-solat"}) |
| 54 | + |
| 55 | + for x, y in zip(waktu, jam): |
36 | 56 | data["today"][x.get_text()] = y.get_text()
|
37 |
| - names = ["Fajr", "Sunrise", "Dhuhr", "Asr", "Maghrib", "Isha'a"] |
| 57 | + |
| 58 | + names = ["Fajr", "Sunrise", "Dhuhr", "Asr", "Maghrib", "Ishaa"] |
| 59 | + |
38 | 60 | try:
|
39 |
| - tomorrow = soup.find("tr", attrs={"class": "active"}).find_next("tr").find_all("td", attrs={"class": "prayertime"}) |
40 |
| - for x,y in zip(names,tomorrow): |
| 61 | + tomorrow = soup.find("tr", attrs={ |
| 62 | + "class": "active" |
| 63 | + }).find_next("tr").find_all("td", attrs={"class": "prayertime"}) |
| 64 | + for x, y in zip(names, tomorrow): |
41 | 65 | data["tomorrow"][x] = y.get_text()
|
42 |
| - except : |
| 66 | + except: |
43 | 67 | month = str(dateparser.parse(data["date"]))[5:7]
|
44 |
| - url = url + '?date=2021-' + str(int(month)+1) |
| 68 | + url = url + '?date=2021-' + str(int(month) + 1) |
45 | 69 | response = requests.get(url)
|
46 | 70 | soup = BeautifulSoup(response.content, "html.parser")
|
47 |
| - tomorrow = soup.find_all("tr")[1].find_all("td", attrs={"class": "prayertime"}) |
48 |
| - for x,y in zip(names,tomorrow): |
| 71 | + tomorrow = soup.find_all("tr")[1].find_all("td", |
| 72 | + attrs={"class": "prayertime"}) |
| 73 | + for x, y in zip(names, tomorrow): |
49 | 74 | data["tomorrow"][x] = y.get_text()
|
50 | 75 | except Exception as e:
|
51 | 76 | print(e)
|
52 | 77 | data["Error"] = "Result Not Found"
|
| 78 | + |
| 79 | + loop.close() |
53 | 80 | return jsonify(data)
|
54 | 81 |
|
| 82 | + |
55 | 83 | def run():
|
56 |
| - app.run(host='0.0.0.0',port=7000) |
| 84 | + app.run(host='0.0.0.0', port=7000) |
| 85 | + |
57 | 86 |
|
58 | 87 | t = Thread(target=run)
|
59 | 88 | t.start()
|
0 commit comments