-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonldproxy.py
More file actions
32 lines (26 loc) · 824 Bytes
/
jsonldproxy.py
File metadata and controls
32 lines (26 loc) · 824 Bytes
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
#!/usr/bin/env python
"""Support for proxying JSON-LD data."""
__author__ = 'Sampo Pyysalo'
__license__ = 'MIT'
import urllib
def rewrite_urls(data, proxy_url):
"""Rewrite URLs in JSON-LD data to pass through proxy."""
if isinstance(data, dict):
data = _rewrite_dict_urls(data, proxy_url)
elif isinstance(data, list):
data = _rewrite_list_urls(data, proxy_url)
else:
pass
return data
def _rewrite_dict_urls(dict_, proxy_url):
if '@id' in dict_:
dict_['@id'] = proxy_url + urllib.quote(dict_['@id'])
for key, value in dict_.iteritems():
if key == '@id':
continue
rewrite_urls(value, proxy_url)
return dict_
def _rewrite_list_urls(list_, proxy_url):
for item in list_:
rewrite_urls(item, proxy_url)
return list_