Commit a88a4ae0 authored by laurent l's avatar laurent l

fix bundle

parent ebd3f9bb
import * as http from 'http';
import * as http from 'tns-core-modules/http';
export interface HTTPClient {
request(options: any): Promise<http.HttpResponse>;
request(options: any): Promise<http.HttpResponse>;
}
import { HTTPClient } from './http-client';
import { Cookies } from '../';
import * as http from 'http';
import * as http from 'tns-core-modules/http';
export class NSHTTPClient implements HTTPClient {
public request(options: any): Promise<http.HttpResponse> {
......
import * as http from 'http';
import {ImageSource} from 'tns-core-modules/image-source'
import {Cookies,
NSHTTPClient,
Account,
Database,
ItemOperation,
Loan,
Item,
Form,
Hold} from '../../models';
import * as http from 'tns-core-modules/http';
import { ImageSource } from 'tns-core-modules/image-source'
import {
Cookies,
NSHTTPClient,
Account,
Database,
ItemOperation,
Loan,
Item,
Form,
Hold
} from '../../models';
export abstract class PortalAdapter {
private _http: any;
......@@ -20,13 +22,13 @@ export abstract class PortalAdapter {
}
public setHTTP(http:any): PortalAdapter {
public setHTTP(http: any): PortalAdapter {
this._http = http;
return this;
}
public updateAdapterHTTPClient(adapter:PortalAdapter): PortalAdapter {
public updateAdapterHTTPClient(adapter: PortalAdapter): PortalAdapter {
adapter.setHTTP(this._http);
return this;
}
......@@ -34,64 +36,64 @@ export abstract class PortalAdapter {
public signIn(account): Promise<any> {
this._cookies.clearCookies();
return this
._signIn(account)
.then( () => {
.then(() => {
return this.updateAccountInfo(account);
} );
});
}
public refresh(account): Promise<any> {
if (!account.isConnected()) {
account.setCookies(new Array<any>());
return this._signIn(account).then( () => {
return this._signIn(account).then(() => {
return this.refresh(account);
});
}
let db: Database = Database.current();
return this
return this
._fetchLoans(account)
.then( (loans) => {
.then((loans) => {
account.deleteLoans(db);
loans.forEach( (loan) => {
loans.forEach((loan) => {
db.save(loan.setAccount(account));
});
db.save(account);
})
.then( () => {
.then(() => {
return this._fetchHolds(account);
})
.then( (holds) => {
.then((holds) => {
account.deleteHolds(db);
holds.forEach( (hold) => {
holds.forEach((hold) => {
db.save(hold.setAccount(account));
});
account.setNumberOfHolds(holds.length);
db.save(account);
db.save(account);
})
}
public request(account: Account, options: any): Promise<http.HttpResponse> {
this._cookies.setCookies(account.getCookies());;
return this._http
.request(options)
.then( (response) => {
.then((response) => {
account.setCookies(this._cookies.getCookies());
return response;
} );
});
}
......@@ -119,7 +121,7 @@ export abstract class PortalAdapter {
return Promise.resolve();
}
public updateAccountInfo(account): Promise<any> {
return (0 < account.label.length)
? Promise.resolve()
......@@ -139,31 +141,31 @@ export abstract class PortalAdapter {
return Promise.resolve(account.credentials['login']);
};
protected _fetchHolds(account: Account): Promise<Array<Hold>> {
return Promise.resolve(new Array<Hold>());
}
protected _fetchThumbnails<T extends ItemOperation>(account: Account,
items: Array<T>): Promise<Array<T>> {
return Promise.all( items.map( (item) => {
items: Array<T>): Promise<Array<T>> {
return Promise.all(items.map((item) => {
return this._fetchThumbnail(account, item);
}))
}
protected _fetchThumbnail<T extends ItemOperation>(account: Account, item: T): Promise<T> {
if ( !item.getRecordThumbnail() )
if (!item.getRecordThumbnail())
return Promise.resolve(item);
return this
.request(account,
{
url: item.getRecordThumbnail(),
headers: { Referer: account.getUrl() }
})
.then( (response) => {
{
url: item.getRecordThumbnail(),
headers: { Referer: account.getUrl() }
})
.then((response) => {
if (response.statusCode == 301 || response.statusCode == 302) {
let location = <any>response.headers['Location']
item.setRecordThumbnail(location)
......@@ -173,10 +175,10 @@ export abstract class PortalAdapter {
if (response.statusCode != 200) {
return Promise.resolve(item)
}
return response.content
.toImage()
.then( (image_source: ImageSource) => {
.then((image_source: ImageSource) => {
return item.setRecordThumbnail('data:image/jpg;base64,' + image_source.toBase64String('jpg'));
})
})
......
import * as http from 'http';
import {PortalAdapter} from './adapter';
import {Database, Account, Hold, Loan, Record, Item} from '../../models';
import {WrongLoginPassword} from './wrong-login-password';
import * as http from 'tns-core-modules/http';
import { PortalAdapter } from './adapter';
import { Database, Account, Hold, Loan, Record, Item } from '../../models';
import { WrongLoginPassword } from './wrong-login-password';
export class InMediaV2 extends PortalAdapter {
......@@ -9,7 +9,7 @@ export class InMediaV2 extends PortalAdapter {
return 'inmedia-v2';
}
public getLabel(): string {
return 'InMedia (V2)';
}
......@@ -26,20 +26,22 @@ export class InMediaV2 extends PortalAdapter {
return this
.request(account,
{ url: account.getUrl() + '/in/rest/api/authenticate',
method: "POST",
content: "locale=fr&username=" + account.credentials['login'] + "&password=" + account.credentials['password'],
headers: {"Content-Type": "application/x-www-form-urlencoded"} })
.then( (response) => {
{
url: account.getUrl() + '/in/rest/api/authenticate',
method: "POST",
content: "locale=fr&username=" + account.credentials['login'] + "&password=" + account.credentials['password'],
headers: { "Content-Type": "application/x-www-form-urlencoded" }
})
.then((response) => {
return this._handleSignIn(account, response)
} )
})
}
protected _handleSignIn(account: Account, response: http.HttpResponse): Promise<any> {
if (response.statusCode == 401)
return Promise.reject(new WrongLoginPassword());
let datas = response.content.toJSON()
account.credentials['token'] = datas.token
account.beConnected()
......@@ -50,9 +52,11 @@ export class InMediaV2 extends PortalAdapter {
protected _fetchAccountSummary(account: Account): Promise<any> {
return this
.request(account,
{ url: account.getUrl() + '/in/rest/api/accountSummary',
headers: { authorization: 'Bearer ' + account.credentials['token'] }})
.then( (response) => {
{
url: account.getUrl() + '/in/rest/api/accountSummary',
headers: { authorization: 'Bearer ' + account.credentials['token'] }
})
.then((response) => {
return response.content.toJSON()
})
}
......@@ -61,7 +65,7 @@ export class InMediaV2 extends PortalAdapter {
protected _fetchAccountLabel(account: Account): Promise<string> {
return this
._fetchAccountSummary(account)
.then( (datas) => {
.then((datas) => {
return datas['name'];
})
}
......@@ -70,7 +74,7 @@ export class InMediaV2 extends PortalAdapter {
public updateCard(account: Account): Promise<any> {
return this
._fetchAccountSummary(account)
.then( (datas) => {
.then((datas) => {
let card = account.getCard()
card.setNumber(datas['subscriberId'])
card.setExpirationDate(this._formatDate(datas['expiryDate']))
......@@ -80,20 +84,22 @@ export class InMediaV2 extends PortalAdapter {
public requestUser(account: Account, action: string): Promise<any> {
return this
.request(account,
{ url: account.getUrl() + '/in/rest/api/accountPage?type=' + action,
headers: { authorization: 'Bearer ' + account.credentials['token'] }})
.then( (response) => {
.request(account,
{
url: account.getUrl() + '/in/rest/api/accountPage?type=' + action,
headers: { authorization: 'Bearer ' + account.credentials['token'] }
})
.then((response) => {
return response.content.toJSON();
});
}
public _fetchLoans(account: Account): Promise<Array<Loan>> {
return this
.requestUser(account, 'loans&pageNo=1&pageSize=100')
.then( (datas) => {
return datas['items'].map( (loan) => {
.then((datas) => {
return datas['items'].map((loan) => {
return new Loan()
.setLoanId(loan['data']['seqNo'])
.setTitle(loan['data']['title'])
......@@ -109,9 +115,9 @@ export class InMediaV2 extends PortalAdapter {
public _fetchHolds(account: Account): Promise<Array<Hold>> {
return this
.requestUser(account, 'reservations&pageNo=1&pageSize=100')
.then( (datas) => {
return datas['items'].map( (loan) => {
.then((datas) => {
return datas['items'].map((loan) => {
return new Hold()
.setHoldId(loan['data']['seqNo'])
.setTitle(loan['data']['title'])
......@@ -125,15 +131,17 @@ export class InMediaV2 extends PortalAdapter {
protected _formatDate(date: string): string {
return [date.substr(0,4),
date.substr(4,2),
date.substr(6,2)].join('-')
return [date.substr(0, 4),
date.substr(4, 2),
date.substr(6, 2)].join('-')
}
protected _holdStatus(code: string): string {
let codes = {RESV_NOT_AVAILABLE: L('hold_not_available'),
RESV_AVAILABLE: L('hold_available')};
let codes = {
RESV_NOT_AVAILABLE: L('hold_not_available'),
RESV_AVAILABLE: L('hold_available')
};
return codes[code] ? codes[code] : L('unknown');
}
}
declare var encodeURIComponent: any;
import * as http from 'http';
import {PortalAdapter} from './adapter';
import * as http from 'tns-core-modules/http';
import { PortalAdapter } from './adapter';
import * as cheerio from 'cheerio';
import {Account, Loan, Hold, Record, Cookies} from '../../models';
import {WrongLoginPassword} from './wrong-login-password';
import { Account, Loan, Hold, Record, Cookies } from '../../models';
import { WrongLoginPassword } from './wrong-login-password';
import * as UrlParse from 'url-parse';
export class OrpheeMedia extends PortalAdapter {
public getIdentifier(): string {
public getIdentifier(): string {
return 'orpheemedia';
}
......@@ -30,15 +30,15 @@ export class OrpheeMedia extends PortalAdapter {
public fetchNovelties(account: Account, page: number): Promise<Array<Record>> {
let fetch = () => {
return this
.request(account,
{ url: account.getUrl() + '?option=com_opac&view=nouveautes&limitstart=' + ((page -1) * 10) + '&limit=10'})
.then( (response) => {
let $ = cheerio.load(response.content.toString());
return $('tr[id*="row_ntc"]').length > 0
? this._parseNoveltiesV1(account, $)
: this._parseNoveltiesV2(account, $);
});
return this
.request(account,
{ url: account.getUrl() + '?option=com_opac&view=nouveautes&limitstart=' + ((page - 1) * 10) + '&limit=10' })
.then((response) => {
let $ = cheerio.load(response.content.toString());
return $('tr[id*="row_ntc"]').length > 0
? this._parseNoveltiesV1(account, $)
: this._parseNoveltiesV2(account, $);
});
}
return account.isConnected()
......@@ -55,16 +55,16 @@ export class OrpheeMedia extends PortalAdapter {
let thumbnail = row.find('div.couverture img').attr('src');
if (thumbnail.charAt(0) == '/')
thumbnail = this._baseUrl(account.getUrl()) + thumbnail;
let title = row.find('td:nth-child(2) a').text().trim();
let url = account.getUrl() + row.find('td:nth-child(2) a').attr('href');
let author = row.find('td:nth-child(3)').text().trim();
records.push( new Record()
.setUrl(url)
.setThumbnailUrl(thumbnail)
.setTitle(title)
.setAuthor(author));
records.push(new Record()
.setUrl(url)
.setThumbnailUrl(thumbnail)
.setTitle(title)
.setAuthor(author));
});
return records;
};
......@@ -78,16 +78,16 @@ export class OrpheeMedia extends PortalAdapter {
let thumbnail = row.find('div.vignette img').attr('src');
if (thumbnail.charAt(0) == '/')
thumbnail = this._baseUrl(account.getUrl()) + thumbnail;
let title = row.find('a.ntc-link-titre').text().trim();
let url = account.getUrl() + row.find('div.vignette a').attr('href');
let author = row.find('a.ntc-link-auteur').text().trim();
records.push( new Record()
.setUrl(url)
.setThumbnailUrl(thumbnail)
.setTitle(title)
.setAuthor(author));
records.push(new Record()
.setUrl(url)
.setThumbnailUrl(thumbnail)
.setTitle(title)
.setAuthor(author));
});
return records;
}
......@@ -101,8 +101,8 @@ export class OrpheeMedia extends PortalAdapter {
public searchUrl(account: Account, terms: string): Promise<string> {
return this
.request(account,
{ url: account.getUrl() + '?option=com_opac&view=rechsimple' })
.then( (response) => {
{ url: account.getUrl() + '?option=com_opac&view=rechsimple' })
.then((response) => {
let $ = cheerio.load(response.content.toString());
let form_action = this._baseUrl(account.getUrl()) + $('form').attr('action');
let form_data = this._extractForm($);
......@@ -114,16 +114,18 @@ export class OrpheeMedia extends PortalAdapter {
search_form['task'] = form_data['task'];
search_form['view'] = form_data['view'];
search_form['layout'] = form_data['layout'];
return this
.request(account,
{ url: account.getUrl(),
method: "POST",
dontFollowRedirects: true,
content: this._encodeForm(search_form),
headers: {"Content-Type": "application/x-www-form-urlencoded"} });
{
url: account.getUrl(),
method: "POST",
dontFollowRedirects: true,
content: this._encodeForm(search_form),
headers: { "Content-Type": "application/x-www-form-urlencoded" }
});
})
.then( (response) => {
.then((response) => {
return <string>response.headers['Location'];
});
}
......@@ -133,11 +135,11 @@ export class OrpheeMedia extends PortalAdapter {
return true;
}
public request(account: Account, options: any): Promise<http.HttpResponse> {
if (!options.headers)
options.headers={};
options.headers = {};
options.headers['User-Agent'] = 'Mozilla/5.0 (X11; Linux x86_64; rv:59.0) Gecko/20100101 Firefox/59.0';
return super.request(account, options);
}
......@@ -146,10 +148,10 @@ export class OrpheeMedia extends PortalAdapter {
protected _signIn(account): Promise<any> {
if (!(account.credentials['login'] && account.credentials['password']))
return Promise.reject(new WrongLoginPassword());
return this
.request(account, { url: account.getUrl() })
.then( (response) => { return this._handleLoginPage(account, response) });
.then((response) => { return this._handleLoginPage(account, response) });
}
......@@ -186,36 +188,40 @@ export class OrpheeMedia extends PortalAdapter {
login_data["Itemid"] = "0";
login_data["submit"] = "Ok";
login_data["ajs"] = "";
login_data["return"] =form_return;
login_data["return"] = form_return;
login_data[span_tok] = "1";
return this
.request(account,
{ url: account.getUrl(),
method: "POST",
content: this._encodeForm(login_data),
headers: {"Content-Type": "application/x-www-form-urlencoded" } })
.then( (response) => {
{
url: account.getUrl(),
method: "POST",
content: this._encodeForm(login_data),
headers: { "Content-Type": "application/x-www-form-urlencoded" }
})
.then((response) => {
return this._handleLoginResponse(account, response, account.getUrl())
} );
});
}
protected _handleLoginResponse(account:Account, response:http.HttpResponse, target_url: string): Promise<any> {
protected _handleLoginResponse(account: Account, response: http.HttpResponse, target_url: string): Promise<any> {
let $ = cheerio.load(response.content.toString());
if (0 < $('form#FormCAS').length) {
let url_cas = $('form').attr('action');
return this
.request(account,
{ url: url_cas,
method: "POST",
content: this._encodeForm(this._extractForm($)),
headers: {"Content-Type": "application/x-www-form-urlencoded"} })
.then( (response) => {
{
url: url_cas,
method: "POST",
content: this._encodeForm(this._extractForm($)),
headers: { "Content-Type": "application/x-www-form-urlencoded" }
})
.then((response) => {
return this._handleLoginResponse(account, response, url_cas);
} );
});
}
......@@ -225,21 +231,23 @@ export class OrpheeMedia extends PortalAdapter {
let host_url = $('link[href*=jsessionid]').attr('href');
let form_action = this._baseUrl(host_url ? host_url : target_url) + $('form').attr('action');
let form_data = this._extractForm($);
return this
.request(account,
{ url: form_action,
method: "POST",
content: this._encodeForm(form_data),
headers: {"Content-Type": "application/x-www-form-urlencoded"} })
.then( (response) => { return this._checkConnected(account, response); } );
{
url: form_action,
method: "POST",
content: this._encodeForm(form_data),
headers: { "Content-Type": "application/x-www-form-urlencoded" }
})
.then((response) => { return this._checkConnected(account, response); });
}
protected _extractForm($): any {
let form_data = [];
$('form input')
.filter( (i, elem) => { return $(elem).attr('name') != undefined; } )
.filter((i, elem) => { return $(elem).attr('name') != undefined; })
.each((i, elem) => {
let input = $(elem);
form_data[input.attr('name')] = input.attr('value');
......@@ -257,7 +265,7 @@ export class OrpheeMedia extends PortalAdapter {
protected _checkConnected(account, response): Promise<any> {
let $ = cheerio.load(response.content.toString());