get_html(my.POPULATION_URL, function(err, population_data) { if(err) { wcb_(err); } else { try{ population_data = population_data.substr(1); population_data = population_data.substr(0, population_data.length - 1); population_data = JSON.parse(population_data); population_data.splice(0, 3); fwk.async.each(JSON_DATA, function(olympic, ecb_) { console.log('populations', olympic.country, olympic.year); if(olympic.medal_count) { fwk.async.each(olympic.medal_count, function(country, ccb_) { country.population_year = olympic.year < 1960 ? 1960 : olympic.year; country.population = get_population(country.country_code, country.population_year, population_data); ccb_(); }, ecb_); } else { ecb_(); } }, wcb_); } catch(err) { wcb_(err); } } });
launch = function() { console.log('['.cyan + my.name.toUpperCase().cyan + ']'.cyan + ': Starting...'); fwk.async.parallel(that.todo(), function(err, results) { if(err) { return console.log('['.cyan + my.name.toUpperCase().cyan + ']'.cyan + ': ' + err.message); } else { for(var i in results) { if(results.hasOwnProperty(i)) { if(results[i].ok) { ok(results[i].description); } else { fail(results[i].description); } } } if(my.fail === 0) { console.log('['.cyan + my.name.toUpperCase().cyan + ']'.cyan + ': Done. ' + '(OK)'.green); } else { console.log('['.cyan + my.name.toUpperCase().cyan + ']'.cyan + ': Done. ' + '(FAIL)'.red); } } }); };
/* Get olympics medal counts */ function(wcb_) { fwk.async.each(JSON_DATA, function(olympic, ecb_) { get_html(olympic.url, function(err, html) { if(err) { ecb_(err); } else { console.log('medals', olympic.country, olympic.year); olympic.medal_count_url = get_medal_count_url(html); if(olympic.medal_count_url) { get_html(olympic.medal_count_url, function(err, html) { if(err) { ecb_(err); } else { console.log('count', olympic.country, olympic.year); olympic.medal_count = get_medal_count(html); ecb_(); } }); } else { ecb_(); } } }); }, wcb_); },
/* Compute population ranking */ function(wcb_) { fwk.async.each(JSON_DATA, function(olympic, ecb_) { console.log('compute', olympic.country, olympic.year); if(olympic.medal_count) { olympic.medal_count.forEach(function(country){ country.score = get_score(country.gold, country.silver, country.bronze, country.population); }); olympic.ranking = compute_ranking(olympic.medal_count); } ecb_(); }, wcb_); }
/* Get country image URLS */ function(wcb_) { fwk.async.each(JSON_DATA, function(olympic, ecb_) { get_html(olympic.country_url, function(err, html) { if(err) { ecb_(err); } else { console.log('country', olympic.country, olympic.year); olympic.country_image = get_country_image(html); ecb_(); } }); }, wcb_); },
fwk.async.each(JSON_DATA, function(olympic, ecb_) { console.log('populations', olympic.country, olympic.year); if(olympic.medal_count) { fwk.async.each(olympic.medal_count, function(country, ccb_) { country.population_year = olympic.year < 1960 ? 1960 : olympic.year; country.population = get_population(country.country_code, country.population_year, population_data); ccb_(); }, ecb_); } else { ecb_(); } }, wcb_);
main = function() { var JSON_DATA = {}; fwk.async.waterfall([ /* Get list of olympics */ function(wcb_) { get_html(my.OLYMPICS_URL, function(err, html) { if(err) { wcb_(err); } else { console.log('olympics'); JSON_DATA = get_olympics(html); wcb_(); } }); }, /* Get olympics image URLS */ function(wcb_) { fwk.async.each(JSON_DATA, function(olympic, ecb_) { get_html(olympic.url, function(err, html) { if(err) { ecb_(err); } else { console.log('image', olympic.country, olympic.year); olympic.image = get_olympic_image(html); ecb_(); } }); }, wcb_); }, /* Get country image URLS */ function(wcb_) { fwk.async.each(JSON_DATA, function(olympic, ecb_) { get_html(olympic.country_url, function(err, html) { if(err) { ecb_(err); } else { console.log('country', olympic.country, olympic.year); olympic.country_image = get_country_image(html); ecb_(); } }); }, wcb_); }, /* Get olympics medal counts */ function(wcb_) { fwk.async.each(JSON_DATA, function(olympic, ecb_) { get_html(olympic.url, function(err, html) { if(err) { ecb_(err); } else { console.log('medals', olympic.country, olympic.year); olympic.medal_count_url = get_medal_count_url(html); if(olympic.medal_count_url) { get_html(olympic.medal_count_url, function(err, html) { if(err) { ecb_(err); } else { console.log('count', olympic.country, olympic.year); olympic.medal_count = get_medal_count(html); ecb_(); } }); } else { ecb_(); } } }); }, wcb_); }, /* Get participating countries population at time of olympics */ function(wcb_) { get_html(my.POPULATION_URL, function(err, population_data) { if(err) { wcb_(err); } else { try{ population_data = population_data.substr(1); population_data = population_data.substr(0, population_data.length - 1); population_data = JSON.parse(population_data); population_data.splice(0, 3); fwk.async.each(JSON_DATA, function(olympic, ecb_) { console.log('populations', olympic.country, olympic.year); if(olympic.medal_count) { fwk.async.each(olympic.medal_count, function(country, ccb_) { country.population_year = olympic.year < 1960 ? 1960 : olympic.year; country.population = get_population(country.country_code, country.population_year, population_data); ccb_(); }, ecb_); } else { ecb_(); } }, wcb_); } catch(err) { wcb_(err); } } }); }, /* Compute population ranking */ function(wcb_) { fwk.async.each(JSON_DATA, function(olympic, ecb_) { console.log('compute', olympic.country, olympic.year); if(olympic.medal_count) { olympic.medal_count.forEach(function(country){ country.score = get_score(country.gold, country.silver, country.bronze, country.population); }); olympic.ranking = compute_ranking(olympic.medal_count); } ecb_(); }, wcb_); } ], function(err, result) { if(err) { console.log(err); process.exit(0); } else { fs.writeFile(my.OUTPUT, JSON.stringify(JSON_DATA), function(err) { if(err) { console.log(err); process.exit(0); } else { console.log('DONE.'); process.exit(1); } }); } }); };
}, function(err, user) { if(err) { /* DaTtSs */ factory.dattss().agg('routes.post_signup.error', '1c'); return res.error(err); } else if(user) { /* DaTtSs */ factory.dattss().agg('routes.post_signup.error', '1c'); return res.error(new Error('Email already in use')); } else { var uid = factory.hash([ email, Date.now().toString() ]); var key = factory.hash([ uid, Date.now().toString() ]); var user = { uid: uid, slt: factory.slt(uid), eml: email, key: key, dte: new Date() }; fwk.async.parallel({ email: function(cb_) { var url = 'http://' + factory.config()['DATTSS_DOMAIN'] + '/#/auth/password?code=' + key; var html = ''; html += '<div>'; html += 'Hi!<br/><br/>'; html += 'Welcome on DaTtSs, we just created your account!<br/><br/>'; html += 'Please follow this <a href="' + url + '">link</a> to '; html += 'activate it and set your password.<br/><br/>'; html += 'You can also manually enter the following code: <strong>'; html += key + '</strong><br/><br/>'; html += 'The Opensource SDK is available on our github: '; html += '<a href="https://github.com/teleportd/dattss">'; html += 'https://github.com/teleportd/dattss</a><br/><br/>'; html += 'For any problem or suggestion, please feel free to contact '; html += 'us at <a href="mailto:team@dattss.com">team@dattss.com</a> '; html += 'or join us at #dattss on irc.freenode.net<br/><br/>'; html += 'Best,<br/><br/>DaTtSs'; html += '</div>'; factory.email().send({ to: email, from: factory.config()['DATTSS_SENDGRID_FROM'], fromname: factory.config()['DATTSS_SENDGRID_FROMNAME'], subject: 'Confirm your account!', html: html }, function(success, message) { if(!success) { return cb_(new Error(message)); } else { return cb_(); } }); }, insert: function(cb_) { c_users.insert(user, function(err) { if(err) { return cb_(err); } else { return cb_(); } }); } }, function(err) { if(err) { /* DaTtSs */ factory.dattss().agg('routes.post_signup.error', '1c'); return res.error(err); } else { /* DaTtSs */ factory.dattss().agg('routes.post_signup.ok', '1c'); return res.data({ uid: uid }); } }); } });
}, function(err, user) { if(err) { /* DaTtSs */ factory.dattss().agg('routes.post_reset.error', '1c'); return res.error(err); } else if(!user) { /* DaTtSs */ factory.dattss().agg('routes.post_reset.ok', '1c'); res.ok(); } else { var key = factory.hash([ user.uid, Date.now().toString() ]); fwk.async.parallel({ email: function(cb_) { var url = 'http://' + factory.config()['DATTSS_DOMAIN'] + '/#/auth/password?code=' + key; var html = ''; html += '<div>'; html += 'Hi!<br/><br/>'; html += 'Please follow this <a href="' + url + '">link</a> to '; html += 'reset your password.<br/><br/>'; html += 'You can also manually enter the following code: <strong>'; html += key + '</strong><br/><br/>'; html += 'If you haven\'t requested a password reset, you can ignore '; html += 'this email. You current password is still valid and safe.'; html += '<br/><br/>'; html += 'For any problem or suggestion, please feel free to contact '; html += 'us at <a href="mailto:team@dattss.com">team@dattss.com</a> '; html += 'or join us at #dattss on irc.freenode.net<br/><br/>'; html += 'Best,<br/><br/>DaTtSs'; html += '</div>'; factory.email().send({ to: email, from: factory.config()['DATTSS_SENDGRID_FROM'], fromname: factory.config()['DATTSS_SENDGRID_FROMNAME'], subject: 'Reset your password!', html: html }, function(success, message) { if(!success) { return cb_(new Error(message)); } else { return cb_(); } }); }, user: function(cb_) { c_users.update({ uid: user.uid, slt: factory.slt(user.uid) }, { $set: { key: key } }, function(err) { if(err) { return cb_(err); } else { return cb_(); } }); } }, function(err) { if(err) { /* DaTtSs */ factory.dattss().agg('routes.post_reset.error', '1c'); return res.error(err); } else { /* DaTtSs */ factory.dattss().agg('routes.post_reset.ok', '1c'); return res.ok(); } }); } });