<commit_before>'use strict';

var ratelimiter = require('ffratelimiter')
  , MetaInspector = require('./node-metainspector-modified')
  ;

module.exports = function(options, imports, register){
    console.log("Setup Scraper module");
    var scraper = new Scraper();
    register(null, {
        "scraper": scraper
    });
};

var Scraper = function() {
    console.log("Setup Scraper module.");
    var me = this;
    var params = {
        limit: 10,
        window: 100, // ms
        concurrency: 10,
        attempts: 2
    };
    me.que = new ratelimiter(params);
};

(function() {

    this.getStructuredData = function(url, callback){
        var me = this;
        var params = [ url ];
        me.que.callWrapper(me._getStructuredData, params, callback, me);
    };

    this._getStructuredData = function(url, callback) {
        var me = this;
        var client = new (MetaInspector())(url, {});
        client.on('fetch', function(){
            var _data = {
                url: client.url,
                host: client.host,
                scheme: client.scheme,
                title: client.ogTitle() || client.title(),
                author: client.author(),
                keywords: client.keywords(),
                charset: client.charset(),
                description: client.description(),
                feeds: client.feeds(),
                internal_links: [],
                external_links: [],
                fulltext: client.document
            };

            var host;
            var hostArr = _data.host.split(".");
            if(hostArr.length > 1){
                var len = hostArr.length;
                host = new RegExp(hostArr[len-2] + "." + hostArr[len-1] + "$");
            }else{
                host = new RegExp(_data.host + "$");
            }

            var links = client.links();
            links.forEach(function(link){
                if(link){
                    if (link[0] == '/'){
                        link = client.rootUrl + link;
                        _data.internal_links.push(link);
                    } else if (link[0] == '#') {
                        // skip - in page link
                    } else {
                        var linkurl = require('url').parse(link);
                        var linkhost = linkurl.hostname;
                        if(!linkhost.match(host)){
                          _data.external_links.push(link);
                        }else{
                          _data.internal_links.push(link);
                        }
                    }
                }
            });
            callback(null, _data);
        });
        client.on('error', function(err){
            callback(err);
        });
        client.fetch();
    };

    this.getLinksFromHtml = function(html, callback){
        var me = this;
        var client = new (MetaInspector())(null, html);
        client.on('fetch', function(){
            var _data = {
                title: client.ogTitle() || client.title(),
                author: client.author(),
                keywords: client.keywords(),
                charset: client.charset(),
                description: client.description(),
                feeds: client.feeds(),
                links: client.links()
            };
            callback(null, _data);
        });
        client.on('error', function(err){
            callback(err);
        });
        client.parse();
    };

}).call(Scraper.prototype);
<commit_msg>Fixes in scraper<commit_after>'use strict';

var ratelimiter = require('ffratelimiter')
  , MetaInspector = require('./node-metainspector-modified')
  ;

module.exports = function(options, imports, register){
    console.log("Setup Scraper module");
    var scraper = new Scraper();
    register(null, {
        "scraper": scraper
    });
};

var Scraper = function() {
    console.log("Setup Scraper module.");
    var me = this;
    var params = {
        limit: 10,
        window: 100, // ms
        concurrency: 10,
        attempts: 2
    };
    me.que = new ratelimiter(params);
};

(function() {

    this.getStructuredData = function(url, callback){
        var me = this;
        var params = [ url ];
        me.que.callWrapper(me._getStructuredData, params, callback, me);
    };

    this._getStructuredData = function(url, callback) {
        var me = this;
        var client = new (MetaInspector())(url, {});
        client.on('fetch', function(){
            var _data = {
                url: client.url,
                host: client.host,
                scheme: client.scheme,
                title: client.ogTitle() || client.title(),
                author: client.author(),
                keywords: client.keywords(),
                charset: client.charset(),
                description: client.description(),
                feeds: client.feeds(),
                internal_links: [],
                external_links: [],
                fulltext: client.document
            };

            var host;
            var hostArr = _data.host.split(".");
            if(hostArr.length > 1){
                var len = hostArr.length;
                host = new RegExp(hostArr[len-2] + "." + hostArr[len-1] + "$");
            }else{
                host = new RegExp(_data.host + "$");
            }

            var links = client.links();
            links.forEach(function(link){
                if(link){
                    if (link[0] == '/'){
                        link = client.rootUrl + link;
                        _data.internal_links.push(link);
                    } else if (link[0] == '#') {
                        // skip - in page link
                    } else {
                        var linkurl = require('url').parse(link);
                        var linkhost = linkurl.hostname;
                        if(linkhost){
                          if(!linkhost.match(host)){
                            _data.external_links.push(link);
                          }else{
                            _data.internal_links.push(link);
                          }
                        }else{
                          console.log('Warning! Url ' + linkurl + ' is getting skipped.');
                        }
                    }
                }
            });
            callback(null, _data);
        });
        client.on('error', function(err){
            callback(err);
        });
        client.fetch();
    };

    this.getLinksFromHtml = function(html, callback){
        var me = this;
        var client = new (MetaInspector())(null, html);
        client.on('fetch', function(){
            var _data = {
                title: client.ogTitle() || client.title(),
                author: client.author(),
                keywords: client.keywords(),
                charset: client.charset(),
                description: client.description(),
                feeds: client.feeds(),
                links: client.links()
            };
            callback(null, _data);
        });
        client.on('error', function(err){
            callback(err);
        });
        client.parse();
    };

}).call(Scraper.prototype);
<|endoftext|>


<commit_before>function(doc) {

    //tender checks
    var startDate = (doc.enquiryPeriod || {}).startDate;
    if ( (!startDate) || (startDate < "2016-04-01") ) {return;}
    if (doc.procurementMethod !== "open") {return;}
    if ((doc.mode || "") === "test") { return;}

    //global tender values
    var kind = doc.procuringEntity.kind || "_kind";
    var owner = doc.owner;
    var tender_id = doc._id;
    var tender_status = doc.status;
    var tenderID = doc.tenderID;
    var datemodified = doc.dateModified;

    var count_lot_bids = function(lot, bids) {
        return ( bids || [] ).map(function(bid) {
            return ( bid.lotValues || [] ).filter(function(value) {
                return value.relatedLot === lot.id;
            }).length;
        }).reduce(function( total, curr) {
            return total + curr;
        }, 0) || 0;
    }

    var max_date = function (obj) {
        //helper function to find max date in object
        var dates = [];

        ['date', 'dateSigned', 'documents'].forEach(function(field){
            var date = obj[field] || '';
            if (date) {
                if (typeof date === "object") {
                    date.forEach(function(d) {
                        dates.push(new Date(d.datePublished));
                    });
                } else {
                    dates.push(new Date(date));
                }
            }
        });
        return new Date(Math.max.apply(null, dates));
    };
    //*****************************************************************************************************************

    var date_normalize = function(date) {
        //return date in UTC format
        var ddate = '';
        if (typeof date !== 'object') {
            ddate = new Date(date)
        } else {
            ddate = date;
        }
        return ddate.toISOString().slice(0, 23);
    };
    //*****************************************************************************************************************

    var Handler = function (tender){
        this.is_multilot = ( "lots" in tender ) ? true:false;
        this.bids_disclosure_standstill = (tender.qualificationPeriod || {}).endDate || (tender.awardPeriod || {}).startDate || (tender.tenderPeriod || {}).endDate;
        this.last_complaint_standstill = ('awards' in tender)?tender.awards[tender.awards.length - 1].complaintPeriod.endDate:null;
        var tender_cancellation_date = '';
        if (tender.status === "cancelled" ) {
            var tender_cancellations = tender.cancellations.filter(function(cancellation) {
                return (cancellation.status === 'active') && (cancellation.cancellationOf === 'tender');
            });
            if (tender_cancellations.length > 0) {
                if (tender_cancellations.length > 1) {
                    this.tender_cancellation_date = max_date(tender_cancellations.reduce(function(prev_doc, curr_doc) {
                        return ( prev_doc.date > curr_doc.date ) ? curr_doc : prev_doc;
                    }));
                } else {
                    this.tender_cancellation_date = max_date(tender_cancellations[0]);
                }
            }
        }

        if (tender.status === 'unsuccessful') {
            this.tender_unsuccessful_date = this.last_complaint_standstill;
        }
    }

    var lotHandler = function (lot, tender){
        this.status = lot.status;
        this.tender_handler = new Handler(tender);

        switch(this.status) {
            case 'unsuccessful':
                var lot_awards = ( tender.awards || [] ).filter(function(award) {
                    return award.lotID === lot.id;
                });
                if (lot_awards.length > 0) {
                    this.lot_date = new Date( lot_awards[lot_awards.length - 1].complaintPeriod.endDate );
                } else{
                    this.lot_date = null;
                }
                break;
            case 'cancelled':
                var lot_cancellations = tender.cancellations.filter(function(cancellation) {
                    return (cancellation.status === 'active') && (cancellation.cancellationOf === 'lot') && (cancellation.relatedLot === lot.id);
                });
                if (lot_cancellations.length > 0) {
                    var date = '';
                    if (lot_cancellations.length > 1) {
                        var date = max_date(lot_cancellations.reduce(function(prev_doc, curr_doc) {
                            return ( prev_doc.date > curr_doc.date ) ? curr_doc : prev_doc;
                        }));
                    } else {
                        date = max_date( lot_cancellations[0] );
                    }
                    if ( !( date < this.tender_handler.bids_disclosure_standstill ) ) {
                        this.lot_date = date;
                    }
                }
                break;
            case 'complete':

                var contract_date = '';
                tender.awards.forEach(function(award) {
                    if (award.lotID === lot.id) {
                        (tender.contracts || []).forEach(function(contract) {
                            if (award.id === contract.awardID) {
                                if (contract.status === 'active') {
                                   contract_date = max_date(contract);
                                }
                            }
                        });
                    }
                });
                this.lot_date = contract_date;
                break;
            default:
                if (tender.status === 'cancelled') {
                    if (!( this.tender_handler.tender_cancellation_date < (new Date(this.tender_handler.bids_disclosure_standstill)) )) {
                        this.lot_date = this.tender_handler.tender_cancellation_date;
                    }
                } else {
                    var lotDate = '';
                    ( tender.awards || [] ).forEach(function(award) {
                        if (award.lotID === lot.id) {
                            (tender.contracts || []).forEach(function(contract) {
                                if (award.id === contract.awardID) {
                                    if (contract.status === 'active') {
                                        lotDate = max_date(contract)
                                    }
                                }
                            });
                        }
                    });
                    this.lot_date = lotDate;
                }
        }
    } // lotHandler

    var emitter = {
        lot: function(lot, date) {
            emit([owner, date_normalize(date), lot.id], {
                tender: tender_id,
                lot: lot.id,
                value: lot.value.amount,
                currency: lot.value.currency,
                kind: kind,
                lot_status: lot.status,
                status: tender_status,
                datemodified: datemodified,
                startdate: startDate,
                tenderID: tenderID,
            });
        },
        tender: function(tender,date) {
            emit([owner, date_normalize(date)], {
                tender: tender_id,
                value: tender.value.amount,
                currency: tender.value.currency,
                kind: kind,
                status: tender_status,
                datemodified: datemodified,
                startdate: startDate,
                tenderID: tenderID,
            });
        }
    }

    var find_tender_data = function(tender) {
        var handler = new Handler(tender);
        switch(tender.status) {
            case 'cancelled':
                if (handler.is_multilot) {

                    tender.lots.forEach(function(lot){

                        var lot_handler = new lotHandler(lot, tender);
                        if (['aboveThresholdUA', 'aboveThresholdEU'].indexOf(tender.procurementMethodType) !== -1) {
                            if (count_lot_bids(lot, tender.bids) > 1) {
                                if (lot_handler.lot_date) {
                                    emitter.lot(lot, lot_handler.lot_date);
                                }
                            }
                        } else {
                            if (count_lot_bids(lot, tender.bids) > 0) {
                                if (lot_handler.lot_date) {
                                    emitter.lot(lot, lot_handler.lot_date);
                                }
                            }
                        }
                    });

                } else {
                    if (handler.tender_cancellation_date < (new Date(handler.bids_disclosure_standstill))) { return; }
                    if (['aboveThresholdUA', 'aboveThresholdEU'].indexOf(tender.procurementMethodType) !== -1) {
                        if (tender.numberOfBids < 2) {
                            return;
                        }
                    } else {
                        if (tender.numberOfBids < 1) {
                            return;
                        }
                    }
                    emitter.tender(tender, handler.tender_cancellation_date);

                }
                break;
            case 'unsuccessful':
                if (handler.is_multilot) {
                    tender.lots.forEach(function(lot) {

                        var lot_handler = new lotHandler(lot, tender);
                        if (['aboveThresholdUA', 'aboveThresholdEU'].indexOf(tender.procurementMethodType) !== -1) {
                            if (count_lot_bids(lot, tender.bids) > 1) {
                                if (lot_handler.lot_date) {
                                    emitter.lot(lot, lot_handler.lot_date);
                                }
                            }
                        } else {
                            if (count_lot_bids(lot, tender.bids) > 0) {
                                if (lot_handler.lot_date) {
                                    emitter.lot(lot, lot_handler.lot_date);
                                }
                            }
                        }
                    });
                } else {
                    if (['aboveThresholdUA', 'aboveThresholdEU'].indexOf(tender.procurementMethodType) !== -1) {
                        if (tender.numberOfBids < 2) {
                            return;
                        }
                    } else {
                        if (tender.numberOfBids < 1) {
                            return;
                        }
                    }
                    if (handler.tender_unsuccessful_date) {
                        emitter.tender(tender, handler.tender_unsuccessful_date);
                    }
                }
                break;
            default:
                if (handler.is_multilot) {
                    tender.lots.forEach(function(lot) {
                       var lot_handler = new lotHandler(lot, tender);
                       if (lot_handler.lot_date) {
                           emitter.lot(lot, lot_handler.lot_date);
                       }
                    }); // forEach end
                } else {
                    ( tender.contracts || []).forEach(function(contract) {
                        if (contract.status === 'active') {
                            var date = max_date(contract);
                            emitter.tender(tender, date_normalize(date));
                        }
                    });
                }
        }
    }

    find_tender_data(doc);
}// end function

<commit_msg>Fixed emitting cancelled lots before bids disclojure date<commit_after>function(doc) {

    //tender checks
    var startDate = (doc.enquiryPeriod || {}).startDate;
    if ( (!startDate) || (startDate < "2016-04-01") ) {return;}
    if (doc.procurementMethod !== "open") {return;}
    if ((doc.mode || "") === "test") { return;}

    //global tender values
    var kind = doc.procuringEntity.kind || "_kind";
    var owner = doc.owner;
    var tender_id = doc._id;
    var tender_status = doc.status;
    var tenderID = doc.tenderID;
    var datemodified = doc.dateModified;

    var count_lot_bids = function(lot, bids) {
        return ( bids || [] ).map(function(bid) {
            return ( bid.lotValues || [] ).filter(function(value) {
                return value.relatedLot === lot.id;
            }).length;
        }).reduce(function( total, curr) {
            return total + curr;
        }, 0) || 0;
    }

    var max_date = function (obj) {
        //helper function to find max date in object
        var dates = [];

        ['date', 'dateSigned', 'documents'].forEach(function(field){
            var date = obj[field] || '';
            if (date) {
                if (typeof date === "object") {
                    date.forEach(function(d) {
                        dates.push(new Date(d.datePublished));
                    });
                } else {
                    dates.push(new Date(date));
                }
            }
        });
        return new Date(Math.max.apply(null, dates));
    };
    //*****************************************************************************************************************

    var date_normalize = function(date) {
        //return date in UTC format
        var ddate = '';
        if (typeof date !== 'object') {
            ddate = new Date(date)
        } else {
            ddate = date;
        }
        return ddate.toISOString().slice(0, 23);
    };
    //*****************************************************************************************************************

    var Handler = function (tender){
        this.is_multilot = ( "lots" in tender ) ? true:false;
        this.bids_disclosure_standstill = (tender.qualificationPeriod || {}).endDate || (tender.awardPeriod || {}).startDate || (tender.tenderPeriod || {}).endDate;
        this.last_complaint_standstill = ('awards' in tender)?tender.awards[tender.awards.length - 1].complaintPeriod.endDate:null;
        var tender_cancellation_date = '';
        if (tender.status === "cancelled" ) {
            var tender_cancellations = tender.cancellations.filter(function(cancellation) {
                return (cancellation.status === 'active') && (cancellation.cancellationOf === 'tender');
            });
            if (tender_cancellations.length > 0) {
                if (tender_cancellations.length > 1) {
                    this.tender_cancellation_date = max_date(tender_cancellations.reduce(function(prev_doc, curr_doc) {
                        return ( prev_doc.date > curr_doc.date ) ? curr_doc : prev_doc;
                    }));
                } else {
                    this.tender_cancellation_date = max_date(tender_cancellations[0]);
                }
            }
        }

        if (tender.status === 'unsuccessful') {
            this.tender_unsuccessful_date = this.last_complaint_standstill;
        }
    }

    var lotHandler = function (lot, tender){
        this.status = lot.status;
        this.tender_handler = new Handler(tender);

        switch(this.status) {
            case 'unsuccessful':
                var lot_awards = ( tender.awards || [] ).filter(function(award) {
                    return award.lotID === lot.id;
                });
                if (lot_awards.length > 0) {
                    this.lot_date = new Date( lot_awards[lot_awards.length - 1].complaintPeriod.endDate );
                } else{
                    this.lot_date = null;
                }
                break;
            case 'cancelled':
                var lot_cancellations = tender.cancellations.filter(function(cancellation) {
                    return (cancellation.status === 'active') && (cancellation.cancellationOf === 'lot') && (cancellation.relatedLot === lot.id);
                });
                if (lot_cancellations.length > 0) {
                    var date = '';
                    if (lot_cancellations.length > 1) {
                        var date = max_date(lot_cancellations.reduce(function(prev_doc, curr_doc) {
                            return ( prev_doc.date > curr_doc.date ) ? curr_doc : prev_doc;
                        }));
                    } else {
                        date = max_date( lot_cancellations[0] );
                    }
                    if ( !( date < (new Date(this.tender_handler.bids_disclosure_standstill)) ) ) {
                        this.lot_date = date;
                    }
                }
                break;
            case 'complete':

                var contract_date = '';
                tender.awards.forEach(function(award) {
                    if (award.lotID === lot.id) {
                        (tender.contracts || []).forEach(function(contract) {
                            if (award.id === contract.awardID) {
                                if (contract.status === 'active') {
                                   contract_date = max_date(contract);
                                }
                            }
                        });
                    }
                });
                this.lot_date = contract_date;
                break;
            default:
                if (tender.status === 'cancelled') {
                    if (!( this.tender_handler.tender_cancellation_date < (new Date(this.tender_handler.bids_disclosure_standstill)) )) {
                        this.lot_date = this.tender_handler.tender_cancellation_date;
                    }
                } else {
                    var lotDate = '';
                    ( tender.awards || [] ).forEach(function(award) {
                        if (award.lotID === lot.id) {
                            (tender.contracts || []).forEach(function(contract) {
                                if (award.id === contract.awardID) {
                                    if (contract.status === 'active') {
                                        lotDate = max_date(contract)
                                    }
                                }
                            });
                        }
                    });
                    this.lot_date = lotDate;
                }
        }
    } // lotHandler

    var emitter = {
        lot: function(lot, date) {
            emit([owner, date_normalize(date), lot.id], {
                tender: tender_id,
                lot: lot.id,
                value: lot.value.amount,
                currency: lot.value.currency,
                kind: kind,
                lot_status: lot.status,
                status: tender_status,
                datemodified: datemodified,
                startdate: startDate,
                tenderID: tenderID,
            });
        },
        tender: function(tender,date) {
            emit([owner, date_normalize(date)], {
                tender: tender_id,
                value: tender.value.amount,
                currency: tender.value.currency,
                kind: kind,
                status: tender_status,
                datemodified: datemodified,
                startdate: startDate,
                tenderID: tenderID,
            });
        }
    }

    var find_tender_data = function(tender) {
        var handler = new Handler(tender);
        switch(tender.status) {
            case 'cancelled':
                if (handler.is_multilot) {

                    tender.lots.forEach(function(lot){

                        var lot_handler = new lotHandler(lot, tender);
                        if (['aboveThresholdUA', 'aboveThresholdEU'].indexOf(tender.procurementMethodType) !== -1) {
                            if (count_lot_bids(lot, tender.bids) > 1) {
                                if (lot_handler.lot_date) {
                                    emitter.lot(lot, lot_handler.lot_date);
                                }
                            }
                        } else {
                            if (count_lot_bids(lot, tender.bids) > 0) {
                                if (lot_handler.lot_date) {
                                    emitter.lot(lot, lot_handler.lot_date);
                                }
                            }
                        }
                    });

                } else {
                    if (handler.tender_cancellation_date < (new Date(handler.bids_disclosure_standstill))) { return; }
                    if (['aboveThresholdUA', 'aboveThresholdEU'].indexOf(tender.procurementMethodType) !== -1) {
                        if (tender.numberOfBids < 2) {
                            return;
                        }
                    } else {
                        if (tender.numberOfBids < 1) {
                            return;
                        }
                    }
                    emitter.tender(tender, handler.tender_cancellation_date);

                }
                break;
            case 'unsuccessful':
                if (handler.is_multilot) {
                    tender.lots.forEach(function(lot) {

                        var lot_handler = new lotHandler(lot, tender);
                        if (['aboveThresholdUA', 'aboveThresholdEU'].indexOf(tender.procurementMethodType) !== -1) {
                            if (count_lot_bids(lot, tender.bids) > 1) {
                                if (lot_handler.lot_date) {
                                    emitter.lot(lot, lot_handler.lot_date);
                                }
                            }
                        } else {
                            if (count_lot_bids(lot, tender.bids) > 0) {
                                if (lot_handler.lot_date) {
                                    emitter.lot(lot, lot_handler.lot_date);
                                }
                            }
                        }
                    });
                } else {
                    if (['aboveThresholdUA', 'aboveThresholdEU'].indexOf(tender.procurementMethodType) !== -1) {
                        if (tender.numberOfBids < 2) {
                            return;
                        }
                    } else {
                        if (tender.numberOfBids < 1) {
                            return;
                        }
                    }
                    if (handler.tender_unsuccessful_date) {
                        emitter.tender(tender, handler.tender_unsuccessful_date);
                    }
                }
                break;
            default:
                if (handler.is_multilot) {
                    tender.lots.forEach(function(lot) {
                       var lot_handler = new lotHandler(lot, tender);
                       if (lot_handler.lot_date) {
                           emitter.lot(lot, lot_handler.lot_date);
                       }
                    }); // forEach end
                } else {
                    ( tender.contracts || []).forEach(function(contract) {
                        if (contract.status === 'active') {
                            var date = max_date(contract);
                            emitter.tender(tender, date_normalize(date));
                        }
                    });
                }
        }
    }

    find_tender_data(doc);
}// end function

<|endoftext|>


<commit_before>/* Copyright 2015 IBM Corporation

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and
limitations under the License. */

var express = require('express');
var router = express.Router();
var config = require('../config/server');
var parseString = require('xml2js').parseString;
var request = require('request');
var Busboy = require('busboy');
var fs = require('fs');

/* GET home page. */
router.get('/', function(req, res, next) {
  res.redirect('/');
});

//getfeed for home page content
router.get('/feed', function(req, res, next){
  if(!req.user)
    res.status(403).end();

  //config.server.domain is the domain name of the server (without the https or the directoy i.e example.com)

  var url = 'https://' + config.server.domain + '/files/oauth/api/documents/feed?visibility=public&includeTags=true';

  //if query parameters exist, append them onto the url
  if(!isEmpty(req.query.q)){
    url = url + '?tag=' + req.query.q;
  }

  var headers = {};

  // we must attach the key we got through passportto the header as
  // Authorization: Bearer + key. Passport gives us access to the user profile
  // we saved through the request user object
  headers['Authorization'] = 'Bearer ' + req.user.accessToken;

  var options = {
    url: url,
    headers: headers
  };

  request.get(options, function(error, response, body){

    // if there is an error lets log it to the console and inform the angular
    // of our app so it can be handled
    if(error){
      console.log('Error occured while getting feed from Connections Cloud: ' + error);
      res.status(500).end();
    } else {

      // otherwise, the api returns an xml which can be easily converted to a
      // JSON to make parsing easier using the xml2js module for nodejs
      parseString(body, function(err, result){

        // initialize the array of photos we will be sending back
        var photos = [];

        // get the actual entries object in the response
        var entries = result.feed.entry;

        // iterate over the entries to send back each photo that was returned
        for(var i = 0; i < entries.length; i++){
          var photo = {};
          var entry = entries[i];

          // the photo id which is used to reference the photo internally for
          // ibm connection cloud, this will need to be stored to quickly
          // get the photo and information on the photo later
          photo.id = entry['td:uuid'][0];

          // iterate through the tags in the entry and build an object that has
          // all the tags, starting at 1 because the first category describes
          // the document and is not a true 'tag'
          var tags = [];
          for(var j = 1; j < entry.category.length; j ++){
            var category = entry.category[j];
            var tag = category.$.label;
            tags.push(tag);
          }
          photo.tags = tags;

          // the author of the file, and in our case the photographer
          photo.photographer = entry.author[0].name[0];

          // the title of the document
          photo.title = entry.title[0]['_'];

          // the upload date of the document
          photo.published = entry.published[0];

          // the link object contains many links related to the document,
          // however we want the link to the actual image, therefore we will
          // look for the object with the type of image
          for(var j = 0; j < entry.link.length; j++){
            var link = entry.link[j];
            var type = link.$.type;
            if(!(type === undefined) && (type.indexOf('image') > -1)){
              photo.link = link.$.href;
              break;
            }
          }

          // in addition we need to pass the library id of the entry, for later
          // calls in which we will have to pass the library id to the api
          photo.libraryid = entry['td:libraryId'][0];
          console.log(photo.id + ' : ' + photo.libraryid);

          // push the photo to our photos array
          console.log('pushing :' + JSON.stringify(photo));
          photos.push(photo);
        }

        //return our photos array
        console.log('sending response');
        res.send(photos);
      });
    }
  });
});

//get photo for retrieving a photo by id
router.get('/photo', function(req, res, next){

  if(!req.user)
    res.status(403).end();

  //if no id was passed, return an error code
  if(isEmpty(req.query.id) || isEmpty(req.query.lid)){
    console.log('query not found');
    res.status(412).end();
  } else {
      var url = 'https://' + config.server.domain + '/files/oauth/api/library/' + req.query.lid + '/document/' + req.query.id + '/entry?includeTags=true';

      // we must attach the key we got through passportto the header as
      // Authorization: Bearer + key. Passport gives us access to the user profile
      // we saved through the request user object
      var headers = {'Authorization': 'Bearer ' + req.user.accessToken};

      var options = {
        url: url,
        headers: headers
      };

      request.get(options, function(error, response, body){
        if(error){
          console.log('photo error: ' + error);
          res.status(500).end();
        } else {
          //see get feed for more details
          parseString(body, function(err, result){
            fs.writeFile("response.txt", JSON.stringify(result));
            var photo = {};
            var entry = result.entry;
            photo.id = entry['td:uuid'][0];
            var tags = [];
            for(var j = 1; j < entry.category.length; j ++){
              var category = entry.category[j];
              var tag = category.$.label;
              tags.push(tag);
            }
            photo.tags = tags;
            for(var j = 0; j < entry.link.length; j++){
              var link = entry.link[j];
              var type = link.$.type;
              if(!(type === undefined) && (type.indexOf('image') > -1)){
                photo.link = link.$.href;
                break;
              }
            }
            photo.photographer = entry.author[0].name[0];
            photo.userid = entry.author[0]['snx:userid'][0];
            photo.title = entry.title[0]['_'];
            photo.published = entry.published[0];
            var socialx = entry['snx:rank'];
            for(var i = 0; i < socialx.length; i++){
              var x = socialx[i];
              if(x.$.scheme.indexOf('recommendations') > -1){
                photo.likes = parseInt(x['_']);
                break;
              }
            }
            photo.libraryid = entry['td:libraryId'][0];
            console.log('Sending response');
            res.send(photo);
          });
        }
      });
  }
});

router.post('/like', function(req, res, next){
  if(!req.user)
    res.status(403).end();

  if(isEmpty(req.query.id)){
    req.status(412).end();
  } else {

  }
})

router.get('/comments', function(req, res, next){
  if(!req.user)
    res.status(403).end();

  //if no id was passed
  if(isEmpty(req.query.id)){
    console.log("query not found");
    res.status(412).end();
  } else if(isEmpty(req.query.userid)){
    res.status(412).end();
  } else {
    var url = 'https://' + config.server.domain + '/files/oauth/api/userlibrary/' + req.query.userid + '/document/' + req.query.id + '/feed?category=comment';

    var headers = {'Authorization': 'Bearer ' + req.user.accessToken};

    var options = {
      url: url,
      headers: headers
    };

    console.log(JSON.stringify(options));

    console.log('Making request');

    request.get(options, function(error, response, body){
      if(error){
        console.log('error in get comment: ' + error);
        res.status(500).end();
      } else {
        parseString(body, function(err, result){
          // fs.writeFile("response.txt", JSON.stringify(result));
          var comments = [];
          var entries = result.feed.entry;
          for(var i = 0; i < entries.length; i++){
            var entry = entries[i];
            var comment = {};
            comment.author = entry.author[0].name[0];
            comment.date = entry.published[0];
            comment.content = entry.content[0]['_'];
            comments.push(comment);
          }
          res.send(comments);
        });
      }
    });
  }
});

//upload a file
router.post('/upload', function(req, res, next){
  // if the user does not exist, send back forbidden
  if(!req.user)
    res.status(403).end()

  // before uploading, we must obtain a nonce, which is a handshake between
  // the api and our server to allow us to post to the server
  var url = 'https://' + config.server.domain + '/files/oauth/api/nonce';

  var headers = {'Authorization': 'Bearer ' + req.user.accessToken};

  var options = {
    url: url,
    headers: headers
  };

  // making the nonce request
  request.get(options, function(error, response, body){

    // if there was an error log the error and send back an error
    if(error){
      console.log('nonce failed: ' + error);
      res.status(500).end()
    } else {

      // the nonce is returned in the body of the response
      var nonce = body;

      // to obtain the file from the client, we use a module called busboy
      // that allows us to obtain the file stream from the client
      var busboy = new Busboy({headers: req.headers});

      // when busboy encounters a file (which should be the only thing it
      // obtains from the page) it will then pip the file to a request to the
      // api server
      busboy.on('file', function(fieldname, file, filename, encoding, mimetype){
        var url = 'https://' + server.domain + '/files/oauth/api/myuserlibrary/feed';

        // the slug is used to tell the api what it should call the file
        var slug = filename;

        var headers = {
          'Authorization': 'Bearer ' + req.user.accessToken,
          'Slug': slug,
          //the nonce is the nonce we obtained before
          'X-Update-Nonce': nonce
        };

        var options = {
          url: url,
          headers: headers
        };

        // we then pipe the file to the request to the api server
        file.pipe(request.post(options, function(error, response, body){

          // if we recieve an error then log it and send and erro back to the
          // client
          if(error){
            console.log('upload failed: ' + error);
            res.status(500).end();
          }
        }));

      });

      // when the busboy finishes processing the file, send back an OK status
      // and close the connection
      busboy.on('finish', function(){
        res.status(200).end();
      });
    }
  });
});

router.get('/profile', function(req, res, next){
  if(!req.user){
    res.status(403).end();
  }

  if(isEmpty(req.query.displayName)){
    console.log('query not found');
    req.status(412).end()
  } else {

    var url = '/profiles/atom/search.do?displayName=' + req.query.displayName;

    var headers = {'Authorization' : 'Bearer ' + req.user.accessToken};

    var options = {
      url : url,
      headers : headers
    };

    request.get(options, function(error, response, body){
      if(error){
        console.log('error: ' + error);
        res.status(500).end();
      }

      parseString(body, function(err, result){
        fs.writeFile("response.txt", JSON.stringify(result));
      });
    });
  }
});

var hasOwnProperty = Object.prototype.hasOwnProperty;

function isEmpty(obj) {

    // null and undefined are "empty"
    if (obj == null) return true;

    // Assume if it has a length property with a non-zero value
    // that that property is correct.
    if (obj.length > 0)    return false;
    if (obj.length === 0)  return true;

    // Otherwise, does it have any properties of its own?
    // Note that this doesn't handle
    // toString and valueOf enumeration bugs in IE < 9
    for (var key in obj) {
        if (hasOwnProperty.call(obj, key)) return false;
    }

    return true;
}

module.exports = router;
<commit_msg>cleaned up code<commit_after>/* Copyright 2015 IBM Corporation

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and
limitations under the License. */

var express = require('express');
var router = express.Router();
var config = require('../config/server');
var parseString = require('xml2js').parseString;
var request = require('request');
var Busboy = require('busboy');

/* GET home page. */
router.get('/', function(req, res, next) {
  res.redirect('/');
});

//getfeed for home page content
router.get('/feed', function(req, res, next){
  if(!req.user)
    res.status(403).end();

  //config.server.domain is the domain name of the server (without the https or the directoy i.e example.com)

  var url = 'https://' + config.server.domain + '/files/oauth/api/documents/feed?visibility=public&includeTags=true';

  //if query parameters exist, append them onto the url
  if(!isEmpty(req.query.q)){
    url = url + '?tag=' + req.query.q;
  }

  var headers = {};

  // we must attach the key we got through passportto the header as
  // Authorization: Bearer + key. Passport gives us access to the user profile
  // we saved through the request user object
  headers['Authorization'] = 'Bearer ' + req.user.accessToken;

  var options = {
    url: url,
    headers: headers
  };

  request.get(options, function(error, response, body){

    // if there is an error lets log it to the console and inform the angular
    // of our app so it can be handled
    if(error){
      console.log('Error occured while getting feed from Connections Cloud: ' + error);
      res.status(500).end();
    } else {

      // otherwise, the api returns an xml which can be easily converted to a
      // JSON to make parsing easier using the xml2js module for nodejs
      parseString(body, function(err, result){

        // initialize the array of photos we will be sending back
        var photos = [];

        // get the actual entries object in the response
        var entries = result.feed.entry;

        // iterate over the entries to send back each photo that was returned
        for(var i = 0; i < entries.length; i++){
          var photo = {};
          var entry = entries[i];

          // the photo id which is used to reference the photo internally for
          // ibm connection cloud, this will need to be stored to quickly
          // get the photo and information on the photo later
          photo.id = entry['td:uuid'][0];

          // iterate through the tags in the entry and build an object that has
          // all the tags, starting at 1 because the first category describes
          // the document and is not a true 'tag'
          var tags = [];
          for(var j = 1; j < entry.category.length; j ++){
            var category = entry.category[j];
            var tag = category.$.label;
            tags.push(tag);
          }
          photo.tags = tags;

          // the author of the file, and in our case the photographer
          photo.photographer = entry.author[0].name[0];

          // the title of the document
          photo.title = entry.title[0]['_'];

          // the upload date of the document
          photo.published = entry.published[0];

          // the link object contains many links related to the document,
          // however we want the link to the actual image, therefore we will
          // look for the object with the type of image
          for(var j = 0; j < entry.link.length; j++){
            var link = entry.link[j];
            var type = link.$.type;
            if(!(type === undefined) && (type.indexOf('image') > -1)){
              photo.link = link.$.href;
              break;
            }
          }

          // in addition we need to pass the library id of the entry, for later
          // calls in which we will have to pass the library id to the api
          photo.libraryid = entry['td:libraryId'][0];

          // push the photo to our photos array
          console.log('pushing :' + JSON.stringify(photo));
          photos.push(photo);
        }

        //return our photos array
        console.log('sending response');
        res.send(photos);
      });
    }
  });
});

//get photo for retrieving a photo by id
router.get('/photo', function(req, res, next){

  if(!req.user)
    res.status(403).end();

  //if no id was passed, return an error code
  if(isEmpty(req.query.id) || isEmpty(req.query.lid)){
    console.log('query not found');
    res.status(412).end();
  } else {
      var url = 'https://' + config.server.domain + '/files/oauth/api/library/' + req.query.lid + '/document/' + req.query.id + '/entry?includeTags=true';

      // we must attach the key we got through passportto the header as
      // Authorization: Bearer + key. Passport gives us access to the user profile
      // we saved through the request user object
      var headers = {'Authorization': 'Bearer ' + req.user.accessToken};

      var options = {
        url: url,
        headers: headers
      };

      request.get(options, function(error, response, body){
        if(error){
          console.log('photo error: ' + error);
          res.status(500).end();
        } else {
          //see get feed for more details
          parseString(body, function(err, result){
            fs.writeFile("response.txt", JSON.stringify(result));
            var photo = {};
            var entry = result.entry;
            photo.id = entry['td:uuid'][0];
            var tags = [];
            for(var j = 1; j < entry.category.length; j ++){
              var category = entry.category[j];
              var tag = category.$.label;
              tags.push(tag);
            }
            photo.tags = tags;
            for(var j = 0; j < entry.link.length; j++){
              var link = entry.link[j];
              var type = link.$.type;
              if(!(type === undefined) && (type.indexOf('image') > -1)){
                photo.link = link.$.href;
                break;
              }
            }
            photo.photographer = entry.author[0].name[0];
            photo.userid = entry.author[0]['snx:userid'][0];
            photo.title = entry.title[0]['_'];
            photo.published = entry.published[0];
            var socialx = entry['snx:rank'];
            for(var i = 0; i < socialx.length; i++){
              var x = socialx[i];
              if(x.$.scheme.indexOf('recommendations') > -1){
                photo.likes = parseInt(x['_']);
                break;
              }
            }
            photo.libraryid = entry['td:libraryId'][0];
            console.log('Sending response');
            res.send(photo);
          });
        }
      });
  }
});

router.post('/like', function(req, res, next){
  if(!req.user)
    res.status(403).end();

  if(isEmpty(req.query.id)){
    req.status(412).end();
  } else {

  }
})

router.get('/comments', function(req, res, next){
  if(!req.user)
    res.status(403).end();

  //if no id was passed
  if(isEmpty(req.query.id)){
    console.log("query not found");
    res.status(412).end();
  } else if(isEmpty(req.query.userid)){
    res.status(412).end();
  } else {
    var url = 'https://' + config.server.domain + '/files/oauth/api/userlibrary/' + req.query.userid + '/document/' + req.query.id + '/feed?category=comment';

    var headers = {'Authorization': 'Bearer ' + req.user.accessToken};

    var options = {
      url: url,
      headers: headers
    };

    request.get(options, function(error, response, body){
      if(error){
        console.log('error in get comment: ' + error);
        res.status(500).end();
      } else {
        parseString(body, function(err, result){
          // fs.writeFile("response.txt", JSON.stringify(result));
          var comments = [];
          var entries = result.feed.entry;
          for(var i = 0; i < entries.length; i++){
            var entry = entries[i];
            var comment = {};
            comment.author = entry.author[0].name[0];
            comment.date = entry.published[0];
            comment.content = entry.content[0]['_'];
            comments.push(comment);
          }
          res.send(comments);
        });
      }
    });
  }
});

//upload a file
router.post('/upload', function(req, res, next){
  // if the user does not exist, send back forbidden
  if(!req.user)
    res.status(403).end()

  // before uploading, we must obtain a nonce, which is a handshake between
  // the api and our server to allow us to post to the server
  var url = 'https://' + config.server.domain + '/files/oauth/api/nonce';

  var headers = {'Authorization': 'Bearer ' + req.user.accessToken};

  var options = {
    url: url,
    headers: headers
  };

  // making the nonce request
  request.get(options, function(error, response, body){

    // if there was an error log the error and send back an error
    if(error){
      console.log('nonce failed: ' + error);
      res.status(500).end()
    } else {

      // the nonce is returned in the body of the response
      var nonce = body;

      // to obtain the file from the client, we use a module called busboy
      // that allows us to obtain the file stream from the client
      var busboy = new Busboy({headers: req.headers});

      // when busboy encounters a file (which should be the only thing it
      // obtains from the page) it will then pip the file to a request to the
      // api server
      busboy.on('file', function(fieldname, file, filename, encoding, mimetype){
        var url = 'https://' + server.domain + '/files/oauth/api/myuserlibrary/feed';

        // the slug is used to tell the api what it should call the file
        var slug = filename;

        var headers = {
          'Authorization': 'Bearer ' + req.user.accessToken,
          'Slug': slug,
          //the nonce is the nonce we obtained before
          'X-Update-Nonce': nonce
        };

        var options = {
          url: url,
          headers: headers
        };

        // we then pipe the file to the request to the api server
        file.pipe(request.post(options, function(error, response, body){

          // if we recieve an error then log it and send and erro back to the
          // client
          if(error){
            console.log('upload failed: ' + error);
            res.status(500).end();
          }
        }));

      });

      // when the busboy finishes processing the file, send back an OK status
      // and close the connection
      busboy.on('finish', function(){
        res.status(200).end();
      });
    }
  });
});

router.get('/profile', function(req, res, next){
  if(!req.user){
    res.status(403).end();
  }

  if(isEmpty(req.query.displayName)){
    console.log('query not found');
    req.status(412).end()
  } else {

    var url = '/profiles/atom/search.do?displayName=' + req.query.displayName;

    var headers = {'Authorization' : 'Bearer ' + req.user.accessToken};

    var options = {
      url : url,
      headers : headers
    };

    request.get(options, function(error, response, body){
      if(error){
        console.log('error: ' + error);
        res.status(500).end();
      }

      parseString(body, function(err, result){
        fs.writeFile("response.txt", JSON.stringify(result));
      });
    });
  }
});

var hasOwnProperty = Object.prototype.hasOwnProperty;

function isEmpty(obj) {

    // null and undefined are "empty"
    if (obj == null) return true;

    // Assume if it has a length property with a non-zero value
    // that that property is correct.
    if (obj.length > 0)    return false;
    if (obj.length === 0)  return true;

    // Otherwise, does it have any properties of its own?
    // Note that this doesn't handle
    // toString and valueOf enumeration bugs in IE < 9
    for (var key in obj) {
        if (hasOwnProperty.call(obj, key)) return false;
    }

    return true;
}

module.exports = router;
<|endoftext|>


<commit_before>"use strict";

module.exports = function(imports) {

    let express = imports.modules.express;
    let Promise = imports.modules.Promise;
    let fs = require("fs"); // TODO: put this into initImports
    let util = imports.util;
    let handler = util.handler;

    let router = express.Router();

    router.get(handler(function*(req, res) {

            let pages = {
                signup: "Signup",
            };

            let page = Object.keys(pages)
                .find(page => req.path.substring(1).startsWith(page)));
        if (!page) {
            return next();
        }

        res.render("../../morteam-web/src/page.html.ejs", {
            page: pages[page]
        });

    }));

router.get("/js/:page", handler(function*(req, res) {
    let page = req.params.page;
    let file = "../../morteam-web/build/" + page + ".js";
    // TODO: use the pacakge fs-promise
    fs.exists(file, function(exists) {
        if (!exists) {
            return res.end("fail"); // AHHHH
        }
        fs.createReadStream(file).pipe(res);
    });
}));

return router;

};
<commit_msg>Make the view work<commit_after>"use strict";

module.exports = function(imports) {

    let express = imports.modules.express;
    let Promise = imports.modules.Promise;
    let fs = require("fs"); // TODO: put this into initImports
    let util = imports.util;
    let handler = util.handler;

    let router = express.Router();

    let pages = {
        signup: "Signup",
    };

    for (let page in pages) {

        router.get("/" + page, handler(function*(req, res) {

            res.render("../../morteam-web/src/page.html.ejs", {
                page: pages[page]
            });

        }));

    }

    router.get("/js/:page", handler(function*(req, res) {
        let page = req.params.page;
        let file = "../morteam-web/build/" + page + ".js";
        // TODO: use the pacakge fs-promise
        fs.exists(file, function(exists) {
            if (!exists) {
                return res.end("fail"); // AHHHH
            }
            fs.createReadStream(file).pipe(res);
        });
    }));

    return router;

};
<|endoftext|>


<commit_before>"use strict";

var SessionSockets = require('session.socket.io'),
    noop = function () {
    };

function socketIoPlugin(alamidRequest, options) {
    options = options || {};

    var errorHandler = options.onError || noop,
        io = options.io,
        sockets,
        session;

    if (options.session) {
        session = options.session;
        sockets = new SessionSockets(io, session.store, session.cookieParser, session.key);
        sockets.on("connection", handleSessionSocket);
        return;
    }

    sockets = io.sockets;
    sockets.on("connection", handleSocket);

    function handleSessionSocket(err, socket, session) {
        if (err) {
            errorHandler(err);
            return;
        }

        //attach ws listeners here
        socket.on("request", function (request, callback) {
            //get the latest session state (needed if http/ws in are both in use)
            reloadSession(session, function (err) {
                if (err) {
                    errorHandler(err);
                    return;
                }

                alamidRequest.adapters.ws(request, session, callback);
            });
        });
    }

    function handleSocket(socket) {
        //attach ws listeners here
        socket.on("request", function (request, callback) {
            alamidRequest.adapters.ws(request, {}, callback);
        });
    }

    function reloadSession(session, callback) {
        session.reload(function (err) {
            if (err) {
                callback(err);
                return;
            }

            session = session.req.session;
            callback(null);
        });
    }
}

module.exports = socketIoPlugin;<commit_msg>- saving session after ws-callback<commit_after>"use strict";

var SessionSockets = require('session.socket.io'),
    noop = function () {
    };

function socketIoPlugin(alamidRequest, options) {
    options = options || {};

    var errorHandler = options.onError || noop,
        io = options.io,
        sockets,
        session;

    if (options.session) {
        session = options.session;
        sockets = new SessionSockets(io, session.store, session.cookieParser, session.key);
        sockets.on("connection", handleSessionSocket);
        return;
    }

    sockets = io.sockets;
    sockets.on("connection", handleSocket);

    function handleSessionSocket(err, socket, session) {
        if (err) {
            errorHandler(err);
            return;
        }

        //attach ws listeners here
        socket.on("request", function (request, callback) {
            //get the latest session state (needed if http/ws in are both in use)
            reloadSession(session, function (err) {
                if (err) {
                    errorHandler(err);
                    callback({
                        status: "error",
                        code: "invalid-session"
                    });
                    return;
                }

                alamidRequest.adapters.ws(request, session, function () {
                    session.save();
                    callback.apply(this, arguments);
                });
            });
        });
    }

    function handleSocket(socket) {
        //attach ws listeners here
        socket.on("request", function (request, callback) {
            alamidRequest.adapters.ws(request, {}, callback);
        });
    }

    function reloadSession(session, callback) {
        session.reload(function (err) {
            if (err) {
                callback(err);
                return;
            }

            session = session.req.session;
            callback(null);
        });
    }
}

module.exports = socketIoPlugin;<|endoftext|>


<commit_before>(function(exports) {
    "use strict";

// These are the forms that require an email.
exports.email_setup = function() {
    if ($('div.paypal-inline').length) {
        // Don't hide this if there was an error.
        if (!$('ul.errorlist').length) {
            $('#id_email').closest('p').addClass('js-hidden');
        };

        $('div.paypal-inline input[type=radio]').click(function(e) {
            if ($(this).val() === 'no') {
                $('#id_email').closest('p').hide();
            } else {
                $('#id_email').closest('p').show();
            }
        });
    }
    $('#paypal-change-address').click(function(e) {
        $('#paypal-change-address-form').show();
    });
    // If you've submitted the form and there was an error, show the form.
    if ($('ul.errorlist').length) {
        $('#paypal-change-address-form').show();
    };
};

// This is the setup payments form.
exports.payment_setup = function() {
    if ($('section.payments input[name=premium_type]').val() === '0') {
        $('form div.brform').slice(1).hide();
    }
    $('section.payments input[name=premium_type]').click(function(e) {
        if ($(this).val() === '0') {
            $('form div.brform').slice(1).hide();
        } else {
            $('form div.brform').slice(1).show();
        }
    });
};

})(typeof exports === 'undefined' ? (this.dev_paypal = {}) : exports);

$(document).ready(function() {
    dev_paypal.email_setup();
    dev_paypal.payment_setup();
});

<commit_msg>only flip on the checked value, not the first in the list<commit_after>(function(exports) {
    "use strict";

// These are the forms that require an email.
exports.email_setup = function() {
    if ($('div.paypal-inline').length) {
        // Don't hide this if there was an error.
        if (!$('ul.errorlist').length) {
            $('#id_email').closest('p').addClass('js-hidden');
        };

        $('div.paypal-inline input[type=radio]').click(function(e) {
            if ($(this).val() === 'no') {
                $('#id_email').closest('p').hide();
            } else {
                $('#id_email').closest('p').show();
            }
        });
    }
    $('#paypal-change-address').click(function(e) {
        $('#paypal-change-address-form').show();
    });
    // If you've submitted the form and there was an error, show the form.
    if ($('ul.errorlist').length) {
        $('#paypal-change-address-form').show();
    };
};

// This is the setup payments form.
exports.payment_setup = function() {
    if ($('section.payments input[name=premium_type]:checked').val() === '0') {
        $('form div.brform').slice(1).hide();
    }
    $('section.payments input[name=premium_type]').click(function(e) {
        if ($(this).val() === '0') {
            $('form div.brform').slice(1).hide();
        } else {
            $('form div.brform').slice(1).show();
        }
    });
};

})(typeof exports === 'undefined' ? (this.dev_paypal = {}) : exports);

$(document).ready(function() {
    dev_paypal.email_setup();
    dev_paypal.payment_setup();
});

<|endoftext|>


<commit_before>var express = require('express');
_ = require('underscore')._
var request = require('request');
var router = express.Router({mergeParams: true});
var exec = require('child_process').exec;
var path = require('path');
var parentDir = path.resolve(process.cwd());

var jsonStream = require('express-jsonstream');

router.get('/', function(req, res, next) {
    res.json({'info' : 'This is the ControCurator API v0.1'});
});

router.post('/',function(req,res) {
    req.jsonStream()
        .on('object',console.log);
    return;

    var input = req.body;
    var amountofitems = input.length;

    var gooditems = 0;
    var baditems = 0;

    for (var i=0; i < amountofitems; i++)
    {
        var currentitem = input[i];
        if (!currentitem.hasOwnProperty('id') || !currentitem.hasOwnProperty('text')) //Test for an id and an actual body
        {
            baditems++;
            continue;
        }
        gooditems++;
    }
    var controscore = Math.random();

    res.json({'controversy':controscore,'totalItems':amountofitems,'goodItems':gooditems,'badItems':baditems});

});
module.exports = router;
<commit_msg>Revert back to limited situation<commit_after>var express = require('express');
_ = require('underscore')._
var request = require('request');
var router = express.Router({mergeParams: true});
var exec = require('child_process').exec;
var path = require('path');
var parentDir = path.resolve(process.cwd());

router.get('/', function(req, res, next) {
    res.json({'info' : 'This is the ControCurator API v0.1'});
});

router.post('/',function(req,res) {
    var input = req.body;
    var amountofitems = input.length;

    var gooditems = 0;
    var baditems = 0;

    for (var i=0; i < amountofitems; i++)
    {
        var currentitem = input[i];
        if (!currentitem.hasOwnProperty('id') || !currentitem.hasOwnProperty('text')) //Test for an id and an actual body
        {
            baditems++;
            continue;
        }
        gooditems++;
    }
    var controscore = Math.random();

    res.json({'controversy':controscore,'totalItems':amountofitems,'goodItems':gooditems,'badItems':baditems});

});
module.exports = router;
<|endoftext|>


<commit_before>/**
 * routes/api.js - Defines /api/search and /api/browse routes for API requests
 *
 * Authors: Ian McGaunn; Dave Zimmelman
 * Modified: 09 Mar 15
 */

var express = require('express');
var request = require('request');
var router = express.Router();

var Cache = require('../lib/badcache');
var Podcasts = require('../lib/podcasts');

/**
 * Routes for api/view URLs
 */
router.get('/view/splash', function (req, res, next) {
    var casts;
    if (req.session.user) {
        Cache.aggregate(req.session.user.subscriptions, function (err, msg) {
            console.log(msg);
            console.log(err);
            res.render(msg);
        }, function (data) {
            res.render('splash', {user: req.session.user,
                                    podcasts: data[0],
                                    favorites: data[1],
                                    genres: Podcasts.Genres,
                                    getGenreId: Podcasts.getGenreId});
        });

    }
    else {
        Cache.aggregate(false, function (err, msg) {
            console.log(msg);
            console.log(err);
            res.render(msg);
        }, function (data) {
            res.render('splash-guest', {podcasts: data[0]});
        }
    }
});

router.get('/view/podcast/:id', function (req, res, next) {

});

/**
 * Route for api/search URL
 */
router.get('/search', function (req, res, next) {
    var api = 'https://itunes.apple.com/search?entity=podcast&term=';
    // Name is ambiguous, and might confuse things when we implement local searching by station/artist/etc.
    // For now, it's best to use `term` to avoid confusion.
    var queryURL = api + req.query.term;

    console.log('url: ' + queryURL);

    // Send requested search data received from Apple API back to caller.
    request(queryURL, function (error, response, body) {
        // Parse JSON results
        var results = JSON.parse(body).results;
        if (results) {
            // Cache results
            Podcasts.cacheSearchResults(results, function (err, msg) {
                console.log(msg);
                console.log(err);
                res.json({"error": msg});
            }, function (pcasts) {
                Cache.search[req.query.term] = pcasts;
                res.json(pcasts);
            });
        } else {
            res.json({});
        }
    });
});

/**
 * Route for api/quicksearch URL
 */
router.get('/quicksearch', function (req, res, next) {
    Podcasts.searchByTerm(req.query.term, function (data) {
        res.json(data);
    });
});

/**
 * Route for api/browse URL
 */
router.get('/browse', function (req, res, next) {
    var api = 'https://itunes.apple.com/us/rss/toppodcasts';
    var limit = (req.query.limit && req.query.limit >= 0 && req.query.limit <= 200)
        ? '/limit=' + req.query.limit
        : '/limit=50';
    var explicit = (req.query.safe)
        ? '/explicit=false'
        : '/explicit=true';
    var genre = (req.query.genre && req.query.genre !== 0)
        ? '/genre=' + req.query.genre
        : '';
    var queryURL = api + limit + genre + explicit + '/json';

    console.log('url: ' + queryURL);

    // Send requested browse data received from Apple API back to caller.
    request(queryURL, function (error, response, body) {
        // Update cache with request
        Cache.browse[req.query.genre] = JSON.parse(body);
        res.send(body);
    });
});

// Expose routes
module.exports = router;
<commit_msg>[Fix] Syntax<commit_after>/**
 * routes/api.js - Defines /api/search and /api/browse routes for API requests
 *
 * Authors: Ian McGaunn; Dave Zimmelman
 * Modified: 09 Mar 15
 */

var express = require('express');
var request = require('request');
var router = express.Router();

var Cache = require('../lib/badcache');
var Podcasts = require('../lib/podcasts');

/**
 * Routes for api/view URLs
 */
router.get('/view/splash', function (req, res, next) {
    var casts;
    if (req.session.user) {
        Cache.aggregate(req.session.user.subscriptions, function (err, msg) {
            console.log(msg);
            console.log(err);
            res.render(msg);
        }, function (data) {
            res.render('splash', {user: req.session.user,
                                    podcasts: data[0],
                                    favorites: data[1],
                                    genres: Podcasts.Genres,
                                    getGenreId: Podcasts.getGenreId});
        });

    }
    else {
        Cache.aggregate(false, function (err, msg) {
            console.log(msg);
            console.log(err);
            res.render(msg);
        }, function (data) {
            res.render('splash-guest', {podcasts: data[0]});
        });
    }
});

router.get('/view/podcast/:id', function (req, res, next) {

});

/**
 * Route for api/search URL
 */
router.get('/search', function (req, res, next) {
    var api = 'https://itunes.apple.com/search?entity=podcast&term=';
    // Name is ambiguous, and might confuse things when we implement local searching by station/artist/etc.
    // For now, it's best to use `term` to avoid confusion.
    var queryURL = api + req.query.term;

    console.log('url: ' + queryURL);

    // Send requested search data received from Apple API back to caller.
    request(queryURL, function (error, response, body) {
        // Parse JSON results
        var results = JSON.parse(body).results;
        if (results) {
            // Cache results
            Podcasts.cacheSearchResults(results, function (err, msg) {
                console.log(msg);
                console.log(err);
                res.json({"error": msg});
            }, function (pcasts) {
                Cache.search[req.query.term] = pcasts;
                res.json(pcasts);
            });
        } else {
            res.json({});
        }
    });
});

/**
 * Route for api/quicksearch URL
 */
router.get('/quicksearch', function (req, res, next) {
    Podcasts.searchByTerm(req.query.term, function (data) {
        res.json(data);
    });
});

/**
 * Route for api/browse URL
 */
router.get('/browse', function (req, res, next) {
    var api = 'https://itunes.apple.com/us/rss/toppodcasts';
    var limit = (req.query.limit && req.query.limit >= 0 && req.query.limit <= 200)
        ? '/limit=' + req.query.limit
        : '/limit=50';
    var explicit = (req.query.safe)
        ? '/explicit=false'
        : '/explicit=true';
    var genre = (req.query.genre && req.query.genre !== 0)
        ? '/genre=' + req.query.genre
        : '';
    var queryURL = api + limit + genre + explicit + '/json';

    console.log('url: ' + queryURL);

    // Send requested browse data received from Apple API back to caller.
    request(queryURL, function (error, response, body) {
        // Update cache with request
        Cache.browse[req.query.genre] = JSON.parse(body);
        res.send(body);
    });
});

// Expose routes
module.exports = router;
<|endoftext|>


<commit_before>// All Utilities needed for proper Presenter working

Melown.Presenter.prototype.Utils.init = function(id_, HTMLtemplate_) {
    var obj_ = this;
    var templatePanelPrefix_ = '

';
    var templatePanelSuffix_ = '

';
    var templatePanel_ = templatePanelPrefix_ + HTMLtemplate_ + templatePanelSuffix_;
    var templateSubtitlesPrefix_ = '
<button type="button"></button><button type="button"></button>'
                                    + '

';
    var templateSubtitlesSuffix_ = '

';
    var templateSubtitles_ = templateSubtitlesPrefix_ + HTMLtemplate_ + templateSubtitlesSuffix_;
    var template_ = templatePanel_ + templateSubtitles_;
    var ctrlDelve_ = this.browser_.ui_.addControl(id_, template_);
    this.id_.push(id_);
    this.setContainer(ctrlDelve_);

    // Set all  tags to have onclick
    this.aTags_ = this.container_.getElementsByTagName('a');
    for(var i = 0; i < this.aTags_.length; i++){
        this.aTags_[i].onclick = function() { obj_.linksDecode(this); };
    };

    setTimeout(function(){
        obj_.renderControl();
    }, 200);
};

Melown.Presenter.prototype.Utils.readTextInput = function(id_) {
    var presentation_ = {
        htmlDataStorage : this.config_['presenter'][id_],
        id : id_,
        checkID : function() {
            var url_ = /^(ftp|http|https):\/\/[^ "]+$/;
            var hash_ = /^#.*$/;
            if(url_.test(this.htmlDataStorage) || this.htmlDataStorage.indexOf("./") != -1) {
                return 'url';
            }
            else if(hash_.test(this.htmlDataStorage)) {
                return 'hash';
            }
            else {
                return 'string';
            };
        }
    };

    var mode_ = presentation_.checkID();

    if(mode_ == 'url') {
        var rawFile_ = new XMLHttpRequest();
        var obj_ = this;
        rawFile_.open("GET", presentation_.htmlDataStorage, false);
        rawFile_.onreadystatechange = function() {
            if(rawFile_.readyState === 4) {
                if(rawFile_.status === 200 || rawFile_.status == 0) {
                    var allText_ = rawFile_.responseText;
                    obj_.html = allText_;
                    obj_.init(presentation_.id, obj_.html);
                }
                else {
                    obj_.file = 'undefined';
                };
            };
        };
        rawFile_.send(null);
    }
    else if(mode_ == 'hash') {
        var obj_ = document.getElementById(presentation_.htmlDataStorage).innerHTML;
        this.init(presentation_.id, obj_);
    }
    else if(mode_ == 'string') {
        this.init(presentation_.id, presentation_.htmlDataStorage);
    };
};

Melown.Presenter.prototype.Utils.linksDecode = function(obj_) {
    var position_ = null;
    var autorotate_ = null;
    var transition_ = null;
    var navigate_ = null;

    if(obj_.getAttribute('data-mln-navigate') !== null) {
        navigate_ = obj_.getAttribute('data-mln-navigate');
        if(navigate_ !== null) {
            if(navigate_ == 'prev') { this.nextArticle('-1'); }
            else if(navigate_ == 'next') { this.nextArticle('+1'); }
            else if(navigate_ == 'first') { this.nextArticle(0); }
            else if(navigate_ == 'last') { this.nextArticle(this.maxNodes_-1); }
            else { this.nextArticle(navigate_); };
            return 'navigation:true';
        };
    };

    if(obj_.getAttribute('data-mln-position') === null){
        return 'position:false';
    };

    position_ = this.getNumbers(obj_.getAttribute('data-mln-position').split(','));

    if(obj_.getAttribute('data-mln-autorotate') !== null) {
        autorotate_ = this.getNumbers(obj_.getAttribute('data-mln-autorotate'));
    };
    if(obj_.getAttribute('data-mln-transition') !== null) {
        transition_ = obj_.getAttribute('data-mln-transition');
    };

    if(transition_ === null) {
        this.browser_.autopilot_.flyTo(position_);
    }
    else if(transition_ == 'teleport') {
        this.browser_.core_.getMap().setPosition(position_);
    }
    else {
        this.browser_.autopilot_.flyTo(position_);
        // Feature to be considered
        // browser.flyTo(position, {mode_ : transition});
    };
    if(autorotate_ !== null) {
        this.browser_.autopilot_.setAutorotate(autorotate_);
    };

    return 'Moving to position: ' + position_;
};

// parseFloat here
Melown.Presenter.prototype.Utils.getNumbers = function(obj_) {
    var obj_ = obj_;
    for(var i = 0; i < obj_.length; i++){
        if(typeof obj_ == 'string' && parseFloat(obj_)) {
            obj_ = parseFloat(obj_);
            break;
        }
        if (parseFloat(obj_[i]))
            obj_[i] = parseFloat(obj_[i]); // toFixed might be added here
    }
    return obj_;
};

Melown.Presenter.prototype.Utils.nextArticle = function(node_, init_, lastNode_) {
    // fly to whatever node we wish
    if(node_ === '+1') { node_ = 1; }
    else if(node_ === '-1') { node_ = -1; }
    else {
        this.actualNode_ = node_;
        node_ = 0;
    };
    this.actualNode_ = this.actualNode_ + node_;

    if(this.actualNode_ >= 0 && this.actualNode_ < this.maxNodes_) {
        if(!init_) {
            if(this.activeToolbox_ == 'right') { this.handleArticle(this.actualNode_); }
            else if(this.activeToolbox_ == 'wide') { this.handleSubtitlesPosition(this.actualNode_); };
        };
        if(typeof lastNode_ !== 'undefined') { this.maxNodes_ = lastNode_; };
        this.linksDecode(this.container_.getElementsByTagName('section')[this.actualNode_]);
        return true;

    }
    else { this.actualNode_ = this.actualNode_ - node_; };
    return false;
};

Melown.Presenter.prototype.Utils.useToolbox = function() {

    var type_ = this.container_.getElementsByTagName('article')[0].getAttribute('data-mln-style');

    if(type_ === null) { type_ = 'right'; };

    var rightPanel_ = this.container_.getElementsByClassName('melown-presentations panelContainer')[0];
    var toolboxContainer_ = this.container_.getElementsByClassName('melown-presentations toolboxContainer')[0];
    var subtitles_ = this.container_.getElementsByClassName('melown-presentations subtitlesContainer')[0];
    var swipeControl_ = this.container_.getElementsByClassName('melown-presentations swipeControl');
    this.activeToolbox_ = type_;

    subtitles_.setAttribute('style', 'opacity: 0;');
    subtitles_.setAttribute('class', 'melown-presentations subtitlesContainer');
    if(type_ == 'right') {
        rightPanel_.style.display = 'block';
        setTimeout(function() {
            rightPanel_.style.opacity = 1;
        }, 20);
        swipeControl_[0].style.display = 'block';
        swipeControl_[1].style.display = 'block';
        for(var i = 0; i < this.sectionTags_.length; i++){ // Set maxHeight back as there is no dynamic rescaling of rightPanel_
            this.sectionTags_[i].style.height = this.maxHeight_ + 'px';
        };
        this.nextArticle(0);
    }
    else if(type_ == 'wide') {
        subtitles_.style.display = 'block';
        setTimeout(function() {
            subtitles_.style.opacity = 1;
        }, 20);
        rightPanel_.style.display = 'none';
        rightPanel_.style.opacity = 0;
        swipeControl_[0].style.display = 'none';
        swipeControl_[1].style.display = 'none';
        for(var i = 0; i < this.sectionTags_.length; i++){ // Set height to auto so we can dynamicaly adjust subtitles height
            this.sectionTags_[i].style.height = 'auto';
        };
        this.handleSubtitlesPosition(0, true);
    };
};

<commit_msg>Changed url reading.<commit_after>// All Utilities needed for proper Presenter working

Melown.Presenter.prototype.Utils.init = function(id_, HTMLtemplate_) {
    var obj_ = this;
    var templatePanelPrefix_ = '

';
    var templatePanelSuffix_ = '

';
    var templatePanel_ = templatePanelPrefix_ + HTMLtemplate_ + templatePanelSuffix_;
    var templateSubtitlesPrefix_ = '
<button type="button"></button><button type="button"></button>'
                                    + '

';
    var templateSubtitlesSuffix_ = '

';
    var templateSubtitles_ = templateSubtitlesPrefix_ + HTMLtemplate_ + templateSubtitlesSuffix_;
    var template_ = templatePanel_ + templateSubtitles_;
    var ctrlDelve_ = this.browser_.ui_.addControl(id_, template_);
    this.id_.push(id_);
    this.setContainer(ctrlDelve_);

    // Set all  tags to have onclick
    this.aTags_ = this.container_.getElementsByTagName('a');
    for(var i = 0; i < this.aTags_.length; i++){
        this.aTags_[i].onclick = function() { obj_.linksDecode(this); };
    };

    setTimeout(function(){
        obj_.renderControl();
    }, 200);
};

Melown.Presenter.prototype.Utils.readTextInput = function(id_) {
    var presentation_ = {
        htmlDataStorage : this.config_['presenter'][id_],
        id : id_,
        checkID : function() {
            var url_ = /^(ftp|http|https):\/\/[^ "]+$/;
            var relative_ = /.*\/+.*/;
            var level_ = /(\.\.)/g;
            var hash_ = /^#.*$/;
            if(url_.test(this.htmlDataStorage) || this.htmlDataStorage.indexOf("./") != -1) {
                return 'url';
            }
            else if(relative_.test(this.htmlDataStorage)) {
                var getLevel_;
                while((getLevel_ = level_.exec(this.htmlDataStorage)) !== null) {

                };
                console.log(getLevel_);
                return 'relative:';
            }
            else if(hash_.test(this.htmlDataStorage)) {
                return 'hash';
            }
            else {
                return 'string';
            };
        }
    };

    var mode_ = presentation_.checkID();

    if(mode_ == 'url' || mode_ == 'relative') {
        var rawFile_ = new XMLHttpRequest();
        var obj_ = this;
        if(mode_ == 'relative') {
            presentation_.htmlDataStorage = window.location.href + presentation_.htmlDataStorage;
        };
        rawFile_.open("GET", presentation_.htmlDataStorage, false);
        rawFile_.onreadystatechange = function() {
            if(rawFile_.readyState === 4) {
                if(rawFile_.status === 200 || rawFile_.status == 0) {
                    var allText_ = rawFile_.responseText;
                    obj_.html = allText_;
                    obj_.init(presentation_.id, obj_.html);
                }
                else {
                    obj_.file = 'undefined';
                };
            };
        };
        rawFile_.send(null);
    }
    else if(mode_ == 'hash') {
        var obj_ = document.getElementById(presentation_.htmlDataStorage).innerHTML;
        this.init(presentation_.id, obj_);
    }
    else if(mode_ == 'string') {
        this.init(presentation_.id, presentation_.htmlDataStorage);
    };
};

Melown.Presenter.prototype.Utils.linksDecode = function(obj_) {
    var position_ = null;
    var autorotate_ = null;
    var transition_ = null;
    var navigate_ = null;

    if(obj_.getAttribute('data-mln-navigate') !== null) {
        navigate_ = obj_.getAttribute('data-mln-navigate');
        if(navigate_ !== null) {
            if(navigate_ == 'prev') { this.nextArticle('-1'); }
            else if(navigate_ == 'next') { this.nextArticle('+1'); }
            else if(navigate_ == 'first') { this.nextArticle(0); }
            else if(navigate_ == 'last') { this.nextArticle(this.maxNodes_-1); }
            else { this.nextArticle(navigate_); };
            return 'navigation:true';
        };
    };

    if(obj_.getAttribute('data-mln-position') === null){
        return 'position:false';
    };

    position_ = this.getNumbers(obj_.getAttribute('data-mln-position').split(','));

    if(obj_.getAttribute('data-mln-autorotate') !== null) {
        autorotate_ = this.getNumbers(obj_.getAttribute('data-mln-autorotate'));
    };
    if(obj_.getAttribute('data-mln-transition') !== null) {
        transition_ = obj_.getAttribute('data-mln-transition');
    };

    if(transition_ === null) {
        this.browser_.autopilot_.flyTo(position_);
    }
    else if(transition_ == 'teleport') {
        this.browser_.core_.getMap().setPosition(position_);
    }
    else {
        this.browser_.autopilot_.flyTo(position_);
        // Feature to be considered
        // browser.flyTo(position, {mode_ : transition});
    };
    if(autorotate_ !== null) {
        this.browser_.autopilot_.setAutorotate(autorotate_);
    };

    return 'Moving to position: ' + position_;
};

// parseFloat here
Melown.Presenter.prototype.Utils.getNumbers = function(obj_) {
    var obj_ = obj_;
    for(var i = 0; i < obj_.length; i++){
        if(typeof obj_ == 'string' && parseFloat(obj_)) {
            obj_ = parseFloat(obj_);
            break;
        }
        if (parseFloat(obj_[i]))
            obj_[i] = parseFloat(obj_[i]); // toFixed might be added here
    }
    return obj_;
};

Melown.Presenter.prototype.Utils.nextArticle = function(node_, init_, lastNode_) {
    // fly to whatever node we wish
    if(node_ === '+1') { node_ = 1; }
    else if(node_ === '-1') { node_ = -1; }
    else {
        this.actualNode_ = node_;
        node_ = 0;
    };
    this.actualNode_ = this.actualNode_ + node_;

    if(this.actualNode_ >= 0 && this.actualNode_ < this.maxNodes_) {
        if(!init_) {
            if(this.activeToolbox_ == 'right') { this.handleArticle(this.actualNode_); }
            else if(this.activeToolbox_ == 'wide') { this.handleSubtitlesPosition(this.actualNode_); };
        };
        if(typeof lastNode_ !== 'undefined') { this.maxNodes_ = lastNode_; };
        this.linksDecode(this.container_.getElementsByTagName('section')[this.actualNode_]);
        return true;

    }
    else { this.actualNode_ = this.actualNode_ - node_; };
    return false;
};

Melown.Presenter.prototype.Utils.useToolbox = function() {

    var type_ = this.container_.getElementsByTagName('article')[0].getAttribute('data-mln-style');

    if(type_ === null) { type_ = 'right'; };

    var rightPanel_ = this.container_.getElementsByClassName('melown-presentations panelContainer')[0];
    var toolboxContainer_ = this.container_.getElementsByClassName('melown-presentations toolboxContainer')[0];
    var subtitles_ = this.container_.getElementsByClassName('melown-presentations subtitlesContainer')[0];
    var swipeControl_ = this.container_.getElementsByClassName('melown-presentations swipeControl');
    this.activeToolbox_ = type_;

    subtitles_.setAttribute('style', 'opacity: 0;');
    subtitles_.setAttribute('class', 'melown-presentations subtitlesContainer');
    if(type_ == 'right') {
        rightPanel_.style.display = 'block';
        setTimeout(function() {
            rightPanel_.style.opacity = 1;
        }, 20);
        swipeControl_[0].style.display = 'block';
        swipeControl_[1].style.display = 'block';
        for(var i = 0; i < this.sectionTags_.length; i++){ // Set maxHeight back as there is no dynamic rescaling of rightPanel_
            this.sectionTags_[i].style.height = this.maxHeight_ + 'px';
        };
        this.nextArticle(0);
    }
    else if(type_ == 'wide') {
        subtitles_.style.display = 'block';
        setTimeout(function() {
            subtitles_.style.opacity = 1;
        }, 20);
        rightPanel_.style.display = 'none';
        rightPanel_.style.opacity = 0;
        swipeControl_[0].style.display = 'none';
        swipeControl_[1].style.display = 'none';
        for(var i = 0; i < this.sectionTags_.length; i++){ // Set height to auto so we can dynamicaly adjust subtitles height
            this.sectionTags_[i].style.height = 'auto';
        };
        this.handleSubtitlesPosition(0, true);
    };
};

<|endoftext|>