Skip to content

Commit eac720a

Browse files
committed
Merge branch 'release/1.1.0'
2 parents d5406f0 + 7e23896 commit eac720a

File tree

5 files changed

+108
-159
lines changed

5 files changed

+108
-159
lines changed

faparser.js

-119
This file was deleted.

package.json

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "faparser",
3-
"version": "1.0.9",
3+
"version": "1.1.0",
44
"description": "Parser for Filmaffinity site",
5-
"main": "faparser.js",
5+
"main": "src/faparser.js",
66
"scripts": {
77
"test": "\"\""
88
},
@@ -30,8 +30,7 @@
3030
},
3131
"homepage": "https://github.com/aespinilla/faparser#readme",
3232
"dependencies": {
33-
"cheerio": "1.0.0-rc.2",
34-
"promise": "^8.0.1",
35-
"request": "^2.83.0"
33+
"bent": "^7.3.12",
34+
"cheerio": "1.0.0-rc.2"
3635
}
3736
}

src/faparser.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
const parser = require('./parser')
2+
const requestfa = require('./requestfa')
3+
4+
module.exports = {
5+
film: film,
6+
preview: preview,
7+
search: search,
8+
TITLE: 'TITLE',
9+
GENRE: 'GENRE',
10+
TOPIC: 'TOPIC',
11+
DIRECTOR: 'DIRECTOR',
12+
CAST: 'CAST'
13+
}
14+
15+
async function search(data) {
16+
data.isFilm = false
17+
data.type = data.type || 'TITLE'
18+
let res = await requestfa.FArequest(data)
19+
res.lang = data.lang
20+
res.type = data.type
21+
return parser.parseSearch(res)
22+
}
23+
24+
async function preview(data) {
25+
data.isFilm = true
26+
let result = await requestfa.FArequest(data)
27+
const film = parser.parseFilm(result)
28+
const filmResult = {
29+
id: data.id,
30+
url: film.url,
31+
thumbnail: film.imageUrlMed.replace("mmed", "msmall"),
32+
year: film.year,
33+
title: film.title,
34+
directors: film.directors,
35+
cast: film.cast,
36+
country: film.country,
37+
rating: film.rating ? film.rating.replace(',', '.') : 0,
38+
votes: film.votes
39+
}
40+
return filmResult
41+
}
42+
43+
async function film(data) {
44+
data.isFilm = true
45+
let film = await filmTaskPromise(data)
46+
return film
47+
}
48+
49+
async function filmTaskPromise(data) {
50+
const f = data
51+
const t = clone(data)
52+
t.isFilm = false
53+
t.type = 'TRAILERS'
54+
const i = clone(data)
55+
i.isFilm = false
56+
i.type = 'IMAGES'
57+
const r = clone(data)
58+
r.isFilm = false
59+
r.type = 'PRO_REVIEWS'
60+
let result = await Promise.all([requestfa.FArequest(f), requestfa.FArequest(i), requestfa.FArequest(t), requestfa.FArequest(r)])
61+
const film = parser.parseFilm(result[0])
62+
film.id = data.id
63+
film.images = parser.parseImages(result[1])
64+
film.trailers = parser.parseTrailers(result[2])
65+
film.proReviews = parser.parseProReviews(result[3])
66+
return film
67+
}
68+
69+
function clone(o) {
70+
return JSON.parse(JSON.stringify(o))
71+
}

parser.js src/parser.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
const jQuery = require('cheerio')
55
const url = require('url')
66

7-
const BASE_URL = "http://www.filmaffinity.com"
7+
const BASE_URL = "https://www.filmaffinity.com"
88

99
module.exports = {
1010
parseFilm: parseFilm,
@@ -62,7 +62,7 @@ function parseFilm(data) {
6262
case "país": {
6363
film.country = {
6464
imgCountry: BASE_URL + jQuery(a).next().find('img').attr('src'),
65-
country: jQuery(a).next().find('img').attr('title'),
65+
country: jQuery(a).next().find('img').attr('alt'),
6666
}
6767
break
6868
}
@@ -193,7 +193,7 @@ function parseFilm(data) {
193193
}
194194

195195
function parseSearch(data) {
196-
const pathname = url.parse(data.response.request.uri.href).pathname;
196+
const pathname = url.parse(data.url).pathname;
197197
if (pathname.includes('film')) {
198198
const idTemp = pathname.substring(pathname.indexOf('film') + 'film'.length, pathname.indexOf('.'));
199199
data.response.lang = data.lang
@@ -203,7 +203,7 @@ function parseSearch(data) {
203203
count: 1,
204204
result: [{
205205
id: idTemp,
206-
url: data.response.request.uri.href,
206+
url: data.url,
207207
thumbnail: film.imageUrlMed.replace("mmed", "msmall"),
208208
year: film.year,
209209
title: film.title,
@@ -238,7 +238,7 @@ function parseSearch(data) {
238238
filmview.url = relUrl;
239239
filmview.country = {
240240
imgCountry: BASE_URL + jQuery(a).find('.mc-title').find('img').attr('src'),
241-
country: jQuery(a).find('.mc-title').find('img').attr('title')
241+
country: jQuery(a).find('.mc-title').find('img').attr('alt')
242242
}
243243
if (jQuery(a).hasClass('mt')) {
244244
year = jQuery(a).find('.ye-w').text();
@@ -368,7 +368,7 @@ function parseSpecialSearch(data) {
368368
f.title = titleHtml.find('a').attr('title').trim()
369369
f.country = {
370370
imgCountry: BASE_URL + titleHtml.find('img').attr('src'),
371-
country: titleHtml.find('img').attr('title')
371+
country: titleHtml.find('img').attr('alt')
372372
}
373373
f.year = titleHtml.text().substring(f.title.length + 2).replace(")", "").trim()
374374
f.directors = []

requestfa.js src/requestfa.js

+27-29
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
/**
22
* Created by aespinilla on 20/6/17.
33
*/
4-
const request = require('request')
5-
const Promise = require('promise')
4+
const bent = require('bent')
65

76
module.exports = {
8-
FArequest: rPromise
7+
FArequest: requestServer
98
}
109

11-
const BASE_URL = "http://www.filmaffinity.com"
10+
const BASE_URL = "https://www.filmaffinity.com"
1211

1312
const searchTypes = {
1413
TITLE: "title",
@@ -21,28 +20,26 @@ const searchTypes = {
2120
PRO_REVIEWS: "/pro-reviews.php?movie-id=",
2221
}
2322

24-
function rPromise(data) {
25-
return new Promise(function (resolve, reject) {
26-
const url = computedUrl(data)
27-
request(url, function (error, response, body) {
28-
if (!error && response.statusCode == 200) {
29-
resolve({
30-
url: url,
31-
response: response,
32-
type: data.type,
33-
isFilm: data.isFilm,
34-
lang: data.lang,
35-
body: body
36-
})
37-
} else {
38-
reject({
39-
code: response.statusCode,
40-
url: url,
41-
error: response.error
42-
})
43-
}
44-
})
45-
})
23+
async function requestServer(data) {
24+
const url = computedUrl(data)
25+
const get = bent(url)
26+
try {
27+
let getResult = await get()
28+
const body = await getResult.text()
29+
return {
30+
url: url,
31+
type: data.type,
32+
isFilm: data.isFilm,
33+
lang: data.lang,
34+
body: body
35+
}
36+
} catch (error) {
37+
throw {
38+
code: error.statusCode,
39+
url: url,
40+
error: error.message
41+
}
42+
}
4643
}
4744

4845
function computedUrl(data) {
@@ -53,18 +50,19 @@ function computedUrl(data) {
5350
const type = data.type && (searchTypes.hasOwnProperty(data.type)) ? data.type : searchTypes.TITLE
5451
const query = data.query
5552
const start = data.start ? data.start : 0
53+
const orderBy = (typeof data.orderByYear === 'undefined' || (data.orderByYear !== 'undefined' && data.orderByYear === true)) ? '&orderby=year' : ''
5654
let computedUrl = BASE_URL + '/' + lang
5755
if (type === 'CAST' || type === 'DIRECTOR') {
5856
computedUrl = computedUrl + '/search.php?stype=' + searchTypes[type] + '&sn'
59-
computedUrl = computedUrl + '&stext=' + encodeURIComponent(query) + '&from=' + start + '&orderby=year'
57+
computedUrl = computedUrl + '&stext=' + encodeURIComponent(query) + '&from=' + start + orderBy
6058
} else if (type === 'GENRE' || type === 'TOPIC') {
6159
computedUrl = computedUrl + searchTypes[type] + query + '&attr=rat_count&nodoc'
6260
} else if (type === 'IMAGES' || type === 'TRAILERS' || type === 'PRO_REVIEWS') {
6361
computedUrl = computedUrl + searchTypes[type] + data.id
6462
} else {
6563
computedUrl = computedUrl + '/search.php?stype=' + searchTypes[type]
66-
computedUrl = computedUrl + '&stext=' + encodeURIComponent(query) + '&from=' + start + '&orderby=year'
64+
computedUrl = computedUrl + '&stext=' + encodeURIComponent(query) + '&from=' + start + orderBy
6765
}
68-
console.info('[' + new Date() + '] faparser: ' + 'Generated URL: ' + computedUrl)
66+
//console.info('[' + new Date() + '] faparser: ' + 'Generated URL: ' + computedUrl)
6967
return computedUrl.toLowerCase()
7068
}

0 commit comments

Comments
 (0)