-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrapeAMCSeries.js
198 lines (174 loc) · 7.03 KB
/
scrapeAMCSeries.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
186
187
188
189
190
191
192
193
194
195
196
197
198
const rp = require('request-promise');
const cheerio = require('cheerio');
const go = async (url) => {
const options = {
uri: url,
transform: function (body) {
return cheerio.load(body);
}
}
try {
const $ = await rp(options);
function ObjectLength(object) {
var length = 0;
for (var key in object) {
if (object.hasOwnProperty(key)) {
++length;
}
}
return length;
};
// setting date for capture date field
const dateCapture = new Date();
var dateOptions = {
year: "numeric",
month: "2-digit",
day: "numeric"
};
// variables for scraping data
const seriesProduction = 'AMC';
let title = '';
title = $('div[class=title]').text() !== '' ? $('div[class=title]').text() :
($('h1[itemprop=name]').text() !== '' ? $('h1[itemprop=name]').text() : $('meta[property="og:title"]')[0].attribs.content);
const seriesData = $('script')[1].childNodes[0].data;
const seriesHelper = seriesData.split('= ');
const jsonParse = JSON.parse(seriesHelper[1]);
const episodeHelper = jsonParse.modules.match.params.episode.split('-');
const seriesEpisodeNo = parseInt(episodeHelper[1].trim());
const seriesSeasonNo = parseInt(jsonParse.modules.match.params.season.trim());
const seriesUrl = 'https://www.amc.com/shows/' + jsonParse.modules.match.params.show.trim();
const episodeMeta = jsonParse.modules.byId;
const obLength = ObjectLength(episodeMeta);
let currentEpisodePartialMeta = {}, sourceId = '', contentRating = '', seriesSourceId = '', seasonSourceId = '',
offerType = '', runtime = '', seriesTitle = '';
if (episodeMeta.hasOwnProperty('episode')) {
switch (obLength) {
case 6:
currentEpisodePartialMeta = episodeMeta.episode_feed_3;
break;
case 7:
currentEpisodePartialMeta = episodeMeta.episode_feed_4;
break;
case 8:
currentEpisodePartialMeta = episodeMeta.episode_feed_5;
break;
case 9:
currentEpisodePartialMeta = episodeMeta.episode_feed_6;
break;
case 10:
currentEpisodePartialMeta = episodeMeta.episode_feed_7;
break;
default:
currentEpisodePartialMeta = episodeMeta.episode_feed_2;
break;
}
if (currentEpisodePartialMeta.hasOwnProperty('posts')) {
const sID_step1 = seriesData.split('"amcn_field_video_pid":"');
if (sID_step1.length > 1) {
const sID_step2 = sID_step1[1].split('","');
sourceId = sID_step2[0];
} else {
sourceId = 'N/A';
}
contentRating = currentEpisodePartialMeta.posts[0].attached.meta.amcn_field_video_tv_content_rating;
seriesSourceId = parseInt(currentEpisodePartialMeta.posts[0].attached.meta.amcn_field_relation_show_id);
seasonSourceId = parseInt(currentEpisodePartialMeta.posts[0].attached.meta.amcn_field_relation_season_id);
offerType = currentEpisodePartialMeta.posts[0].attached.meta.amcn_field_video_category === 'TVE-Unauth' ? 'FREE' : 'TVE';
runtime = parseInt(currentEpisodePartialMeta.posts[0].attached.meta.amcn_field_video_end_credits_start) || '';
seriesTitle = currentEpisodePartialMeta.posts[0].attached.meta.amcn_field_relation_show_display;
} else {
const sID_step1 = seriesData.split('"amcn_field_video_pid":"');
const serSID_step1 = seriesData.split('"amcn_field_relation_show_id":"');
const serSID_step2 = serSID_step1[serSID_step1.length - 1].split('","');
const seaSID_step1 = seriesData.split('"amcn_field_relation_season_id":"');
const seaSID_step2 = seaSID_step1[seaSID_step1.length - 1].split('","');
if (sID_step1.length > 1) {
const sID_step2 = sID_step1[1].split('","');
sourceId = sID_step2[0];
const extraJSONURL = `https://link.theplatform.com/s/M_UwQC/media/${sourceId}?format=preview`;
const extraData = await rp(extraJSONURL);
const jsonDATA = JSON.parse(extraData);
if (jsonDATA.ratings.length > 0) {
contentRating = jsonDATA.ratings[0].rating;
}
seriesSourceId = parseInt(serSID_step2[0]);
seasonSourceId = parseInt(seaSID_step2[0]);
offerType = jsonDATA.amc$videoCategory === 'TVE-Unauth' ? 'FREE' : 'TVE';
runtime = Math.trunc(parseInt(jsonDATA.duration) / 1000) || '';
seriesTitle = jsonDATA.amc$show;
} else {
sourceId = 'N/A';
}
}
} else {
const sID_step1 = seriesData.split('"amcn_field_video_pid":"');
const sID_step2 = sID_step1[1].split('","');
const serSID_step1 = seriesData.split('"amcn_field_relation_show_id":"');
const serSID_step2 = serSID_step1[serSID_step1.length - 1].split('","');
const seaSID_step1 = seriesData.split('"amcn_field_relation_season_id":"');
const seaSID_step2 = seaSID_step1[seaSID_step1.length - 1].split('","');
sourceId = sID_step2[0];
const extraJSONURL = `https://link.theplatform.com/s/M_UwQC/media/${sourceId}?format=preview`;
const extraData = await rp(extraJSONURL);
const jsonDATA = JSON.parse(extraData);
if (jsonDATA.ratings.length > 0) {
contentRating = jsonDATA.ratings[0].rating;
}
seriesSourceId = parseInt(serSID_step2[0]);
seasonSourceId = parseInt(seaSID_step2[0]);
offerType = jsonDATA.amc$videoCategory === 'TVE-Unauth' ? 'FREE' : 'TVE';
runtime = Math.trunc(parseInt(jsonDATA.duration) / 1000) || '';
seriesTitle = jsonDATA.amc$show;
}
// object for each series data
const seriesList = {
'bot_system': '',
'bot_version': '1.0.0',
'bot_country': 'us',
'capture_date': '',
'offer_type': '',
'purchase_type': '',
'picture_quality': '',
'program_price': '',
'bundle_price': '',
'currency': '',
'addon_name': '',
'is_movie': 0,
'season_number': '',
'episode_number': '',
'title': '',
'genre': '',
'source_id': '',
'program_url': '',
'maturity_rating': '',
'release_date': '',
'viewable_runtime': '',
'series_title': '',
'series_source_id': '',
'series_url': '',
'series_genre': '',
'season_source_id': ''
};
// assign data for object series
seriesList.bot_system = seriesProduction;
seriesList.capture_date = dateCapture.toLocaleString('en', dateOptions);
seriesList.offer_type = offerType;
seriesList.season_number = seriesSeasonNo;
seriesList.episode_number = seriesEpisodeNo;
seriesList.title = title;
seriesList.source_id = sourceId;
seriesList.program_url = url;
seriesList.maturity_rating = contentRating;
seriesList.viewable_runtime = runtime;
seriesList.series_title = seriesTitle;
seriesList.series_source_id = seriesSourceId;
seriesList.series_url = seriesUrl;
seriesList.season_source_id = seasonSourceId;
return seriesList;
} catch (e) {
console.log(e);
}
};
module.exports = {
start: go
};