-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_resolver_map.py
executable file
·69 lines (54 loc) · 1.48 KB
/
make_resolver_map.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Autore: Cristian Consonni <[email protected]>
# Inspired by this gist by atomotic:
# https://gist.github.com/atomotic/7229203
#
# The code is released with an MIT license
# please see the LICENSE file for details.
import sys
import csv
# My pywikibot installation is not working as it should be.
# So I have downloaded the code from:
# http://tools.wmflabs.org/pywikibot/
# and extracted it in the current directory
# Now I have a "core" directory in the current dir.
sys.path.append('core')
import pywikibot
q = "Q{}".format(sys.argv[1])
site = pywikibot.Site('it','wikipedia')
repo = site.data_repository()
item = pywikibot.ItemPage(repo, q)
try:
data = item.get()
except pywikibot.exceptions.NoPage as e:
print "Error fetching item Q{}".format(sys.argv[1])
exit(0)
thes = item.claims['P508'][0].getTarget()
itwiki = u''
try:
itwiki = data['sitelinks']['itwiki']
except KeyError:
pass
enwiki = u''
try:
enwiki = data['sitelinks']['enwiki']
except KeyError:
pass
OUTFILENAME = 'resolv.map'
FIELDNAMES = ('wikidata_id', 'thes_id', 'itwiki', 'enwiki')
out = open(OUTFILENAME, "a+")
writer = csv.DictWriter(out, FIELDNAMES)
elements = [sys.argv[1],
thes.encode('utf-8'),
itwiki.encode('utf-8'),
enwiki.encode('utf-8'),
]
row = dict(zip(FIELDNAMES, elements))
try:
writer.writerow(row)
except Exception as e:
print "Error with row: ", e, row
out.close()
exit(0)