-
Notifications
You must be signed in to change notification settings - Fork 0
/
catall.py
113 lines (103 loc) · 3.4 KB
/
catall.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
# -*- coding: utf-8 -*-
"""
Add or change categories on a number of pages. Usage: catall.py name - goes
through pages, starting at 'name'. Provides the categories on the page and asks
whether to change them. If no starting name is provided, the bot starts at 'A'.
Options:
-onlynew : Only run on pages that do not yet have a category.
"""
#
# (C) Rob W.W. Hooft, Andre Engels, 2004
# (C) Pywikipedia bot team, 2004-2011
#
# Distributed under the terms of the MIT license.
#
__version__ = '$Id$'
#
import sys
import wikipedia as pywikibot
from pywikibot import i18n
def choosecats(pagetext):
chosen=[]
flag=False
length=1000
print ("Give the new categories, one per line.")
print ("Empty line: if the first, don't change. Otherwise: Ready.")
print ("-: I made a mistake, let me start over.")
print ("?: Give the text of the page with GUI.")
print ("??: Give the text of the page in console.")
print ("xx: if the first, remove all categories and add no new.")
print ("q: quit.")
while flag == False:
choice=pywikibot.input(u"?")
if choice=="":
flag=True
elif choice=="-":
chosen=choosecats(pagetext)
flag=True
elif choice=="?":
import editarticle
editor = editarticle.TextEditor()
newtext = editor.edit(pagetext)
elif choice =="??":
pywikibot.output(pagetext[0:length])
length = length+500
elif choice=="xx" and chosen==[]:
chosen = None
flag=True
elif choice=="q":
print "quit..."
sys.exit()
else:
chosen.append(choice)
return chosen
def make_categories(page, list, site = None):
if site is None:
site = pywikibot.getSite()
pllist=[]
for p in list:
cattitle="%s:%s" % (site.category_namespace(), p)
pllist.append(pywikibot.Page(site,cattitle))
page.put_async(pywikibot.replaceCategoryLinks(page.get(), pllist),
comment=i18n.twtranslate(site.lang, 'catall-changing'))
def main():
docorrections=True
start=[]
for arg in pywikibot.handleArgs():
if arg == '-onlynew':
docorrections=False
else:
start.append(arg)
if start == []:
start='A'
else:
start=' '.join(start)
mysite = pywikibot.getSite()
for p in mysite.allpages(start = start):
try:
text=p.get()
cats=p.categories()
if cats == []:
pywikibot.output(u"========== %s ==========" % p.title())
print "No categories"
print "-" * 40
newcats=choosecats(text)
if newcats != [] and newcats is not None:
make_categories(p, newcats, mysite)
elif docorrections:
pywikibot.output(u"========== %s ==========" % p.title())
for c in cats:
pywikibot.output(c.title())
print "-" * 40
newcats=choosecats(text)
if newcats is None:
make_categories(p, [], mysite)
elif newcats != []:
make_categories(p, newcats, mysite)
except pywikibot.IsRedirectPage:
pywikibot.output(u'%s is a redirect' % p.title())
if __name__ == "__main__":
try:
main()
finally:
pywikibot.stopme()