-
Notifications
You must be signed in to change notification settings - Fork 916
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add webcast link + youtube video id update script
- Loading branch information
1 parent
a13aace
commit b37df98
Showing
2 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* This script updates the youtube link and youtube video id for the next upcoming launch | ||
*/ | ||
|
||
const MongoClient = require('mongodb'); | ||
const cheerio = require('cheerio'); | ||
const request = require('request-promise-native'); | ||
const fuzz = require('fuzzball'); | ||
|
||
(async () => { | ||
let client; | ||
try { | ||
client = await MongoClient.connect(process.env.MONGO_URL, { useNewUrlParser: true }); | ||
} catch (err) { | ||
console.log(err.stack); | ||
process.exit(1); | ||
} | ||
|
||
const launch = client.db('spacex-api').collection('launch'); | ||
|
||
const data = await launch.find({ upcoming: true }).sort({ flight_number: 1 }).limit(1).toArray(); | ||
const { flight_number } = data[0]; | ||
const { mission_name } = data[0]; | ||
|
||
const result = await request('https://www.spacex.com/webcast'); | ||
const $ = cheerio.load(result); | ||
|
||
const embedSource = $('.left_column > font:nth-child(1) > iframe:nth-child(4)').attr('src'); | ||
const embedName = $('#page-title').text(); | ||
|
||
const youtube_url = embedSource.replace(/https:\/\/www\.youtube\.com\/embed/i, 'https://youtu.be'); | ||
const youtube_id = youtube_url.replace(/https:\/\/youtu\.be\//i, ''); | ||
|
||
const update = { | ||
'links.video_link': youtube_url, | ||
'links.youtube_id': youtube_id, | ||
}; | ||
|
||
const ratio = fuzz.ratio(embedName.replace(/mission/i, ''), mission_name.replace(/mission/i, '')); | ||
|
||
console.log(embedName); | ||
console.log(mission_name); | ||
console.log(update); | ||
console.log(ratio); | ||
|
||
// 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 | ||
if (ratio >= 50) { | ||
console.log('Match'); | ||
await launch.updateOne({ upcoming: true, flight_number }, { $set: { update } }); | ||
} | ||
|
||
if (client) { | ||
client.close(); | ||
} | ||
})(); |