-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgetdiberri.py
67 lines (53 loc) · 1.76 KB
/
getdiberri.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
import cgi
import cgitb; cgitb.enable()
import urllib
#import urllib2 # Now called inside cachedFetch
from cachedfetch import cachedFetch
import re
from django.utils import simplejson as json
def error(message):
print 'Content-Type: text/plain'
print ''
print message
exit()
def main():
form = cgi.FieldStorage()
if (not form.has_key("isbn")):
error('No isbn!')
isbn = form["isbn"].value
isbn = re.sub('[^0-9]', '', isbn)
if (not form.has_key("callback")):
error('No callback.')
callback = form["callback"].value
url = "http://diberri.dyndns.org/cgi-bin/templatefiller/index.cgi?type=isbn&vertical=1&dont_use_etal=1&format=xml&id=" + isbn
xmldata = cachedFetch(url, 3600)
if xmldata == '':
error('Bad data from Diberri tool')
#error(xmldata)
bookdata = {}
for line in xmldata.splitlines():
m = re.search('^\|title=(.+)$', line, re.I)
if m:
bookdata['title'] = m.group(1)
m = re.search('\|publisher=(.+)$', line, re.I)
if m:
bookdata['publisher'] = m.group(1)
m = re.search('\|location=(.+)$', line, re.I)
if m:
bookdata['location'] = m.group(1)
m = re.search('\|year=(.+)$', line, re.I)
if m:
bookdata['year'] = m.group(1)
m = re.search('\|isbn=(.+)$', line, re.I)
if m:
bookdata['isbn'] = m.group(1)
m = re.search('\|author=(.+)$', line, re.I)
if m:
bookdata['authors'] = m.group(1)
jsonstr = json.dumps(bookdata, sort_keys=True, indent=4, ensure_ascii=False)
jsonp = callback + '(' + jsonstr + ');'
print 'Content-Type: text/javascript'
print ''
print jsonp
if __name__ == "__main__":
main()