-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rabibasariya.py
192 lines (146 loc) · 6.35 KB
/
rabibasariya.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
import requests
from bs4 import BeautifulSoup
import json
import datetime
import time
import os
import re
DOMAIN = "https://www.anandabazar.com"
HEADERS = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
}
class Story:
def __init__(self,url):
self.name = ""
self.author = ""
self.image = ""
self.text = ""
self.url = url
self.soup = ""
self.fetch_story()
self.get_name()
self.get_author()
self.get_image()
self.get_text()
self.write_metadata()
self.write_story()
def fetch_story(self):
# Send the request with the user agent header
response = requests.get(self.url, headers=HEADERS)
# Parse the HTML with Beautiful Soup
self.soup = BeautifulSoup(response.text, 'html.parser')
def get_name(self):
name_div = self.soup.find('div',{'class': 'articletbox mt-32'})
name_tag = name_div.find('h1')
self.name = name_tag.text.strip()
# define regular expression to match English characters
english_pattern = re.compile("[a-zA-Z]")
# remove English characters from the text
self.name = re.sub(english_pattern, "", self.name)
self.name = self.name.replace(':','').strip()
self.name = self.name.replace("short story:-", "").strip()
self.name = self.name.replace("Short Story: ", "").strip()
self.name = self.name.replace("short story: ", '').strip()
self.name = self.name.replace("A shrot story: ", "").strip()
self.name = self.name.replace("A short story: ", "").strip()
def get_author(self):
try:
author_div = self.soup.find('div',{'class': 'editorbox mt-24'})
author_tag = author_div.find('h5')
self.author = author_tag.text.strip()
except:
self.author = "Unknown"
def get_image(self):
image_div = self.soup.find('div',{'class': 'leadimgbox mt-24'})
img_tag = image_div.find('img')
self.image = img_tag['src']
def get_text(self):
story_text = ""
content_div = self.soup.find('div', {'class': 'acontentbox'})
# Find all the <p> tags under the <div> and print their contents
for p_tag in content_div.find_all('p'):
story_text += p_tag.text + '\n\n'
self.text = story_text.strip()
def write_metadata(self):
# Create a datetime object for the current date and time
now = datetime.datetime.now()
# Convert the datetime object to a string with the desired format
date_time_str = now.strftime("%Y-%m-%d %H:%M:%S")
date_str = now.strftime("%d-%m-%Y")
metadata = {}
metadata['url'] = self.url
metadata['author'] = self.author
metadata['crawl_date'] = date_time_str
output_file_path = f'./metadata/rabibasariya/{self.name}-{self.author}.json'
output_file_path = output_file_path.replace(' ', '-')
with open(output_file_path, 'w', encoding='utf-8') as outfile:
json.dump(metadata, outfile, ensure_ascii=False)
def write_story(self):
print(f'{self.name}: Writing story')
# download image
now = datetime.datetime.now()
date_str = now.strftime("%d-%m-%Y")
image_outfile = f"metadata/images/rabibasariya/{self.name}-{self.author}.jpg"
image_outfile = image_outfile.replace(' ', '-')
response = requests.get(self.image)
if response.status_code == 200:
# Open a file in binary mode and write the response content to it
with open(image_outfile, 'wb') as f:
f.write(response.content)
print(f'{self.name}: Image downloaded')
else:
print('Failed to download image')
image_section = f'<div align=center> <img src="../../{image_outfile}" align="center"></div>'
name_section = f'<h1 align=center>{self.name}</h1>'
author_section = f'<h2 align=center>{self.author}</h2>'
story_section = self.text
markdown_content = f'{image_section}<br>{name_section}\n{author_section}<br>{story_section}'
markdown_outfile = f'./stories/rabibasariya/{self.name}-{self.author}.md'
markdown_outfile = markdown_outfile.replace(" ", '-')
# if not already scraped
if not os.path.exists(markdown_outfile):
with open(markdown_outfile, 'w') as f:
f.write(markdown_content)
## append to README
with open ('rabibasariya', "a") as f:
f.write(f"\n[ {self.name} - {self.author} ]({markdown_outfile})")
print(f'{self.name}: Appending to README')
###########################################
class Homepage:
def __init__(self,page):
self.home = "https://www.anandabazar.com/rabibashoriyo/"
self.page = page
self.url = f'{self.home}page-{self.page}'
self.stories = []
self.fetch_entries()
def fetch_entries(self):
response = requests.get(self.url, headers=HEADERS)
soup = BeautifulSoup(response.text, 'html.parser')
a_tags = soup.find_all('a')
for a_tag in a_tags:
story_url = a_tag.get('href')
# matching the keyword
if "short-story" in story_url:
if story_url not in self.stories:
self.stories.append(story_url)
###################################### manual add ###########################
def manual_crawl(url):
story = Story(url)
# print(story.author)
# print(story.text)
# print(story.image)
##################################### CI add ####################################
def auto_crawl(start,end):
for i in range(start,end):
print("############# " + str(i) + "\n")
rabibasariya = Homepage(str(i))
stories_url = rabibasariya.stories
for story_url in stories_url:
url = f"{DOMAIN}/{story_url}"
print(f"Fetching: {url}")
story = Story(url)
time.sleep(5)
#################################### entry point ####################################
url = "https://www.anandabazar.com//rabibashoriyo/bengali-short-story-written-by-sujit-basak/cid/1377215"
# manual_crawl(url)
auto_crawl(1,2)