-
Notifications
You must be signed in to change notification settings - Fork 6
/
dmpnews.py
170 lines (150 loc) · 5.66 KB
/
dmpnews.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
# -*- coding: utf-8 -*-
# encoding=utf8
import sys
from importlib import reload
reload(sys)
sys.setdefaultencoding('utf8')
import os
import json
import time
from datetime import date, timedelta
from bs4 import BeautifulSoup
import requests
newspaper_base_url = 'https://dmpnews.org'
newspaper_archive_base_url = 'https://dmpnews.org'
start_date = date(2020, 10, 31)
end_date = date.today()
delta = end_date - start_date
output_result = []
data = []
exceptions = 0
def month_converter(month):
if month == "জানুয়ারী":
month = 1
elif month == "ফেব্রুয়ারী":
month = 2
elif month == "মার্চ":
month = 3
elif month == "এপ্রিল":
month = 4
elif month == "মে":
month = 5
elif month == "জুন":
month = 6
elif month == "জুলাই":
month = 7
elif month == "আগস্ট":
month = 8
elif month == "সেপ্টেম্বর":
month = 9
elif month == "অক্টোবর":
month = 10
elif month == "নভেম্বর":
month = 11
else:
month = 12
return month
def date_translator(bn_number):
en_number = ""
for letter in bn_number:
if letter == '০':
en_number += "0"
elif letter == '১':
en_number += "1"
elif letter == '২':
en_number += "2"
elif letter == '৩':
en_number += "3"
elif letter == '৪':
en_number += "4"
elif letter == '৫':
en_number += "5"
elif letter == '৬':
en_number += "6"
elif letter == '৭':
en_number += "7"
elif letter == '৮':
en_number += "8"
elif letter == '৯':
en_number += "9"
return en_number
for i in range(delta.days + 1):
date_str = start_date + timedelta(days=i)
print(date_str)
with open("log.txt", 'a') as file:
file.write(str(date_str) + '\n')
for i in range(1, 10):
url = newspaper_archive_base_url + "/" + str(date_str.year) + "/" + str(date_str.month) + "/" + str(
date_str.day) + "/page/" + str(i)
try:
print(url)
archive_soup = requests.get(url)
except:
print("No response for links in archive,trying to reconnect")
time.sleep(2)
continue
soup = BeautifulSoup(archive_soup.content, "html.parser")
all_links = soup.find_all("a")
page_links_length = len(all_links)
if page_links_length == 0:
break
else:
for link in all_links:
try:
article_url = link.get('href')
link_separator = article_url.split('/')
except:
continue
if len(link_separator) != 5 or "dmpnews.org" not in link_separator[2]:
continue
output_file_name = '{}.txt'.format(link_separator[3])
try:
article_data = requests.get(article_url).text
except:
print("No response for content in link,trying to reconnect")
time.sleep(2)
continue
article_soup = BeautifulSoup(article_data, "html.parser")
print(article_url)
try:
date_published = article_soup.find("time", {"itemprop": "datePublished"}).get_text().strip()
except Exception as e:
continue
try:
article_title_text = article_soup.find("title").get_text().split("|")[0].strip()
except:
article_title_text = ""
try:
article_body_text = article_soup.find("div", {"class": "entry-content"}).get_text().replace(
"printশেয়ার করুনTweetEmail", "").strip()
except:
article_body_text = ""
try:
article_body_text = article_body_text.replace("ডিএমপি নিউজ রিপোর্ট:", "").strip()
except:
pass
try:
article_body_text = article_body_text.replace("ডিএমপি নিউজঃ", "").strip()
except:
pass
try:
article_body_text = article_body_text.replace("ডিএমপি নিউজ:", "").strip()
except:
pass
try:
article_body_text = article_body_text.replace("Categories:", "").strip()
except:
pass
try:
article_body_text = article_body_text.replace("ডিএমপি নিউজ রিপোর্ট:", "").strip()
except:
pass
data = "<article>\n"
data += "<title>" + article_title_text + "</title>\n"
data += "<date>" + date_published + "</date>\n"
data += "<text>" + "\n" + article_body_text + "\n" + "</text>\n"
data += "</article>"
with open('Raw/' + output_file_name, 'w') as file:
file.write(str(article_url) + '\n' + str(article_data))
with open('Data/' + output_file_name, 'w') as file:
file.write(data + '\n\n')