-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfind_a_grave-id.py
89 lines (65 loc) · 2.36 KB
/
find_a_grave-id.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
#!/usr/local/bin/python3
import re
import sys
import common
import pywikibot
from pywikibot import ( ItemPage, pagegenerators )
FAG_ID = 'P535'
WM_IMPORT_URL = 'P4656'
FAG_NAME = 'findagrave'
def main(limit):
site = pywikibot.Site('en', 'wikipedia')
cat = pywikibot.Category(site, 'Find a Grave template with ID not in Wikidata')
repo = site.data_repository()
summary = '([[Wikidata:Requests for permissions/Bot/AmmarBot 5|Adding Find A Grave ID]])'
count = 0
pages = pagegenerators.CategorizedPageGenerator(cat, recurse=False)
for page in pages:
title = page.title()
item = common.getDataItem(page, verbose=True)
if not item:
continue
claims = item.get()['claims']
if FAG_ID in claims:
print('Claim already exists for %s... skipping now.' %title)
continue
if not common.checkInstance('Q5', claims):
continue
res = processPage(page, item, summary)
count += int(res)
if limit == -1:
continue
elif count == limit:
break
print('Finished. Updated %s items' %count)
def processPage(page, item, summary):
def getRelevantVal(templates):
arguments = None
while templates:
# Start from the last template because the template
# we care about here is typically found at the end or
# near the end of a page
template, arguments = templates.pop()
# normalize title for comparison
title = template.title(with_ns=False).lower().replace(' ', '')
if title == FAG_NAME:
break
return arguments
print('Processing %s' %page.title())
templates = page.templatesWithParams()
args = getRelevantVal(templates)
if args and args != []:
value = args[0].strip()
if re.match(r'^[0-9]*$', value):
common.addSingleClaim(
item, FAG_ID, value,
summary=summary, check_value=False, add_ref=True)
permalink = page.permalink(with_protocol = True)
common.addReference(item.title(), FAG_ID, WM_IMPORT_URL, permalink)
return True
return False
if __name__ == '__main__':
if len(sys.argv) < 2:
sys.exit('Limit is required. -1 means no limit')
limit = int(sys.argv[1])
main(limit)