Skip to content

Commit

Permalink
Merge branch 'release/1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
aespinilla committed Sep 9, 2021
2 parents d5406f0 + 7e23896 commit eac720a
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 159 deletions.
119 changes: 0 additions & 119 deletions faparser.js

This file was deleted.

9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "faparser",
"version": "1.0.9",
"version": "1.1.0",
"description": "Parser for Filmaffinity site",
"main": "faparser.js",
"main": "src/faparser.js",
"scripts": {
"test": "\"\""
},
Expand Down Expand Up @@ -30,8 +30,7 @@
},
"homepage": "https://github.com/aespinilla/faparser#readme",
"dependencies": {
"cheerio": "1.0.0-rc.2",
"promise": "^8.0.1",
"request": "^2.83.0"
"bent": "^7.3.12",
"cheerio": "1.0.0-rc.2"
}
}
71 changes: 71 additions & 0 deletions src/faparser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const parser = require('./parser')
const requestfa = require('./requestfa')

module.exports = {
film: film,
preview: preview,
search: search,
TITLE: 'TITLE',
GENRE: 'GENRE',
TOPIC: 'TOPIC',
DIRECTOR: 'DIRECTOR',
CAST: 'CAST'
}

async function search(data) {
data.isFilm = false
data.type = data.type || 'TITLE'
let res = await requestfa.FArequest(data)
res.lang = data.lang
res.type = data.type
return parser.parseSearch(res)
}

async function preview(data) {
data.isFilm = true
let result = await requestfa.FArequest(data)
const film = parser.parseFilm(result)
const filmResult = {
id: data.id,
url: film.url,
thumbnail: film.imageUrlMed.replace("mmed", "msmall"),
year: film.year,
title: film.title,
directors: film.directors,
cast: film.cast,
country: film.country,
rating: film.rating ? film.rating.replace(',', '.') : 0,
votes: film.votes
}
return filmResult
}

async function film(data) {
data.isFilm = true
let film = await filmTaskPromise(data)
return film
}

async function filmTaskPromise(data) {
const f = data
const t = clone(data)
t.isFilm = false
t.type = 'TRAILERS'
const i = clone(data)
i.isFilm = false
i.type = 'IMAGES'
const r = clone(data)
r.isFilm = false
r.type = 'PRO_REVIEWS'
let result = await Promise.all([requestfa.FArequest(f), requestfa.FArequest(i), requestfa.FArequest(t), requestfa.FArequest(r)])
const film = parser.parseFilm(result[0])
film.id = data.id
film.images = parser.parseImages(result[1])
film.trailers = parser.parseTrailers(result[2])
film.proReviews = parser.parseProReviews(result[3])
return film
}

function clone(o) {
return JSON.parse(JSON.stringify(o))
}
12 changes: 6 additions & 6 deletions parser.js → src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const jQuery = require('cheerio')
const url = require('url')

const BASE_URL = "http://www.filmaffinity.com"
const BASE_URL = "https://www.filmaffinity.com"

module.exports = {
parseFilm: parseFilm,
Expand Down Expand Up @@ -62,7 +62,7 @@ function parseFilm(data) {
case "país": {
film.country = {
imgCountry: BASE_URL + jQuery(a).next().find('img').attr('src'),
country: jQuery(a).next().find('img').attr('title'),
country: jQuery(a).next().find('img').attr('alt'),
}
break
}
Expand Down Expand Up @@ -193,7 +193,7 @@ function parseFilm(data) {
}

function parseSearch(data) {
const pathname = url.parse(data.response.request.uri.href).pathname;
const pathname = url.parse(data.url).pathname;
if (pathname.includes('film')) {
const idTemp = pathname.substring(pathname.indexOf('film') + 'film'.length, pathname.indexOf('.'));
data.response.lang = data.lang
Expand All @@ -203,7 +203,7 @@ function parseSearch(data) {
count: 1,
result: [{
id: idTemp,
url: data.response.request.uri.href,
url: data.url,
thumbnail: film.imageUrlMed.replace("mmed", "msmall"),
year: film.year,
title: film.title,
Expand Down Expand Up @@ -238,7 +238,7 @@ function parseSearch(data) {
filmview.url = relUrl;
filmview.country = {
imgCountry: BASE_URL + jQuery(a).find('.mc-title').find('img').attr('src'),
country: jQuery(a).find('.mc-title').find('img').attr('title')
country: jQuery(a).find('.mc-title').find('img').attr('alt')
}
if (jQuery(a).hasClass('mt')) {
year = jQuery(a).find('.ye-w').text();
Expand Down Expand Up @@ -368,7 +368,7 @@ function parseSpecialSearch(data) {
f.title = titleHtml.find('a').attr('title').trim()
f.country = {
imgCountry: BASE_URL + titleHtml.find('img').attr('src'),
country: titleHtml.find('img').attr('title')
country: titleHtml.find('img').attr('alt')
}
f.year = titleHtml.text().substring(f.title.length + 2).replace(")", "").trim()
f.directors = []
Expand Down
56 changes: 27 additions & 29 deletions requestfa.js → src/requestfa.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/**
* Created by aespinilla on 20/6/17.
*/
const request = require('request')
const Promise = require('promise')
const bent = require('bent')

module.exports = {
FArequest: rPromise
FArequest: requestServer
}

const BASE_URL = "http://www.filmaffinity.com"
const BASE_URL = "https://www.filmaffinity.com"

const searchTypes = {
TITLE: "title",
Expand All @@ -21,28 +20,26 @@ const searchTypes = {
PRO_REVIEWS: "/pro-reviews.php?movie-id=",
}

function rPromise(data) {
return new Promise(function (resolve, reject) {
const url = computedUrl(data)
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
resolve({
url: url,
response: response,
type: data.type,
isFilm: data.isFilm,
lang: data.lang,
body: body
})
} else {
reject({
code: response.statusCode,
url: url,
error: response.error
})
}
})
})
async function requestServer(data) {
const url = computedUrl(data)
const get = bent(url)
try {
let getResult = await get()
const body = await getResult.text()
return {
url: url,
type: data.type,
isFilm: data.isFilm,
lang: data.lang,
body: body
}
} catch (error) {
throw {
code: error.statusCode,
url: url,
error: error.message
}
}
}

function computedUrl(data) {
Expand All @@ -53,18 +50,19 @@ function computedUrl(data) {
const type = data.type && (searchTypes.hasOwnProperty(data.type)) ? data.type : searchTypes.TITLE
const query = data.query
const start = data.start ? data.start : 0
const orderBy = (typeof data.orderByYear === 'undefined' || (data.orderByYear !== 'undefined' && data.orderByYear === true)) ? '&orderby=year' : ''
let computedUrl = BASE_URL + '/' + lang
if (type === 'CAST' || type === 'DIRECTOR') {
computedUrl = computedUrl + '/search.php?stype=' + searchTypes[type] + '&sn'
computedUrl = computedUrl + '&stext=' + encodeURIComponent(query) + '&from=' + start + '&orderby=year'
computedUrl = computedUrl + '&stext=' + encodeURIComponent(query) + '&from=' + start + orderBy
} else if (type === 'GENRE' || type === 'TOPIC') {
computedUrl = computedUrl + searchTypes[type] + query + '&attr=rat_count&nodoc'
} else if (type === 'IMAGES' || type === 'TRAILERS' || type === 'PRO_REVIEWS') {
computedUrl = computedUrl + searchTypes[type] + data.id
} else {
computedUrl = computedUrl + '/search.php?stype=' + searchTypes[type]
computedUrl = computedUrl + '&stext=' + encodeURIComponent(query) + '&from=' + start + '&orderby=year'
computedUrl = computedUrl + '&stext=' + encodeURIComponent(query) + '&from=' + start + orderBy
}
console.info('[' + new Date() + '] faparser: ' + 'Generated URL: ' + computedUrl)
//console.info('[' + new Date() + '] faparser: ' + 'Generated URL: ' + computedUrl)
return computedUrl.toLowerCase()
}

0 comments on commit eac720a

Please sign in to comment.