Skip to content

Commit

Permalink
Add webcast link + youtube video id update script
Browse files Browse the repository at this point in the history
  • Loading branch information
jakewmeyer committed Jan 13, 2019
1 parent a13aace commit b37df98
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"launchpad": "node scripts/launchpad.js",
"landpad": "node scripts/landpad.js",
"cores": "node scripts/cores.js",
"capsules": "node scripts/capsules.js"
"capsules": "node scripts/capsules.js",
"webcast": "node scripts/webcast.js"
},
"repository": {
"type": "git",
Expand Down
59 changes: 59 additions & 0 deletions scripts/webcast.js
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();
}
})();

0 comments on commit b37df98

Please sign in to comment.