{"size": 5241, "ext": "js", "max_stars_repo_path": "server/lib/jobs/hephystoParser.js", "max_stars_repo_name": "Uni-Heidelberg/Uni-HD-App-Server", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server/lib/jobs/hephystoParser.js", "max_issues_repo_name": "Uni-Heidelberg/Uni-HD-App-Server", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server/lib/jobs/hephystoParser.js", "max_forks_repo_name": "Uni-Heidelberg/Uni-HD-App-Server", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 46.7946428571, "max_line_length": 116, "alphanum_fraction": 0.2911658081}
'use strict';

var request = require('request');
var async = require('async');

var md = require('html-md');

var nullOrString = function (testStr) {
    return testStr.length > 0 ? testStr : null;
};

module.exports = function (agenda, app) {
    var NewsSource = app.models.NewsSource;
    var NewsTalk = app.models.NewsTalk;

    agenda.define(
        'parse hephysto',
        function (job, done) {
            console.log('Starting parsing hephysto data');

            NewsSource.find(
                {
                    'where': {
                        'type': 'hephysto'
                    }
                },
                function (err, hephystos) {
                    if (err) {
                        return done(err);
                    }
                    async.each(
                        hephystos,
                        function (hephysto, callback) {
                            if (!(hephysto.options && hephysto.options.hephysto && hephysto.options.hephysto.url)) {
                                return callback('invalid hephysto source');
                            }
                            request(
                                {
                                    url: hephysto.options.hephysto.url,
                                    json: true
                                },
                                function (err, response, body) {
                                    if (err || response.statusCode !== 200) {
                                        return callback(err);
                                    }
                                    async.each(
                                        body,
                                        function (hephystoTalk, callback) {
                                            var talkData = {
                                                title: hephystoTalk.title,
                                                date: hephystoTalk.date,
                                                abstract: nullOrString(hephystoTalk.abstract),
                                                building: md(hephystoTalk.building),
                                                room: md(hephystoTalk.room),
                                                url: hephystoTalk.url,

                                                source: hephysto,

                                                speakerName: nullOrString(hephystoTalk.speaker.name),
                                                speakerAffiliation: nullOrString(hephystoTalk.speaker.affiliation),
                                                speakerUrl: nullOrString(hephystoTalk.speaker.url),
                                                speakerEmail: nullOrString(hephystoTalk.speaker.email)
                                            };

                                            talkData.urlHash =
                                                'hephysto:' + hephystoTalk.talkSeriesId + ':' + hephystoTalk.talkId;

                                            NewsTalk.findOrCreate(
                                                {
                                                    where: {
                                                        urlHash: talkData.urlHash
                                                    }
                                                },
                                                talkData,
                                                function (err, talk) {
                                                    if (err) {
                                                        return callback(err);
                                                    }

                                                    talk.title = talkData.title;
                                                    talk.date = talkData.date;
                                                    talk.abstract = talkData.abstract;
                                                    talk.building = talkData.building;
                                                    talk.room = talkData.room;
                                                    talk.url = talkData.url;

                                                    talk.speakerName = talkData.speakerName;
                                                    talk.speakerAffiliation = talkData.speakerAffiliation;
                                                    talk.speakerUrl = talkData.speakerUrl;
                                                    talk.speakerEmail = talkData.speakerEmail;

                                                    talk.save();

                                                    callback(null);
                                                }
                                            );
                                        },
                                        function (err) {
                                            callback(err);
                                        }
                                    );
                                }
                            );
                        },
                        done
                    );
                }
            );
        }
    );
};
