-
Notifications
You must be signed in to change notification settings - Fork 32
/
index.js
185 lines (172 loc) · 4.67 KB
/
index.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* main file for library and the all the functions are exported from these file
* and exported from here to user
* only functions with request are added to this file
* use functions which return the promise only
* */
const d3TimeFormat = require("d3-time-format").timeFormat;
const cheerio = require("cheerio"); // import cheerio for making use of css selector to get info
const {
request,
options,
clearCache,
stopCacheClear
} = require("./lib/request"); // importing request for making get request
const { ifError } = require("./lib/error"); // error file
const { getWinner } = require("./lib/awards"); // awards are provided
const { getCast, getPoster, changeQuality } = require("./lib/photo"); // poster and cast info is given by this function
const { getEpisodes } = require("./lib/episode");
const {
getRating,
getGenre,
getPro,
getStory,
getTitle,
getRuntime,
getYear,
getEpisodeCount,
getStars,
getSimilarMoviesById
} = require("./lib/data");
const { getTrending, getTrendingGenre } = require("./lib/trending"); // provide trending functions
const { search, searchActor, simpleSearch } = require("./lib/search"); // provide search functions
const { getUpcoming } = require("./lib/upcoming"); // provide upcoming movies
const { getActorData } = require("./lib/actor");
const getMonthDay = d3TimeFormat("%m-%d");
const BASE_URL = "https://www.imdb.com";
/**
* scrapper - the function to get meta data about movie or tvshow
*
* @param {type} id The id of movie or tv show
*
* @returns {Promise<Object>} the keys are
* title,runtime,year,story,writers|writer,producer|producers,
* director|directors,genre,poster,episodes,seasons,related
*/
function scrapper(id) {
return request(`${BASE_URL}/title/${id}/?ref_=nv_sr_1`)
.then(data => {
const $ = cheerio.load(data);
return {
...getTitle($),
...getRuntime($),
...getYear($),
...getStory($),
...getPro($),
...getGenre($),
...getRating($),
...getPoster($),
...getEpisodeCount($),
...getSimilarMoviesById($)
};
})
.catch(ifError);
} // combining all the low level api in the single one
/**
* awardsPage - get top three awards won by the movies
*
* @param {type} id id of the movie
*
* @returns {Promise<Array>} array contains object
*/
function awardsPage(id) {
return request(`${BASE_URL}/title/${id}/awards?ref_=tt_awd`)
.then(data => {
const $ = cheerio.load(data);
return [getWinner(4, $), getWinner(7, $), getWinner(10, $)];
})
.catch(ifError);
}
/**
* episodesPage - provides the epiosdes of particular series
*
* @param {String} id the id of movie or tv show
* @param {number} [season=1] Description
*
* @returns {Promise<Array>} array contains the object
*/
function episodesPage(id, season = 1) {
return request(`https://www.imdb.com/title/${id}/episodes?season=${season}`)
.then(data => {
const $ = cheerio.load(data);
return { ...getEpisodes($) };
})
.catch(ifError);
}
/**
* getStarsByBornDay - the function provide the days stars were borned
*
* @param {object} [date={}] the date which born stars are required
*
* @returns {Promise<Array>} the array contains the object
*/
function getStarsByBornDay(date = new Date()) {
const monthday = getMonthDay(date);
return request(
`${BASE_URL}/search/name?birth_monthday=${monthday}&refine=birth_monthday&ref_=nv_cel_brn`
)
.then(data => {
const $ = cheerio.load(data);
return getStars($);
})
.catch(ifError);
}
/**
* getStarsBornToday - get stars born today
*
* @returns {Promise<Array>} array contains the object
*/
function getStarsBornToday() {
return getStarsByBornDay(Date.now());
}
/**
* getFull - get all the details of a particular movie
*
* @param {type} id the id movie or the tv show
*
* @returns {Promise<object>}
*/
function getFull(id) {
return Promise.all([scrapper(id), awardsPage(id), getCast(id)])
.then(data => {
return { ...data[0], awards: data[1], ...data[2] };
})
.catch(ifError);
}
/**
* getActor - get actor detail
*
* @param {type} id of the actor
*
* @returns {Promise<Array>} the array contains the object
*/
function getActor(id) {
return request(`https://www.imdb.com/name/${id}/?ref_=tt_cl_t1`)
.then(data => {
const $ = cheerio.load(data);
return getActorData($);
})
.catch(ifError);
}
module.exports = {
scrapper,
getTrendingGenre,
getTrending,
search,
getFull,
getStarsByBornDay,
getStarsBornToday,
awardsPage,
episodesPage,
getCast,
getActor,
searchActor,
simpleSearch,
ifError,
request,
getUpcoming,
changeQuality,
options,
clearCache,
stopCacheClear
};