Skip to content

Commit efeeed0

Browse files
authored
🔨 update API to use AsyncDDGS
1 parent a060507 commit efeeed0

File tree

1 file changed

+47
-18
lines changed

1 file changed

+47
-18
lines changed

main.py

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,87 @@
22
from flask import Flask, jsonify
33
from flask_cors import CORS, cross_origin
44
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
610
from bs4 import BeautifulSoup
711
import dateparser
12+
813
app = Flask('')
914
cors = CORS(app)
1015
app.config['CORS_HEADERS'] = 'Content-Type'
1116
app.config['JSON_SORT_KEYS'] = False
1217

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('/')
1426
def home():
15-
return "I'm alive"
27+
return "I'm alive"
28+
1629

1730
@app.route('/api/<string:s>', methods=['GET'])
1831
@cross_origin(origin='*')
1932
def prayer(s):
33+
loop = asyncio.new_event_loop()
34+
asyncio.set_event_loop(loop)
35+
2036
query = str(s + " prayer time site:muslimpro.com")
2137
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:
2443
url = urls[0]['href']
2544
response = requests.get(url)
2645
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"})
2948
data["city"] = city.get_text()
3049
data["date"] = dates.get_text()
3150
data["today"] = {}
3251
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):
3656
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+
3860
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):
4165
data["tomorrow"][x] = y.get_text()
42-
except :
66+
except:
4367
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)
4569
response = requests.get(url)
4670
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):
4974
data["tomorrow"][x] = y.get_text()
5075
except Exception as e:
5176
print(e)
5277
data["Error"] = "Result Not Found"
78+
79+
loop.close()
5380
return jsonify(data)
5481

82+
5583
def run():
56-
app.run(host='0.0.0.0',port=7000)
84+
app.run(host='0.0.0.0', port=7000)
85+
5786

5887
t = Thread(target=run)
5988
t.start()

0 commit comments

Comments
 (0)