-
Notifications
You must be signed in to change notification settings - Fork 0
/
filmBot2.py
201 lines (151 loc) · 5.28 KB
/
filmBot2.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
import pywikibot, re, os, time, mwparserfromhell, sys, json
import toolforge
from datetime import date, datetime, timedelta, timezone
from pytz import timezone
from scripts import upload
#Atmaksas stunda
#127 stundas
#Kārtējais pirmais randiņš
conn = toolforge.connect('lvwiki_p','analytics')
connLabs = toolforge.connect_tools('s53143__meta_p')
cursor1 = connLabs.cursor()
ensite = pywikibot.Site("en", "wikipedia")
lvsite = pywikibot.Site("lv", "wikipedia")
imgregex = re.compile("^(\s*\|\s*attēls\s*=\s*)$",re.M)
tplnames_lv = ['filmas infokaste','filma']
tplnames_en = ['film infobox','infobox film','infobox japanese film','infobox movie','infobox tamil film','infobox television']
lv_params = ['attēls']
en_params = ['image']
def do_upl(imgname):
imgname = imgname.replace('_',' ')
imgname = re.sub('^([Ff]ile|[Ii]mage):','',imgname)
if '{{!}}' in imgname or '|' in imgname:
return False
#imgname = re.sub('({{!}}|\|).+$','',imgname)#fixme: paplašinājums
imgname = re.sub('(<!--.*?-->)','',imgname)
image = "File:"+imgname
page = pywikibot.Page(ensite,image)
#pywikibot.output(page)
imagePage = pywikibot.FilePage(ensite,image)
finalTitle = imagePage.title(with_ns=False)
if imagePage.isRedirectPage():
finalTitle = imagePage.getRedirectTarget().title(with_ns=False)
pywikibot.output(imagePage)
desc = "== Avots ==\n[[:en:File:{name}|{name}]]\n\n== Licence ==\n{{{{Filmas plakāts}}}}".format(name=finalTitle)
#url = 'https://upload.wikimedia.org/wikipedia/en/b/b4/Project_X_Poster.jpg'
bot = upload.UploadRobot(url=imagePage.fileUrl(), description=desc,
keepFilename=True,
verifyDescription=False, ignoreWarning=True,
targetSite=lvsite)
bot.run()
return finalTitle
def downloadPhoto(url):
'''
Download the photo and store it in a StrinIO.StringIO object.
TODO: Add exception handling
'''
imageFile=urllib.request.urlopen(url).read()
#)
return io.StringIO(str(imageFile))
def getparam(tlobject,params):
for param in params:
if tlobject.has(param):
opficmlapa = tlobject.get(param).value.strip()
if opficmlapa!='':
return opficmlapa
break
#else:
return False
#
def enwiki(entitle):
#entitle = getEnWiki(lvarticle)
if not entitle or entitle=='':
return
page = pywikibot.Page(ensite,entitle)
pagetext = page.get()
wikicode = mwparserfromhell.parse(pagetext)
templates = wikicode.filter_templates()
for tpl in templates:
name = tpl.name.lower().strip().replace('_',' ')
if name in tplnames_en:
image_en = getparam(tpl,en_params)
if image_en:
#do upload
print('image found')
imgname = do_upl(image_en)
return imgname
break
#
def pagename(bg):
bg = re.sub('\s*(\([^\(]+)$','',bg)
return bg
#
def create_redirect(thispage,redirecttitle):
thispage = thispage.replace('_',' ')
if not redirecttitle: return 0
redirecttitle = pagename(redirecttitle.replace('_',' '))
if redirecttitle==thispage: return 0
if redirecttitle=='': return 0
redPage = pywikibot.Page(lvsite,redirecttitle)
if redPage.exists(): return 0
#pywikibot.output('\t'.join([origtitle,partial,variant,"{}{}".format(partial,variant)]))
redPage.text = "#REDIRECT [[{}]]".format(thispage)
redPage.save(summary='bots: pāradresācija uz [[{}]]'.format(thispage), botflag=True, minor=False)
#
def lvwikistuff(articleLV,articleEN):
page = pywikibot.Page(lvsite,articleLV)
#create_redirect(articleLV,articleEN)
pagetext = page.get()
wikicode = mwparserfromhell.parse(pagetext)
templates = wikicode.filter_templates()
for tpl in templates:
name = tpl.name.lower().strip().replace('_',' ')
if name in tplnames_lv:
image_lv = getparam(tpl,lv_params)
if not image_lv:
#do upload
print('no image')
resulting = enwiki(articleEN)
if resulting:
wikicoddasde = re.sub(imgregex,r'\1 '+resulting,pagetext)
pywikibot.showDiff(wikicode,wikicoddasde)
page.put(wikicoddasde, "Bots: pievienots attēls")
break
#
def encode_if_necessary(b):
if type(b) is bytes:
return b.decode('utf8')
return b
def run_query(query,connection = conn):
#query = query.encode('utf-8')
#print(query)
try:
cursor = connection.cursor()
cursor.execute(query)
rows = cursor.fetchall()
except KeyboardInterrupt:
sys.exit()
return rows
#
SQLMAIN = """select orig.page_title, (select m2.ll_title from langlinks m2 where m2.ll_from=orig.page_id and m2.ll_lang="en")
from page orig
join templatelinks t on orig.page_id = t.tl_from AND t.tl_namespace = 10 AND t.tl_title='Filmas_infokaste'
where orig.page_is_redirect=0 and orig.page_namespace=0
"""
def get_articles():
query_res = run_query(SQLMAIN,conn)
return [[encode_if_necessary(f[0]),encode_if_necessary(f[1])] for f in query_res]
#
def main():
#articles= [f for f in filmlist.split('\n') if len(f)>3]#
#articlelist = ['','']
wanted = 'Kin-dza-dza'
skipped = False
thelist = get_articles()
for article in thelist:
if article[0] != wanted and not skipped:
continue
skipped = True
lvwikistuff(article[0],article[1])
#set_last_run(datetime.utcnow())
main()