forked from sushant354/egazette
-
Notifications
You must be signed in to change notification settings - Fork 0
/
madhyapradesh.py
217 lines (174 loc) · 7.85 KB
/
madhyapradesh.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import datetime
import re
import os
import urllib
from basegazette import BaseGazette
import utils
class MadhyaPradesh(BaseGazette):
def __init__(self, name, storage):
BaseGazette.__init__(self, name, storage)
self.baseurl = 'http://govtpressmp.nic.in/gazette.html'
self.hostname = 'govtpressmp.nic.in'
self.extraordinary_url = '/history-gazette-extra-%d.html'
self.ordinary_urls = {\
('Part 1', '/history-gazette-1-%d.html'), \
('Part 2', '/history-gazette-2-%d.html'), \
('Part 3', '/history-gazette-3-%d.html'), \
('Part 4', '/history-gazette-4-%d.html'), \
}
self.start_date = datetime.datetime(2010, 1, 1)
def download_oneday(self, relpath, dateobj):
dls = []
self.download_extraordinary(dls, relpath, dateobj)
self.download_ordinary(dls, relpath, dateobj)
return dls
def download_ordinary(self, dls, relpath, dateobj):
for partnum, parturl in self.ordinary_urls:
parturl = urllib.basejoin(self.baseurl, parturl % dateobj.year)
response = self.download_url(parturl)
if not response or not response.webpage:
self.logger.warn('Unable to download Ordinary gazette list for Part %s, year %d', partnum, dateobj.year)
continue
d = utils.parse_webpage(response.webpage, self.parser)
if not d:
self.logger.warn('Unable to parse Ordinary gazette list for Part %s, year %d', partnum, dateobj.year)
continue
minfos = self.parse_listing_webpage(parturl, d, dateobj, partnum, 'Ordinary')
self.download_metainfos(minfos, dls, relpath)
def download_extraordinary(self, dls, relpath, dateobj):
ex_url = urllib.basejoin(self.baseurl, self.extraordinary_url % dateobj.year)
response = self.download_url(ex_url)
if not response or not response.webpage:
self.logger.warn('Unable to download Extraordinary gazette for year %d', dateobj.year)
return
d = utils.parse_webpage(response.webpage, self.parser)
if not d:
self.logger.warn('Unable to parse Extraordinary gazette list for year %d', dateobj.year)
return
if dateobj.year == 2010:
minfos = self.parse_listing_webpage(ex_url, d, dateobj, None, 'Extraordinary')
else:
minfos = self.parse_extraordinary_webpage(d, dateobj, ex_url)
self.download_metainfos(minfos, dls, relpath)
def parse_listing_webpage(self, parturl, d, dateobj, partnum, gztype):
minfos = []
for li in d.find_all('li'):
link = li.find('a')
if link == None:
continue
href = link.get('href')
if href and href.startswith('pdf'):
url = urllib.basejoin(parturl, href)
txt = utils.get_tag_contents(li)
txt = txt.strip()
if not txt:
continue
nums = re.findall('\d+', txt)
if len(nums) < 4:
self.logger.warn('Not able to parse. Ignoring %s', link)
continue
gznum = ''.join(nums[:-3])
try:
date = datetime.date(int(nums[-1]), int(nums[-2]), int(nums[-3]))
except:
self.logger.warn('Could not get date. Ignoring %s', txt)
continue
if date != dateobj:
continue
metainfo = utils.MetaInfo()
metainfo.set_gztype(gztype)
metainfo.set_date(dateobj)
metainfo.set_url(url)
metainfo['gznum'] = gznum
if partnum:
metainfo['partnum'] = partnum
minfos.append(metainfo)
return minfos
def find_result_table(self, d):
tables = d.find_all('table')
tablelist = []
for table in tables:
if table.find('table') == None:
tablelist.append((table, len(table.find_all('tr'))))
tablelist.sort(cmp = lambda x, y: cmp(x[1], y[1]), reverse = True)
if not tablelist:
return None
return tablelist[0][0]
def parse_extraordinary_webpage(self, d, dateobj, ex_url):
minfos = []
result_table = self.find_result_table(d)
if result_table == None:
self.loger.warn('Could not find result table for date %s', dateobj)
return minfos
order = None
for tr in result_table.find_all('tr'):
if not order:
order = self.find_result_order(tr)
continue
link = tr.find('a')
if link == None:
continue
metainfo = self.process_row(tr, order, dateobj)
if metainfo:
href = link.get('href')
if href:
gzurl = urllib.basejoin(ex_url, href)
metainfo.set_url(gzurl)
minfos.append(metainfo)
return minfos
def find_result_order(self, tr):
order = []
for td in tr.find_all('th'):
txt = utils.get_tag_contents(td)
if txt and re.search(u'\u0935\u093f\u0937\u092f', txt):
order.append('subject')
elif txt and re.search(u'\u0935\u093f\u092d\u093e\u0917', txt):
order.append('department')
elif txt and re.search(u'\u0926\u093f\u0928\u093e\u0902\u0915', txt):
order.append('date')
elif txt and re.search(u'\u0915\u094d\u0930\.', txt):
order.append('gznum')
else:
order.append('')
if 'subject' in order and 'date' in order and 'gznum' in order:
return order
return None
def process_row(self, tr, order, dateobj):
metainfo = utils.MetaInfo()
i = 0
for td in tr.find_all('td'):
txt = utils.get_tag_contents(td)
if txt and order[i] in ['subject', 'department', 'gznum']:
txt, n = re.subn('\s+', ' ', txt)
metainfo[order[i]] = txt.strip()
elif txt and order[i] == 'date':
nums = re.split('[./-]+', txt)
if len(nums) < 3:
self.logger.warn('Couldn\'t get date from %s for extraordinary gazette list', txt)
i += 1
continue
nums = [re.subn('\s+', '', n)[0] for n in nums]
nums = [n for n in nums if n]
d = datetime.date(int(nums[2]), int(nums[1]), int(nums[0]))
try:
metainfo.set_date(d)
except:
self.logger.warn('Could not parse date %s', txt)
i += 1
if metainfo.get_date() == dateobj:
metainfo.set_gztype('Extraordinary')
return metainfo
return None
def download_metainfos(self, minfos, dls, relpath):
for metainfo in minfos:
if not 'gztype' in metainfo or not 'gznum' in metainfo or \
not 'url' in metainfo:
self.logger.warn('Ignoring as not enough info to download: %s', metainfo)
continue
filename = '%s_%s' % (metainfo['gztype'], metainfo['gznum'])
if 'partnum' in metainfo:
filename = '%s_%s' % (filename, metainfo['partnum'])
filename, n = re.subn('\s+', '-', filename)
relurl = os.path.join(relpath, filename)
if self.save_gazette(relurl, metainfo['url'], metainfo):
dls.append(relurl)