forked from mpeel/wikicode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommons_categoryitem.py
97 lines (83 loc) · 2.88 KB
/
commons_categoryitem.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Create new Wikidata items
# Started 25 August 2018 by Mike Peel
# 3 November 2018 - focus on people for now
from __future__ import unicode_literals
import pywikibot
import numpy as np
import time
import string
from pywikibot import pagegenerators
from pywikibot.data import api
import urllib
commons = pywikibot.Site('commons', 'commons')
repo = commons.data_repository() # this is a DataSite object
wikidatainfobox = ["Wikidata Infobox", "Wikidata infobox", "wikidata infobox", "wikidata Infobox", "Infobox Wikidata", "infobox Wikidata", "infobox wikidata", "Infobox wikidata", "Wikidata infobox", "wikidata infobox", "Wikidata Infobox", "wikidata Infobox", "Wdbox", "wdbox", 'WI']
def newitem(category, items,cat=True):
new_item = pywikibot.ItemPage(repo)
label = category.title()
if cat == False:
label = label.replace('Category:','')
new_item.editLabels(labels={"en":label}, summary="Creating item")
candidate_item = pywikibot.ItemPage(repo, new_item.getID())
print(candidate_item)
data = {'sitelinks': [{'site': 'commonswiki', 'title': category.title()}]}
candidate_item.editEntity(data, summary=u'Add commons sitelink')
for item in items:
claim = pywikibot.Claim(repo, item[0])
if item[0] == 'P458':
claim.setTarget(item[1])
else:
claim.setTarget(pywikibot.ItemPage(repo, item[1]))
try:
candidate_item.addClaim(claim, summary=u'Setting '+item[0]+' value')
except:
print("That didn't work")
return candidate_item
category = 'Category:Uses of Wikidata Infobox with manual qid'
targetcats = pagegenerators.SubCategoriesPageGenerator(cat, recurse=False);
for targetcat in targetcats:
# Get the page contents
commonscat_page = pywikibot.Page(commons, targetcat)
text = commonscat_page.get()
# See if we can find the QID
id_val = (text.split("{{"+templates[i]+"|qid="))[1].split("}}")[0]
try:
id_val = id_val.split('|')[0]
except:
# null = 1
continue
# Make sure the category isn't already connected to an item
try:
# item_dict = page.get()
# qid = page.title()
wd_item = pywikibot.ItemPage.fromPage(commonscat_page)
item_dict = wd_item.get()
qid = wd_item.title()
print('Matching item found')
continue
except:
null = 1
# Get the item, and make sure it doesn't have a P910 value
try:
# item_dict = page.get()
# qid = page.title()
wd_item = pywikibot.ItemPage.fromPage(commonscat_page)
item_dict = wd_item.get()
qid = wd_item.title()
print('Matching item found')
continue
except:
null = 1
# Start assembling the Wikdata entry
items = [['P31','Q4167836']] # Instance of Wikimedia category
items.append(['P301',id_val]) # Main topic
print(items)
test = input('Create new category item?')
if test == 'y':
new_item = newitem(commonscat_page, items)
newclaim = pywikibot.Claim(repo, 'P910')
newclaim.setTarget(new_item)
topic_item.addClaim(newclaim, summary=u'Link to category item')
#EOF