-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathname_uris.py
146 lines (101 loc) · 4.22 KB
/
name_uris.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import urllib, requests, re, json, csv, sys
from bs4 import BeautifulSoup
from rdflib import Graph, Literal, BNode, Namespace, RDF, URIRef
def getNames():
names = {}
#Read names from text files
# with open ('G:/scripts/voyager_names.txt', 'r') as text:
# count = 1
# for line in text:
# name = line.rstrip('\n')
# l = []
# l.append(name)
# names[count]=l
# count += 1
# Scrape names from dimenovels.org
soup = BeautifulSoup(urllib.urlopen('http://dimenovels.org/People'))
for a in soup.findAll('a'):
if a.has_key('href'):
if re.match(r'\/([A-Z])\w+\/[0-9]+', a['href'], re.UNICODE):
href = a['href']
name = a.contents[0].lstrip().rstrip().encode('utf-8')
l = []
l.append(name)
names[href] = l
return names
def getURIs():
names = getNames()
for key, value in names.iteritems():
# Escape special characters in name label for URL
name = urllib.quote(value[0])
print name
# Use name to get LC, VIAF, and WKP URIs
try:
# Get LCNAF using Linked Data Service label redirect
response = requests.get('http://id.loc.gov/authorities/label/%s' % name)
link = response.url
pattern = re.compile(r'(.+)\.html$')
lcnaf = pattern.findall(link)[0].encode('utf-8')
names[key].append(lcnaf)
print "Found LC %s for %s" % (lcnaf, key)
# Prepare LCNAF for VIAF request
r = re.search('([a-z]+)([0-9]+)$', lcnaf)
lcnaf_viaf = r.group(1).split()[0] + ' ' + r.group(2).split()[0]
# Get VIAF using LCNAF
try:
response = requests.get('http://viaf.org/viaf/sourceID/' + urllib.quote('LC| ' + lcnaf_viaf))
viaf = response.url.encode('utf-8')
names[key].append(viaf)
print "Found VIAF %s for %s" % (viaf, key)
# Get WKP from VIAF JSON
# JSON seems to be easier and more reliable than HTML or cluster XML
j = viaf + 'justlinks.json'
response = requests.get(j)
data = json.loads(response.text)
try:
wkp = data['WKP'][0].encode('utf-8')
names[key].append(wkp)
# Fetch Wikipedia abstract
# Similar functions can be written to fetch any data from LCNAF or VIAF
try:
abstract = getAbstract(wkp)
names[key].append(abstract)
except:
names[key].append('NoAbstract')
except:
print "No WKP found for %s" % key
names[key].append('NoWKP')
names[key].append('NoAbstract')
except:
print "No VIAF found for %s" % key
names[key].append('NoVIAF')
names[key].append('NoWKP')
names[key].append('NoAbstract')
except:
print "No LC found for %s" % key
names[key].append('NoLC')
names[key].append('NoVIAF')
names[key].append('NoWKP')
names[key].append('NoAbstract')
return names
def getAbstract(wkp):
wkp = urllib.quote(wkp)
url = 'http://dbpedia.org/data/%s.ntriples' % wkp
g = Graph()
g.parse(url, format='nt')
query = g.query(
"""SELECT ?abstract
WHERE {
<http://dbpedia.org/resource/%s> <http://dbpedia.org/ontology/abstract> ?abstract .
FILTER(langMatches(lang(?abstract), "EN"))
}""" % wkp)
abstract = ''.join(query.result[0]).encode('utf-8')
return abstract
def main(argv):
names = getURIs()
with open('G:/scripts/dime_names_2015_04_17.csv', 'wb') as f:
writer = csv.writer(f)
for key, value in names.items():
writer.writerow([key] + value)
if __name__ == '__main__':
sys.exit(main(sys.argv))