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

3 | 4 |

5 | 6 | It's an easy to use API to get today's (and tomorrow!) prayer time for your next project! (based on **Muslim Pro**) 7 | Written in Python using _Flask, Beautiful Soup, and duckduckgo_search_. 8 | ### Example Response: 9 | ```json 10 | { 11 | "city": "Mecca", 12 | "date": "25 March 2024", 13 | "today": { 14 | "Fajr": "05:04", 15 | "Sunrise": "06:20", 16 | "Dhuhr": "12:27", 17 | "Asr": "15:52", 18 | "Maghrib": "18:34", 19 | "Isha'a": "20:04" 20 | }, 21 | "tomorrow": { 22 | "Date": "Fri 26 Mar", 23 | "Fajr": "05:03", 24 | "Sunrise": "06:19", 25 | "Dhuhr": "12:27", 26 | "Asr": "15:52", 27 | "Maghrib": "18:34", 28 | "Isha'a": "20:04" 29 | } 30 | } 31 | ``` 32 | ## Get Started 33 | To use the API, you can simply fetch the data using the public API in 34 | https://dailyprayer.abdulrcs.repl.co/api/ (City Name) 35 | 36 | > note: currently the `dailyprayer.abdulrcs.repl.co/api/` doesn't work since Repl.it has migrated it's link to a new one 37 | > but you can always still fork the repo, spin up the main.py yourself, and self-host the Daily Prayer Time API yourself :) 38 | 39 | 40 | ## Here's an example 😉 41 | ### Print the Data in Python: 42 | ```python 43 | import requests 44 | import json 45 | 46 | url = "https://dailyprayer.abdulrcs.repl.co/api/singapore" 47 | response = requests.get(url) 48 | data = response.json() 49 | print(data['city']) 50 | print(data['date']) 51 | for prayer in data["today"]: 52 | print(prayer + ": " + data["today"][prayer]) 53 | # If you want to request for tomorrow prayer's time: 54 | # for prayer in data["tomorrow"]: 55 | # print(prayer + ": " + data["tomorrow"][prayer]) 56 | ``` 57 | 58 | ### Print the Data in Node.js: 59 | ```javascript 60 | const https = require('https'); 61 | let url = "https://dailyprayer.abdulrcs.repl.co/api/singapore"; 62 | 63 | https.get(url,(res) => { 64 | let body = ""; 65 | 66 | res.on("data", (chunk) => { 67 | body += chunk; 68 | }); 69 | 70 | res.on("end", () => { 71 | try { 72 | let json = JSON.parse(body); 73 | console.log(json["city"]) 74 | console.log(json["date"]) 75 | for(prayer in json["today"]) 76 | console.log(prayer + ": " + json["today"][prayer]) 77 | } catch (error) { 78 | console.error(error.message); 79 | }; 80 | }); 81 | 82 | }).on("error", (error) => { 83 | console.error(error.message); 84 | }); 85 | ``` 86 | ### Output: 87 | ``` 88 | Singapore 89 | 17 January 2024 90 | Fajr: 05:52 91 | Sunrise: 07:14 92 | Dhuhr: 13:17 93 | Asr: 16:40 94 | Maghrib: 19:17 95 | Isha'a: 20:31 96 | ``` 97 | --------------------------------------------------------------------------------