Skip to content

Commit ec1ce64

Browse files
committed
initial commit
0 parents  commit ec1ce64

11 files changed

+190875
-0
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Random Hadith
2+
3+
##### A simple web application for generating a random Hadith. Will be adding more and more Ahadith as time goes.
4+
5+
Sources for this project include [sunnah.com](https://sunnah.com/).

external-link.svg

+1
Loading

hadithAPI/bukhari.json

+68,071
Large diffs are not rendered by default.

hadithAPI/index.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from flask import Flask, jsonify
2+
import random
3+
import json
4+
5+
# creating a Flask app
6+
app = Flask(__name__)
7+
8+
@app.route('/', methods = ["GET"])
9+
def home():
10+
return(
11+
'''
12+
<h1>hadith api</h1>
13+
<h2>get random</h2>
14+
<a href="/bukhari">sahih al-bukhari </a>
15+
<br>
16+
<a href="/muslim">sahih muslim</a>
17+
<h2>get custom</h2>
18+
<p>bukhari limit 1-7563
19+
<br>
20+
muslim limit 1-3032
21+
</p>
22+
<a href="/bukhari/1">/bukhari/1</a>
23+
<br>
24+
<a href="/muslim/1">/muslim/1</a>
25+
'''
26+
)
27+
28+
@app.route('/muslim/')
29+
@app.route('/muslim/<int:id>', methods = ['GET'])
30+
def muslim(id=None):
31+
if id is None:
32+
id = random.randint(1, 3034)
33+
if(id):
34+
id = id-1
35+
with open('muslim.json') as f:
36+
data = json.load(f)
37+
d = data['hadith'][id]
38+
return jsonify({'data': d})
39+
40+
@app.route('/bukhari/')
41+
@app.route('/bukhari/<int:id>', methods = ['GET'])
42+
def bukhari(id=None):
43+
if id is None:
44+
id = random.randint(1, 7564)
45+
if(id):
46+
id = id-1
47+
with open('bukhari.json') as f:
48+
data = json.load(f)
49+
d = data['hadith'][id]
50+
return jsonify({'data': d})
51+
52+
if __name__ == "__main__":
53+
app.run()

hadithAPI/muslim.json

+27,301
Large diffs are not rendered by default.

hadithAPI/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Flask==2.1.2

hadithAPI/vercel.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"version": 2,
3+
"builds": [
4+
{
5+
"src": "./index.py",
6+
"use": "@vercel/python"
7+
}
8+
],
9+
"routes": [
10+
{
11+
"src": "/(.*)",
12+
"dest": "/"
13+
}
14+
]
15+
}

scrapeHadith/.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"python.formatting.provider": "autopep8"
3+
}

scrapeHadith/bukhari.json

+68,071
Large diffs are not rendered by default.

scrapeHadith/main.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import json
2+
import requests
3+
from bs4 import BeautifulSoup
4+
5+
# TODO
6+
# - scrape number of hadiths in a book
7+
8+
def write_json(new_data, filename='muslim.json'):
9+
with open(filename,'r+') as file:
10+
file_data = json.load(file)
11+
file_data["hadith"].append(new_data)
12+
file.seek(0)
13+
json.dump(file_data, file, indent = 4)
14+
15+
for i in range(1, 3034):
16+
obj = {}
17+
# hadith to scrape
18+
hname = "muslim" # bukhari, muslim, nasai, abudawud, tirmidhi, ibnmajah
19+
url = requests.get(f'https://sunnah.com/{hname}:{i}').text
20+
# source = url.text
21+
soup = BeautifulSoup(url, 'lxml')
22+
23+
for hadith in soup.find_all('div', class_='mainContainer'):
24+
25+
# Book Name
26+
try: bookName = hadith.find('div', class_='book_page_english_name').text
27+
except: bookName = 'null'
28+
29+
# Chapter Name
30+
try: chapterName = hadith.find('div', class_='englishchapter').text
31+
except: chapterName = 'null'
32+
33+
# Narrated By
34+
try: by = hadith.find("div", class_='hadith_narrated').text
35+
except: by = "null"
36+
37+
# Hadith text
38+
text = hadith.find("div", class_='text_details').text
39+
40+
# Hadith Referance
41+
try: no = hadith.find("div", class_='hadith_reference_sticky').text
42+
except: no = "null"
43+
44+
obj["id"] = i
45+
obj["header"] = by
46+
obj["hadith_english"] = text
47+
obj["book"] = "Sahih Muslim"
48+
obj["refno"] = no
49+
obj["bookName"] = bookName
50+
obj["chapterName"] = chapterName
51+
52+
write_json(obj)
53+
print(i)

scrapeHadith/muslim.json

+27,301
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)