-
Notifications
You must be signed in to change notification settings - Fork 8
/
commons_move_cat_to_subcat.py
85 lines (73 loc) · 2.23 KB
/
commons_move_cat_to_subcat.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Move categories to a subcategory
# Mike Peel 11-Apr-2020 v1 - start
from __future__ import unicode_literals
import pywikibot
import numpy as np
import time
import string
from pywikibot import pagegenerators
from pywikibot import textlib
commons = pywikibot.Site('commons', 'commons')
repo = commons.data_repository() # this is a DataSite object
while(True):
categoryname = input('Category name?')
categoryname = categoryname.strip()
subcategoryname = input('Subcategory name?')
subcategoryname = subcategoryname.strip()
exclude = 'run'
toexclude = []
while exclude != '':
exclude = input('Text to exclude?')
exclude = exclude.strip()
if exclude != '':
toexclude.append(exclude)
print(toexclude)
input('Continue?')
try:
commonscat_page = pywikibot.Category(commons, categoryname)
text = commonscat_page.get()
except:
try:
commonscat_page = pywikibot.Category(commons, categoryname[-1])
text = commonscat_page.get()
except:
print("Couldn't find the category, try again?")
continue
try:
subcommonscat_page = pywikibot.Category(commons, subcategoryname)
text = subcommonscat_page.get()
except:
try:
subcommonscat_page = pywikibot.Category(commons, subcategoryname[-1])
text = subcommonscat_page.get()
except:
print("Couldn't find the subcategory, try again?")
continue
# See if there are subcategories that we want to check in the future
subcats = pagegenerators.SubCategoriesPageGenerator(commonscat_page, recurse=False);
for subcat in subcats:
print('\n')
print(subcat.title())
topass = False
if subcat.title() == commonscat_page.title() or subcat.title() == subcommonscat_page.title():
topass = True
if toexclude != ['']:
for exclude in toexclude:
if exclude in subcat.title():
topass = True
if topass:
continue
try:
target_text = subcat.get()
except:
print('Error, subcat not found!')
continue
target_text = target_text.replace("[["+commonscat_page.title(),"[["+subcommonscat_page.title())
print(target_text)
savemessage = 'Moving from "' + commonscat_page.title() + '" to "' + subcommonscat_page.title() + '"'
# input('Continue?')
subcat.text = target_text.strip()
subcat.save(savemessage)
#EOF