-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
194 lines (160 loc) · 6.94 KB
/
views.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
'''
Created on Oct 6, 2011
@author: tim
'''
from django.shortcuts import render_to_response, HttpResponse
from django.core.context_processors import csrf
from django.core import serializers
from django.utils import simplejson
from django.template import RequestContext
from discorrelate.forms import requestForm
from json import JSONEncoder
#import discogs_client as discogsAPI
import re
def getQuery(apiKey, dataset, query):
import httplib2
import urllib
h = httplib2.Http(".cache")
baseURL = "http://api.kasabi.com/dataset/%s/apis/sparql?" % dataset
params = urllib.urlencode({'apikey':apiKey, 'query':query, 'output':'json'})
response, content = h.request(baseURL+params)
if response.status in range(200,300):
import simplejson as json
return json.loads(content)
else:
print "Oh no! %d %s " % (response.status, response.reason)
return False
def flatten(results):
nodes, links = [], []
nodesLookup = {} # A dictionary to lookup which nodes have already been encountered
linksLookup = {} # Dictionary of tuples where (linkStart,linkFinish)
for subject in results:
if not subject in nodesLookup:
nodesLookup[subject] = len(nodes)
nodes.append({}) # add blank node, will fill with properties later
for predicate in results[subject]:
if predicate == 'http://data.rewire.it#linked':
# this predicate has been reserved to specify links
objects = results[subject][predicate]
for object in objects:
if not object['value'] in nodesLookup:
nodesLookup[object['value']] = len(nodes)
nodes.append({}) # add blank node, will fill with properties later
# now add the links
linkStart = nodesLookup[subject]
linkEnd = nodesLookup[object['value']]
link = (linkStart, linkEnd)
if not link in links:
linksLookup[link] = len(links)
links.append({'source':linkStart,'target':linkEnd,'value':1})
else:
newValue = links[linksLookup[link]]['value']+1
links[linksLookup[link]]['value'] = newValue
else:
# any predicate not specified as a link is added to the node as a property
#get last bit of URL to use as property name (bit after the last / or #)
splits = re.split(r'[/|#]', predicate)
propName = splits[len(splits)-1]
objects = results[subject][predicate]
for object in objects:
nodes[nodesLookup[subject]][propName] = object['value']
return simplejson.dumps({'nodes':nodes,'links':links})
def root(request):
if not request.POST:
form = requestForm()
c = RequestContext(request, {'form':form})
c.update(csrf(request))
return render_to_response('root.html',c)
url = request.POST.get('url')
year = request.POST.get('year')
#query = request.POST.get('query')
q="""
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX mo: <http://purl.org/ontology/mo/>
PREFIX dateIssued: <http://purl.org/dc/terms/issued>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX dc: <http://purl.org/dc/terms/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX INPUT: <%(url)s>
CONSTRUCT{
?NODEartist1 <http://data.rewire.it#linked> ?NODErelease1;
foaf:name ?artist1Name;
mo:discogs ?artist1Discogs;
rdf:type ?artist1Type.
?NODErelease1 <http://data.rewire.it#linked> ?NODElabel;
dc:title ?release1Title;
mo:discogs ?release1Discogs;
rdf:type ?release1Type.
?NODElabel <http://data.rewire.it#linked> ?NODErelease2;
foaf:name ?labelName;
mo:discogs ?labelDiscogs;
rdf:type ?labelType.
?NODEartist2 <http://data.rewire.it#linked> ?NODErelease2;
foaf:name ?artist2name;
mo:discogs ?artist2Discogs;
rdf:type ?artist2Type.
?NODErelease2 dc:title ?release2Title;
mo:discogs ?release2Discogs;
rdf:type ?release2Type.
}
WHERE{
?NODEartist1 mo:discogs INPUT:;
foaf:name ?artist1Name;
mo:discogs ?artist1Discogs;
rdf:type ?artist1Type;
foaf:made ?NODErelease1.
?NODErelease1 dateIssued: "%(year)s"^^xsd:year;
dc:title ?release1Title;
rdf:type ?release1Type;
mo:discogs ?release1Discogs.
# - - - - - - - - - - - - - - - - -
?NODErelease1 mo:publisher ?NODElabel.
?NODElabel foaf:name ?labelName;
mo:discogs ?labelDiscogs;
rdf:type ?labelType.
# - - - - - - - - - - - - - - - - -
?NODErelease2 mo:publisher ?NODElabel.
?NODErelease2 dateIssued: "%(year)s"^^xsd:year;
dc:title ?release2Title;
mo:discogs ?release2Discogs;
rdf:type ?release2Type.
# - - - - - - - - - - - - - - - - -
?NODErelease2 foaf:maker ?NODEartist2.
?NODEartist2 foaf:name ?artist2name;
mo:discogs ?artist2Discogs;
rdf:type ?artist2Type.
}
LIMIT 100
"""% {'url':url,'year':year}
#discogsAPI.user_agent = 'discorrelate/0.1 +http://www.rewire.it'
print q
apiKey = '5dac588049e18e51fead5e788dc2449e38c2077a'
results = getQuery(apiKey, "discogs", q)
jsonny = flatten(results)
return HttpResponse(jsonny, content_type="application/json; charset=utf8")
# elif query == 'colabs':
# q = """
# PREFIX foaf: <http://xmlns.com/foaf/0.1/>
# PREFIX mo: <http://purl.org/ontology/mo/>
# PREFIX dc: <http://purl.org/dc/terms/>
# CONSTRUCT {
#
# }
# SELECT DISTINCT ?artist ?name WHERE{
# <""" + url + """> foaf:made ?release.
# ?artist foaf:made ?release.
# ?artist foaf:name ?name.
# }
# """
#
# elif query == 'comps':
# q = """
# PREFIX foaf: <http://xmlns.com/foaf/0.1/>
# PREFIX mo: <http://purl.org/ontology/mo/>
# PREFIX dc: <http://purl.org/dc/terms/>
# SELECT DISTINCT ?artist ?name WHERE{
# <""" + url + """> foaf:made ?release.
# ?artist foaf:made ?release.
# ?artist foaf:name ?name.
# }
# """