forked from r-spacex/SpaceX-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebcast.js
120 lines (108 loc) · 3.25 KB
/
webcast.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
const got = require('got');
const cheerio = require('cheerio');
const fuzz = require('fuzzball');
const { logger } = require('../middleware/logger');
const SPACEX_WEBCAST = 'https://www.spacex.com/webcast';
const SPACEX_API = 'https://stage.spacexdata.com/v4';
const KEY = process.env.SPACEX_KEY;
const HEALTHCHECK = process.env.WEBCAST_HEALTHCHECK;
/**
* Check for new SpaceX webcast links
* @return {Promise<void>}
*/
module.exports = async () => {
try {
const launches = await got.post(`${SPACEX_API}/launches/query`, {
json: {
query: {
upcoming: true,
},
options: {
sort: {
flight_number: 'asc',
},
limit: 1,
},
},
resolveBodyOnly: true,
responseType: 'json',
});
const launchId = launches.docs[0].id;
const missionName = launches.docs[0].name;
// Get most recent launch youtube link
const previousLaunches = await got.post(`${SPACEX_API}/launches/query`, {
json: {
query: {
upcoming: false,
},
options: {
sort: {
flight_number: 'desc',
},
limit: 1,
},
},
resolveBodyOnly: true,
responseType: 'json',
});
const prevYoutubeUrl = previousLaunches.docs[0].links.webcast;
const response = await got(SPACEX_WEBCAST);
const $ = cheerio.load(response.body);
const embedSource = $('#content > div.left_column > font > iframe').attr('src');
const embedName = $('#page-title').text();
const youtubeUrl = embedSource.replace(/https:\/\/www\.youtube\.com\/embed/i, 'https://youtu.be');
const youtubeId = youtubeUrl.replace(/https:\/\/youtu\.be\//i, '');
const update = {
'links.video_link': youtubeUrl,
'links.youtube_id': youtubeId,
};
const ratio = fuzz.ratio(embedName.replace(/mission/i, ''), missionName.replace(/mission/i, ''));
// Check if most recent launch matches
// Prevents early triggering for extremely similar launch names
if (prevYoutubeUrl === youtubeUrl) {
logger.info({
rawMission: embedName,
apiMission: missionName,
update,
matchRatio: ratio,
match: false,
matchesRecent: true,
});
// Might need to play with this ratio, but 50% match should be good enough to
// reasonably assume it's the correct mission. Worst case, if it doesn't pick it
// up correctly, the data would be entered regardless, this script is purely for convenience
} else if (ratio >= 50) {
logger.info({
rawMission: embedName,
apiMission: missionName,
update,
matchRatio: ratio,
match: true,
matchesRecent: false,
});
await got.patch(`${SPACEX_API}/launches/${launchId}`, {
json: {
'links.webcast': youtubeUrl,
'links.youtube_id': youtubeId,
},
headers: {
'spacex-key': KEY,
},
});
} else {
logger.info({
rawMission: embedName,
apiMission: missionName,
update,
matchRatio: ratio,
match: false,
matchesRecent: false,
});
}
if (HEALTHCHECK) {
await got(HEALTHCHECK);
}
} catch (error) {
console.log(error);
}
};