-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbiblioapi.py
80 lines (67 loc) · 2.41 KB
/
biblioapi.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
#coding: utf-8
import urllib, urllib2
import json
import re
from flask import Flask, jsonify
import lxml.html
import mechanize
app = Flask(__name__)
@app.route('/biblio/mtl/<isbn>')
def mtl_isbn_api(isbn):
return jsonify(status='ok', data=mtl_get_status_by_isbn(isbn))
@app.route('/biblio/banq/<isbn>')
def banq_isbn_api(isbn):
return jsonify(status='ok', data=banq_get_status_by_isbn(isbn))
MTL_BASE_URL = 'http://nelligan.ville.montreal.qc.ca/search~S58*frc/?'
MTL_BASE_URL_OPTS = {
'searchtype': 'i',
'searchscope': 58,
'SORT': 'DZ',
'extended': 0,
'SUBMIT': 'Chercher',
'searchlimits': ''
}
def mtl_get_status_by_isbn(isbn):
"""Given an ISBN string, returns availability status of holdings in the
Ville de Montréal libraries."""
urlopts = dict(MTL_BASE_URL_OPTS)
urlopts['searcharg'] = isbn
url = MTL_BASE_URL + urllib.urlencode(urlopts)
urlresp = urllib2.urlopen(url)
tree = lxml.html.parse(urlresp)
root = tree.getroot()
result = []
for itemrow in root.cssselect('tr.bibItemsEntry'):
cols = [c.text_content().strip() for c in itemrow.cssselect('td')]
assert len(cols) == 4
r = {
'bibliotheque': cols[0],
'note': cols[1],
'cote': cols[2],
'statut': cols[3]
}
r['disponible'] = 'DISPO' in r['statut']
result.append(r)
return result
BANQ_BASE_URL = 'http://iris.banq.qc.ca/alswww2.dll/APS_ZONES?fn=AdvancedSearch&Style=Portal3&SubStyle=&Lang=FRE&ResponseEncoding=utf-8'
def banq_get_status_by_isbn(isbn):
"""Given an ISBN string, returns availability status of holdings in the
Grande Bibliothèque of the Bibliothèques et Archives Nationales du Québec."""
br = mechanize.Browser()
br.open(BANQ_BASE_URL)
br.select_form(name="ExpertSearch")
br["q.form.t1.term"] = ["NUMERO="]
br["q.form.t1.expr"] = str(isbn)
resp2 = br.submit()
resp3 = br.follow_link(url_regex=re.compile(r'^APS_PRESENT_BIB'))
root = lxml.html.fromstring(resp3.get_data())
result = []
for item in root.cssselect('.DetailDataCell ol li'):
itemtext = item.text_content()
result.append({
'cote': item.cssselect('a')[0].text_content().strip(),
'disponible': ('Disponible' in itemtext and 'Disponible le' not in itemtext)
})
return result
if __name__ == '__main__':
app.run()