...
 
Commits (3)
......@@ -19,6 +19,8 @@
from __future__ import unicode_literals
from time import time
from weboob.browser import LoginBrowser, URL, need_login
from weboob.capabilities.base import find_object
......@@ -29,13 +31,13 @@ class BibliothequesparisBrowser(LoginBrowser):
BASEURL = 'https://bibliotheques.paris.fr/'
login = URL(r'/Default/Portal/Recherche/logon.svc/logon', LoginPage)
bookings = URL('/Default/Portal/Recherche/Search.svc/RenderAccountWebFrame', LoansPage)
renew = URL(r'/Default/Portal/Services/ILSClient.svc/RenewLoans', RenewPage)
bookings = URL(r'/Default/Portal/Services/UserAccountService.svc/ListLoans\?serviceCode=SYRACUSE&token=(?P<ts>\d+)&userUniqueIdentifier=&timestamp=(?P<ts2>\d+)', LoansPage)
renew = URL(r'/Default/Portal/Services/UserAccountService.svc/RenewLoans', RenewPage)
search = URL(r'/Default/Portal/Recherche/Search.svc/Search', SearchPage)
json_headers = {
'Accept': 'application/json, text/javascript',
'Content-Type': 'application/json; charset=utf-8',
}
def do_login(self):
......@@ -47,19 +49,22 @@ class BibliothequesparisBrowser(LoginBrowser):
@need_login
def get_loans(self):
# do not add any space! the site is so fragile it breaks if even a single whitespace is added...
s = '{"portalId":5,"category":"Loans","providerCode":""}'
self.session.cookies['ErmesSearch_Default'] = '{"mainScenario":"CATALOGUE","mainScenarioText":"Catalogue"}'
self.bookings.go(data=s, headers=self.json_headers)
return self.page.sub.get_loans()
now = int(time() * 1000)
self.bookings.go(ts=now, ts2=now, headers=self.json_headers)
return self.page.get_loans()
@need_login
def do_renew(self, id):
b = find_object(self.get_loans(), id=id)
assert b, 'loan not found'
assert b._renew_data, 'book has no data'
post = u'{"loans":[%s]}' % b._renew_data
self.renew.go(data=post.encode('utf-8'), headers=self.json_headers)
post = {
'loans': [b._renew_data],
'serviceCode': 'SYRACUSE',
'userUniqueIdentifier': '',
}
self.renew.go(json=post, headers=self.json_headers)
self.page.check_error()
def search_books(self, pattern):
max_page = 0
......
......@@ -19,9 +19,13 @@
from __future__ import unicode_literals
from datetime import datetime
from weboob.browser.pages import HTMLPage, JsonPage, LoggedPage
from weboob.browser.elements import ListElement, ItemElement, method, DictElement
from weboob.browser.filters.standard import CleanText, Date, Regexp, Field
from weboob.browser.filters.standard import (
CleanText, Date, Regexp, Field,
)
from weboob.browser.filters.html import Link
from weboob.browser.filters.json import Dict
from weboob.capabilities.base import UserError
......@@ -40,14 +44,42 @@ class JsonMixin(JsonPage):
for err in self.doc.get('errors', []):
raise Exception(err['msg'])
# at this point, success is true, but that doesn't really mean anything
if isinstance(self.doc['d'], list) and self.doc['d']:
# does this still happen?
msg = self.doc['d'][0].get('ErrorMessage')
if msg:
raise UserError(msg)
elif isinstance(self.doc['d'], dict) and self.doc['d'].get('Errors'):
msg = self.doc['d']['Errors'][0].get('Value')
if msg:
raise UserError(msg)
class LoansPage(LoggedPage, JsonPage):
@method
class get_loans(DictElement):
item_xpath = 'd/Loans'
class item(ItemElement):
klass = Book
obj_url = Dict('TitleLink')
obj_id = Dict('Id')
obj_name = Dict('Title')
def obj_date(self):
# 1569967200000+0200 is 2019-10-02 00:00:00 +0200
# but it's considered by the library to be 2019-10-01!
return datetime.fromtimestamp(int(Regexp(Dict('WhenBack'), r'\((\d+)000')(self)) - 3600).date()
obj_location = Dict('Location')
#obj_author = Regexp(CleanText('.//div[@class="loan-custom-result"]//p[@class="template-info"]'), '^(.*?) - ')
def obj__renew_data(self):
return self.el
class LoansPage(LoggedPage, JsonMixin):
def __init__(self, browser, response, *args, **kwargs):
def x__init__(self, browser, response, *args, **kwargs):
super(LoansPage, self).__init__(browser, response, *args, **kwargs)
self.sub = self.sub_class(browser, response, data=self.sub_data)
......
......@@ -62,7 +62,7 @@ class RecipePage(HTMLPage):
json_content = CleanText(u'.',
replace=[('//<![CDATA[ ', ''),
(' //]]>', '')])(item[0])
(' //]]>', '')])(item[1])
self.el = json.loads(json_content)
obj_id = Env('id')
......