-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwlm_secession.py
73 lines (65 loc) · 2.1 KB
/
wlm_secession.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import codecs
from pg8000 import DBAPI
import wikipedia as pywikibot
from pywikibot import i18n
import category
# FIXME code overlap with wlm_up, shoul be refactored into indafoto
title_template_first = u'%(desc)s (%(koh_id)d. számú műemlék)'
title_template = u'%(desc)s (%(koh_id)d. számú műemlék) %(num)d'
def get_title(row):
row = {
'koh_id': row[0],
'num': row[1],
'desc': row[2],
}
if row['num'] == 1:
title = title_template_first % row
else:
title = title_template % row
return title + '.jpg'
def write_titles():
infile = open('wlm_secession_id.txt', 'rt')
outfile = codecs.open('wlm_secession_title.txt', 'wt', 'utf-8')
conn = DBAPI.connect(host='1.2.3.4', port=12345, database='wikiloves', user='wlm_web', password='XXX')
cursor = conn.cursor()
for id in infile:
id = id.strip().partition('.')[0]
cursor.execute('SELECT i.koh_id, i.num, m.description FROM inda i JOIN monuments m ON i.koh_id = m.azonosito WHERE i.id = %s', (id,))
row = cursor.fetchall()[0]
title = get_title(row)
title = title[0].upper() + title[1:]
outfile.write(title + '\n')
def secession_page_gen():
file = codecs.open('wlm_secession_title.txt', 'rt', 'utf-8')
site = pywikibot.Site('commons', 'commons')
for line in file:
title = 'File:' + line.strip()
page = pywikibot.Page(site, title)
yield page
def check_page_exists():
for page in secession_page_gen():
try:
page.get()
except pywikibot.NoPage:
pywikibot.output(title)
def add_category():
category = u'Art nouveau images from Wiki Loves Monuments 2011'
bot = AddCategory(secession_page_gen())
bot.run(category)
class AddCategory(category.AddCategory):
def run(self, cat):
self.newcatTitle = cat
if not self.site.nocapitalize:
self.newcatTitle = self.newcatTitle[:1].upper() + \
self.newcatTitle[1:]
if not self.editSummary:
self.editSummary = i18n.twtranslate(self.site, 'category-adding',
{'newcat': self.newcatTitle})
counter = 0
for page in self.generator:
self.treat(page)
counter += 1
pywikibot.output(u"%d page(s) processed." % counter)
add_category()