forked from dexter21767/Trakt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtmdb.js
More file actions
64 lines (56 loc) · 2.08 KB
/
tmdb.js
File metadata and controls
64 lines (56 loc) · 2.08 KB
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
const axios = require('axios').default;
const config = require('./config')();
const NodeCache = require("node-cache");
const Cache = new NodeCache({ stdTTL: 3600, checkperiod: 600 });
const BaseURL = 'https://api.themoviedb.org/3';
const imagePath = 'https://image.tmdb.org/t/p/original/';
async function request(url, header) {
return await axios
.get(url, header, { timeout: 5000 })
.then(res => {
return res;
})
.catch(error => {
if (error.response) {
console.error('error on tmdb.js request:', error.response.status, error.response.statusText, error.config.url);
} else {
console.error(error);
}
});
}
async function getMeta(type, id) {
try {
console.log('getMeta', type, id)
const Cached = Cache.get(id)
if (Cached) return Cached
let data;
let meta = {};
if (typeof id == 'string' && id.startsWith('tt')) {
if (type == "movie") {
const url = `${BaseURL}/movie/${id}?api_key=${config.tmdb}`
const res = await request(url);
data = res.data
} else if (type == "series") {
const url = `${BaseURL}/find/${id}?api_key=${config.tmdb}&external_source=imdb_id`
const res = await request(url);
data = res.data.tv_results[0];
}
}
else {
type = type == 'series' ? 'tv' : 'movie';
const url = `${BaseURL}/${type}/${id}?api_key=${config.tmdb}`
const res = await request(url);
data = res.data;
}
if (!data) throw "error getting data"
if (data.backdrop_path) meta.background = imagePath + data.backdrop_path;
if (data.poster_path) meta.poster = imagePath + data.poster_path;
Cache.set(id, meta);
return meta
} catch (e) {
console.error(e)
}
}
//getMeta("movie", 786836).then(meta => (console.log(meta)))
//getMeta("series", 'tt0903747').then(meta => (console.log(meta)))
module.exports = getMeta;