Commit 16cfab4b authored by Laurent's avatar Laurent

upgrade syracuse/ermes adapter: fetch holds

parent 9e58846f
import {PortalAdapter} from './adapter'
import * as cheerio from 'cheerio'
import {Account, Loan} from '../../models'
import {Account, Loan, Hold} from '../../models'
import {WrongLoginPassword} from './wrong-login-password'
import * as papa from 'papaparse'
......@@ -89,20 +89,6 @@ export class Ermes extends PortalAdapter {
}
protected _fetchLoans(account): Promise<Array<Loan>> {
return this._fetchLoansHTML(account)
/* let token = this.getTime()
return this
.request(account,
{ url: account.getUrl() + '/Default/Portal/Services/ilsclient.svc/ExportAccount?type=csv&sections=loans&Token=' + token })
.then( (response) => {
return response.statusCode == 404
? this._fetchLoansHTML(account)
: this._parseLoansCSV(account, response)
})*/
}
protected _parseLoansCSV(account, response): Array<Loan> {
let loans: Array<Loan> = new Array<Loan>()
let datas = papa.parse(response.content.toString(),
......@@ -153,7 +139,7 @@ export class Ermes extends PortalAdapter {
}
protected _fetchLoansHTML(account: Account): Promise<Array<Loan>> {
protected _fetchLoans(account): Promise<Array<Loan>> {
return this
._requestAccountCategory(account, 'Loans')
.then( (response) => {
......@@ -179,7 +165,7 @@ export class Ermes extends PortalAdapter {
let record_search_id = /\/(rsc|doc\/[^\/]+)\/([a-zA-Z0-9]+)\//.exec(record_link)
let record_id = record_search_id ? record_search_id[2] : ''
let loan_data = JSON.parse(node.find('.loan-data').text())
let loan_data = JSON.parse(node.find('.loan-data').text().trim() || '{}')
let author = node.find('.vignette_document img')
.attr('alt')
......@@ -208,4 +194,63 @@ export class Ermes extends PortalAdapter {
return Array.from(loans.values());
}
protected _fetchHolds(account): Promise<Array<Hold>> {
return this
._requestAccountCategory(account, 'Loans')
.then( (response) => {
return this._parseHoldsHTML(account, response)
})
}
protected _parseHoldsHTML(account, response): Array<Hold> {
let holds: Map<string, Hold> = new Map<string, Hold>()
let $ = cheerio.load(response.content.toJSON().d)
let datas = $('.reservation-results li.loan-item')
datas.each((i, elem) => {
let node = $(elem)
let title = node.find('.title[title]').attr('title')
let thumbnail = node.find('.vignette_document img').attr('src')
let record_link = node.find('a[href*="/rsc/"]').add('a[href*="/doc/"]').attr('href')
let record_search_id = /\/(rsc|doc\/[^\/]+)\/([a-zA-Z0-9]+)\//.exec(record_link)
let record_id = record_search_id ? record_search_id[2] : ''
let library = node.find('.holdingplace .loan-info-value').text().trim();
let hold_data = JSON.parse(node.find('.loan-data').text().trim() || '{}')
let author = node.find('.vignette_document img')
.attr('alt')
.split('|')
.slice(-1)[0]
.trim();
if (!author) {
author = node.find('.title[title] + p').text().split('.')[0].trim()
}
let status = node.find('.whenavailableend').length
? L('hold_available')
: L('hold_not_available');
let hold = new Hold()
.setHoldId(hold_data.HoldingId || hold_data.RecordId)
.setTitle(title)
.setAuthor(author)
.setLibrary(library)
.setStatus(status)
.setRecordId(record_id)
.setRecordUrl(record_link)
.setRecordThumbnail(thumbnail)
holds.set(hold.getHoldId(), hold)
})
return Array.from(holds.values());
}
}
This diff is collapsed.
......@@ -81,7 +81,7 @@ describe('Account on Ermes', () => {
})
it('v2 should have one loan', () => {
it('v2 should have one loan and one hold', () => {
let http = new ErmesLoansWithSuccessfulLoginV2('http://ermes.fr', time)
return adapter.setHTTP(http).refresh(account).then(() => {
let loans = account.findLoans(db);
......@@ -94,11 +94,23 @@ describe('Account on Ermes', () => {
expect(loans[0].getRecordThumbnail()).to.equals('http://marketplace.archimed.fr/Cover/VPCO/MONO/EEJ5pTzUVIHLMPY8ccApeQ2/978-2-7459-8430-2/MEDIUM?fallback=https%3a%2f%2fermes.fr%2fui%2fskins%2fdefault%2fportal%2ffront%2fimages%2fGeneral%2fDocType%2fMONO_MEDIUM.png')
expect(loans[0].getRecordId()).to.equals('1107727')
expect(loans[0].getRecordUrl()).to.equals('https://ermes.fr/Default/doc/SYRACUSE/1107727/copain-des-geeks');
let holds = account.findHolds(db);
expect(holds).to.have.lengthOf(1)
expect(holds[0].getHoldId()).to.equals('1105779')
expect(holds[0].getTitle()).to.equals('Chasse royale. 2, Deuxième branche')
expect(holds[0].getAuthor()).to.equals('Jean-Philippe Jaworski (1969-....). Auteur')
expect(holds[0].getLibrary()).to.equals('75020 - Marguerite Duras')
expect(holds[0].getStatus()).to.equals(L('hold_not_available'))
expect(holds[0].getRecordThumbnail()).to.equals('http://marketplace.archimed.fr/Cover/VPCO/MONO/T39kO8wocqk3RKO1M_B32g2/978-2-36183-375-6/MEDIUM?fallback=https%3a%2f%2fermes.fr%2fui%2fskins%2fdefault%2fportal%2ffront%2fimages%2fGeneral%2fDocType%2fMONO_MEDIUM.png')
expect(holds[0].getRecordId()).to.equals('1105779')
expect(holds[0].getRecordUrl()).to.equals('https://ermes.fr/Default/doc/SYRACUSE/1105779/chasse-royale-2-deuxieme-branche');
})
})
it('v3 should have one loan', () => {
it('v3 should have one loan and one hold', () => {
let http = new ErmesLoansWithSuccessfulLoginV3('http://ermes.fr', time)
return adapter.setHTTP(http).refresh(account).then(() => {
let loans = account.findLoans(db);
......@@ -111,6 +123,17 @@ describe('Account on Ermes', () => {
expect(loans[0].getRecordThumbnail()).to.equals('http://marketplace.archimed.fr/Cover/CACM/AUDI/2HasC_PL5occK5oGjNwLHQ2/3770000853049/MEDIUM?fallback=http%3a%2f%2fermes.fr%2fui%2fskins%2fdefault%2fportal%2ffront%2fimages%2fGeneral%2fDocType%2fAUDI_MEDIUM.png')
expect(loans[0].getRecordId()).to.equals('frOr1434603153')
expect(loans[0].getRecordUrl()).to.equals('http://ermes.fr/Default/doc/ORPHEE/frOr1434603153/berceuses-de-toujours-readaptation-en-berceuses-des-plus-belles-chansons-d-enfants-1');
let holds = account.findHolds(db);
expect(holds).to.have.lengthOf(1)
expect(holds[0].getHoldId()).to.equals('31715092039219')
expect(holds[0].getTitle()).to.equals('Imagier mouvementé')
expect(holds[0].getAuthor()).to.equals('Joffre, Véronique (1982-....)')
expect(holds[0].getLibrary()).to.equals('')
expect(holds[0].getStatus()).to.equals(L('hold_available'))
expect(holds[0].getRecordId()).to.equals('frOr1425701132')
expect(holds[0].getRecordUrl()).to.equals('http://ermes.fr/Default/doc/ORPHEE/frOr1425701132/imagier-mouvemente');
})
})
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment