forked from aespinilla/faparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
faparser.js
119 lines (111 loc) · 3.76 KB
/
faparser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const parser = require('./parser')
const requestfa = require('./requestfa')
const Promise = require('promise')
module.exports = {
film: film,
preview: preview,
search: search,
TITLE: 'TITLE',
GENRE: 'GENRE',
TOPIC: 'TOPIC',
DIRECTOR: 'DIRECTOR',
CAST: 'CAST'
}
function search(data) {
return new Promise(function (resolve, reject) {
const now = new Date()
data.isFilm = false
data.type = data.type || 'TITLE'
requestfa.FArequest(data).then(function (res) {
res.lang = data.lang
res.type = data.type
const result = parser.parseSearch(res)
log(now, data, result.result)
return resolve(result)
}).catch(function (error) {
console.error('[' + new Date() + '] faparser: ' + JSON.stringify(error))
log(now, data, {error: error})
return reject({
code: error.code,
error: error.error
})
})
})
}
function preview(data) {
return new Promise(function (resolve, reject) {
const now = new Date()
data.isFilm = true
requestfa.FArequest(data).then(function (result) {
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
}
log(now, data, film)
return resolve(filmResult)
}).catch(function (error) {
console.error('[' + new Date() + '] faparser: ' + JSON.stringify(error))
log(now, data, {error: error})
return reject({
code: error.code,
error: error.error
})
})
})
}
function film(data) {
return new Promise(function (resolve, reject) {
const now = new Date()
data.isFilm = true
filmTaskPromise(data).then(function (film) {
log(now, data, film)
return resolve(film)
}).catch(function (error) {
console.error('[' + new Date() + '] faparser: ' + JSON.stringify(error))
log(now, data, {error: error})
return reject({
code: error.code,
error: error.error
})
})
})
}
function filmTaskPromise(data) {
return new Promise(function (resolve, reject) {
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'
Promise.all([requestfa.FArequest(f), requestfa.FArequest(i), requestfa.FArequest(t), requestfa.FArequest(r)])
.then(function (result) {
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 resolve(film)
}).catch(reject)
})
}
function clone(o) {
return JSON.parse(JSON.stringify(o))
}
function log(now, data, response) {
const diff = new Date().getTime() - now.getTime()
console.info('[' + new Date() + '] faparser: ' + 'Process time: ' + diff + 'ms for request data: ' + JSON.stringify(data) + ' with response: ' + JSON.stringify(response))
}