-
Notifications
You must be signed in to change notification settings - Fork 0
/
manubot.py
520 lines (439 loc) · 14.1 KB
/
manubot.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#!/usr/bin/python
# -*- coding: utf-8 -*-
import wikipedia
import re
import pagegenerators
import catlib
import codecs
import sqlite3
gtemplates = (
u'Infobox film'.lower(),
u'Imdb title'.lower(),
u'Amg title'.lower())
gremoval_re = (
(r'\n',''),
(r'\s*=\s*','=')
)
gdictInfobox = {}
gpageData = u''
gcurrent_page = u''
def getInfobox(film):
info_box_dict = {}
site = wikipedia.getSite('en','wikipedia')
page = wikipedia.Page(site, film)
if page.isRedirectPage():
page = page.getRedirectTarget()
for template in page.templatesWithParams():
x = template[0].lower()
if x in gtemplates:
info_box_dict[x] = template[1]
#print info_box_dict
try:
#Populate the global dictionary
for i in info_box_dict[gtemplates[0]]:
#Cleanup the data
for regex,removal in gremoval_re:
r = re.compile(regex,re.UNICODE)
r.sub(removal,i)
#split
#Strip not req
split_val = i.split(u'=',1)
key = split_val[0].strip()
val = split_val[1].strip()
if val:
gdictInfobox[key] = val
except KeyError:
print "info_box_dict update failure in getInfobox()"
except IndexError:
print "Can be bug in pywiki.= not present.Wrong index in getInfobox()",i
try:
gdictInfobox['imdb_id'] = info_box_dict[gtemplates[1]]
gdictInfobox['amg_id'] = info_box_dict[gtemplates[2]]
except KeyError:
pass
print gdictInfobox
def getInterwiki(input_page,lang = 'ml',string = True):
#print type(input_page)
#print (input_page)
regex = r'\[\[%s:(.*)\]\]' % lang
r = re.compile(regex,re.UNICODE)
try:
if string:
site = wikipedia.getSite('en','wikipedia')
page = wikipedia.Page(site, input_page)
else:
page = input_page
if page.isRedirectPage():
page = page.getRedirectTarget()
for i in page.interwiki():
#print i.aslink().encode('utf-8')
m = r.search(i.aslink())
if m:
iwiki = m.group(1)
#print iwiki
return iwiki
except wikipedia.NoPage:
print 'getInterwiki: No Page to open',input_page.encode('utf-8')
return None
#for url = 0 the function returns the url links of the page,if 1 it returns the name.
def getMyLinks(stringdata,url=0):
links = []
r = re.compile(r'\[\[(.*?)\]\]',re.UNICODE)
for i in r.findall(stringdata):
try:
i = i.split(u'|')[url]
except IndexError:
i = i.split(u'|')[0]
i = i.strip()
links.append(i)
return links
#{{ubl|Harikrishnan|P. F. Mathews}}
#Christopher Nolan<br />[[Emma Thomas]]
def getItems(stringdata):
if stringdata.find('{{ubl|') != -1:
stringdata = stringdata.replace('{{ubl|','')
stringdata = stringdata.replace('}}','')
elif stringdata.find('<br />') != -1:
stringdata = stringdata.replace('<br />','|')
return [i.strip() for i in stringdata.split('|')]
def updateInfobox():
global gpageData
gpageData+=u'{{Infobox Film\n'
for i,j in gdictInfobox.iteritems():
gpageData+=u'| '
gpageData+=i
gpageData+=u'='
gpageData+=j
gpageData+=u'\n'
gpageData+=u'''}}'''
def updateFilmDetails():
global gpageData
if gdictInfobox.has_key('name'):
gpageData+= u'\n\'\'\'\'\''
gpageData+=gdictInfobox['name']
gpageData+=u'\'\'\'\'\' '
if gdictInfobox.has_key('director'):
try:
gpageData+= getMyLinks(gdictInfobox['director'],1)[0]
except IndexError:
gpageData+= gdictInfobox['director']
gpageData+=u' സംവിധാനം നിര്വഹിച്ച് '
if gdictInfobox.has_key('released'):
gpageData+=gdictInfobox['released']
gpageData+=u'പുറത്തിറങ്ങിയ '
if gdictInfobox.has_key('language'):
gpageData+=gdictInfobox['language']
gpageData+=u'ചലച്ചിത്രമാണ്.'
def updateActortable():
global gpageData
gpageData+=u'''
== അഭിനേതാക്കൾ ==
{| class="wikitable"
|-
! അഭിനേതാവ് !! കഥാപാത്രം
|-
'''
for i in getMyLinks(gdictInfobox['starring'],1):
gpageData+=u'|'
gpageData+=i
gpageData+=u'||\n'
gpageData+=u'|-\n'
gpageData+=u'''|}'''
def updateIwikis():
global gpageData
site = wikipedia.getSite('en','wikipedia')
page = wikipedia.Page(site,gcurrent_page)
gpageData+=u'[['
gpageData+=u'en:'
gpageData+=gcurrent_page
gpageData+=u']]'
for i in page.interwiki():
gpageData+=i
def update_music():
global gpageData
temp = gpageData
gpageData+=u'''
== സംഗീതം ==
'''
if gdictInfobox.has_key('music') and gdictInfobox.has_key('lyrics'):
gpageData+=u'ഇതിലെ ഗാനങ്ങൾ രചിച്ചത് '
gpageData+=gdictInfobox['lyrics']
gpageData+=u' ആണ്.ഈണം പകർന്നിരുന്നത് '
gpageData+=gdictInfobox['music']
gpageData+=u' ആണ്.'
elif gdictInfobox.has_key('music'):
gpageData+=u'ഇതിലെ ഗാനങ്ങൾക്ക് ഈണം പകർന്നിരുന്നത് '
gpageData+=gdictInfobox['music']
gpageData+=u' ആണ്.'
elif gdictInfobox.has_key('lyrics'):
gpageData+=u'ഇതിലെ ഗാനങ്ങൾ രചിച്ചത് '
gpageData+=gdictInfobox['lyrics']
gpageData+=u' ആണ്.'
else:
gpageData = temp
def update_aniyara():
global gpageData
flag = False
temp = gpageData
gpageData+=u'''
== അണിയറ പ്രവർത്തകർ ==
{| class="wikitable"
|-
! അണിയറപ്രവർത്തനം !! നിർവ്വഹിച്ചത്
|-
'''
if gdictInfobox.has_key('cinematography'):
flag = True
gpageData+=u'''
| ഛായാഗ്രഹണം || '''
gpageData+=gdictInfobox['cinematography']
gpageData+=u'\n|-'
elif gdictInfobox.has_key('editing'):
flag = True
gpageData+=u'''
| ചിത്രസംയോജനം || '''
gpageData+=gdictInfobox['editing']
gpageData+=u'\n|-'
if flag == True:
gpageData+=u'\n|}'
else:
gpageData = temp
def update_others():
global gpageData
gpageData+=u'''
== അവലംബം ==
<references />
== പുറത്തേക്കുള്ള കണ്ണികൾ ==
{{film-stub}}
'''
def udate2wiki(pagename=u'ഉപയോക്താവ്:Manubot/sandbox',towiki=True):
global gpageData
if towiki:
site = wikipedia.getSite('ml','wikipedia')
page = wikipedia.Page(site,pagename)
page.put(gpageData,u'ബോട്ടിന്റെ കൂന്തി വിളയാട്ടം')
wikipedia.stopme()
else:
f = codecs.open(pagename+u'.txt',encoding='utf-8', mode='w')
f.write(gpageData)
f.close()
def savePage(pagename):
updateInfobox()
updateFilmDetails()
updateActortable()
update_music()
update_aniyara()
update_others()
updateIwikis()
udate2wiki(pagename)
def delTestPage(pagename):
myuserpage = u"ഉപയോക്താവ്:" + 'Manubot'
mypage = myuserpage + "/BotLabs/test" + pagename
# doing the job
site = wikipedia.getSite('ml','wikipedia')
page = wikipedia.Page(site,mypage)
page.delete(reason='Deleting Test pages', prompt=False, throttle=True, mark=True)
wikipedia.stopme()
def processDict():
gdictInfobox['country'] = u'{{ഫലകം:Film India}}'
gdictInfobox['language'] = u'മലയാളം'
try:
gdictInfobox['preceded_by'] = gdictInfobox['preceded by']
del gdictInfobox['preceded by']
gdictInfobox['followed_by'] =gdictInfobox['followed by']
del gdictInfobox['followed by']
except KeyError:
pass
convert2native()
link2name()
#unknown2ml()
def convert2native():
for i,j in gdictInfobox.iteritems():
temp = u''
for k in getMyLinks(j):
int_wiki = getInterwiki(k)
if int_wiki:
#replace the data in the dictionary
temp+=u'[['
temp+=int_wiki
temp+=u']]<br/>'
else:
temp+=u'[['
temp+=k
temp+=u']]<br/>'
gdictInfobox[i] = temp
#print gdictInfobox
def link2name():
r = re.compile(r'\s*\(.*?\)',re.UNICODE)
for i,j in gdictInfobox.iteritems():
for k in getMyLinks(j):
x = u''
y = u''
x = r.sub('',k)
#print x,k
if x != k:
#y+=u'[['
y+=k
y+=u' | '
y+=x
#y+=u']]'
gdictInfobox[i] = gdictInfobox[i].replace(k,y,1)
#print gdictInfobox
def unknown2ml():
for i,j in gdictInfobox.iteritems():
for k in getMyLinks(j,1):
if ord(k[0]) < 255:
y = translate2ml(k)
gdictInfobox[i] = gdictInfobox[i].replace(k,y)
gdictInfobox['name'] = translate2ml(gdictInfobox['name'])
#print gdictInfobox
def translate2ml(string):
ml = Transliterate.Transliteration()
return ml.getTransliteration(string)
def processPage(page):
outpage = u'ഉപയോക്താവ്:Manubot/sandbox'
getInfobox(page)
processDict()
savePage(outpage)
gdata = u''
gdictInfobox = {}
if __name__ == '__main__':
gcurrent_page = 'Elektra (2010 film)'
processPage(gcurrent_page)
# def listfilms(cat,iscat=False):
# global conn,curs,lang_code
# site = wikipedia.getSite('en','wikipedia')
# if iscat:
# cat = catlib.Category(site, cat)
# gen = pagegenerators.CategorizedPageGenerator(cat,True)
# else:
# page = wikipedia.Page(site,cat)
# if page.isRedirectPage():
# page = page.getRedirectTarget()
# for i in pagegenerators.LinkedPageGenerator(page):
# if i.exists():
# if i.isRedirectPage():
# i = i.getRedirectTarget()
# res1 = curs.execute('select pages from crawled_db').fetchall()
# if (i.title(),) not in res1:
# curs.execute('INSERT INTO crawled_db VALUES (?)',(i.title(),))
# conn.commit()
# #print i.templates()
# res = curs.execute('select en_link from wiki_db').fetchall()
# #print res
# if (i.title(),) not in res:
# print "listfilms(): not in db " + i.title().encode('utf-8')
# if u'Infobox film'.lower() in [hh.lower() for hh in i.templates()]:
# x = getInterwiki(i,string = False)
# # print 'listfilms():',x.encode('utf-8')
# if x == None:
# print 'listfilms():',i.title().encode('utf-8')
# #appending only thye names
# #page_to_add.append(i.title())
# page_to_add.append(i)
# curs.execute('INSERT INTO wiki_db VALUES (?,?,?,?,?)',(i.title(),None,1,1,lang_code))
# conn.commit()
# else:
# curs.execute('INSERT INTO wiki_db VALUES (?,?,?,?,?)',(i.title(),x,1,0,lang_code))
# conn.commit()
# elif (u'Infobox person'.lower() in [hh.lower() for hh in i.templates()]) or (u'Infobox actor'.lower() in [hh.lower() for hh in i.templates()]):
# print 'listfilms():','Got actor page'
# x = getInterwiki(i,string = False)
# # print 'listfilms():',x.encode('utf-8')
# if x == None:
# print 'listfilms():',i.title().encode('utf-8')
# #appending only thye names
# #page_to_add.append(i.title())
# curs.execute('INSERT INTO wiki_db VALUES (?,?,?,?,?)',(i.title(),None,2,1,lang_code))
# conn.commit()
# else:
# curs.execute('INSERT INTO wiki_db VALUES (?,?,?,?,?)',(i.title(),x,2,0,lang_code))
# conn.commit()
# else:
# print 'listfilms():',"Skipping junk "+i.title().encode('utf-8')
# x = getInterwiki(i,string = False)
# if x is None:
# curs.execute('INSERT INTO wiki_db VALUES (?,?,?,?,?)',(i.title(),None,255,1,lang_code))
# else:
# curs.execute('INSERT INTO wiki_db VALUES (?,?,?,?,?)',(i.title(),x,255,0,lang_code))
# conn.commit()
# else:
# print 'listfilms():',"Skipping as in db "+i.title().encode('utf-8')
# def updatedb(cat):
# global conn,curs,lang_code
# site = wikipedia.getSite('en','wikipedia')
# cat = catlib.Category(site, cat)
# gen = pagegenerators.CategorizedPageGenerator(cat,True)
# for page in gen:
# if page.isRedirectPage():
# page = page.getRedirectTarget()
# print 'updatedb()',page.title().encode('utf-8')
# res1 = curs.execute('select pages from crawled_db').fetchall()
# if (page.title(),) not in res1:
# curs.execute('INSERT INTO crawled_db VALUES (?)',(page.title(),))
# conn.commit()
# res = curs.execute('select en_link from wiki_db').fetchall()
# if (page.title(),) not in res:
# print 'updatedb() not in db '+page.title().encode('utf-8')
# if u'Infobox film'.lower() in [hh.lower() for hh in page.templates()]:
# print 'updatedb()',"Got Film"
# x = getInterwiki(page,string = False)
# # print 'updatedb()',x.encode('utf-8')
# if x == None:
# print 'updatedb()',page.title().encode('utf-8')
# #appending only thye names
# #page_to_add.append(page.title())
# curs.execute('INSERT INTO wiki_db VALUES (?,?,?,?,?)',(page.title(),None,1,1,lang_code))
# conn.commit()
# else:
# curs.execute('INSERT INTO wiki_db VALUES (?,?,?,?,?)',(page.title(),x,1,0,lang_code))
# conn.commit()
# elif (u'Infobox person'.lower() in [hh.lower() for hh in page.templates()]) or (u'Infobox actor'.lower() in [hh.lower() for hh in page.templates()]):
# print 'updatedb()','Got actor page'
# x = getInterwiki(page,string = False)
# # print 'updatedb()',x.encode('utf-8')
# if x == None:
# print 'updatedb()',page.title().encode('utf-8')
# #appending only thye names
# #page_to_add.append(page.title())
# curs.execute('INSERT INTO wiki_db VALUES (?,?,?,?,?)',(page.title(),None,2,1,lang_code))
# conn.commit()
# else:
# curs.execute('INSERT INTO wiki_db VALUES (?,?,?,?,?)',(page.title(),x,2,0,lang_code))
# conn.commit()
# else:
# print 'updatedb()',"List page"
# listfilms(page.title())
# else:
# print 'updatedb()',"Skipping as in db ",page.title().encode('utf-8')
# if __name__ == '__main__':
# global conn,curs
# conn = sqlite3.connect('wiki_db.db')
# curs = conn.cursor()
# try:
# curs.execute('''CREATE TABLE wiki_db (
# en_link TEXT UNIQUE,
# ml_link TEXT,
# type INT,
# missing INT,
# lang INT)''')
# curs.execute('''CREATE TABLE crawled_db (
# pages TEXT UNIQUE)''')
# except sqlite3.OperationalError:
# print "Database present already"
# pass
# #ml = Transliterate.Transliteration()
# updatedb('Category:Hindi-language_films')
# #page_to_add.append('Makalkku')
# # for i in page_to_add:
# # print i
# # j= ml.getTransliteration(i.title())
# # j= i.title()
# # gcurrent_page = i.title()
# # getInfobox(i)
# # processDict()
# # savePage(j)
# # data = u''
# # gdictInfobox = {}
# conn.commit()
# conn.close()