<commit_before>"use strict";

var unloading = false;

$(function(){
    $("#body").replaceWith(createModes(mode));
    $(".link:first").click();
    $("#txt").focus();
    $("#txt").change(validate).keyup(validate);
    $(window).unload(function(){
        if (!unloading) send("/cancel");
    });
});

function send(url, val) // String -> Optional String -> Maybe String
{
    var res;
    $.ajax(
        {async:false
        ,data:{arg:val}
        ,dataType:"text"
        ,url:url
        ,success:function(x){res = x}});
    return res;
}

function validate()
{
    if (unloading) return;
    var s = $("#txt").val();
    var res = send("/check", $("#txt").val());
    if (res == "") {
        $("#err").hide();
        $('#ok').removeAttr('disabled');
    } else {
        $("#err-content").text(res);
        $("#err").show();
        $('#ok').attr('disabled', 'disabled');
    }
}

function clickOK()
{
    unloading = true;
    send("/ok", $("#txt").val());
    window.close();
    return false;
}

function clickCancel()
{
    unloading = true;
    send("/cancel");
    window.close();
}

var activeMode = "";

function space(a, b)
{
    return a === "" || b === "" ? a + b : a + " " + b;
}

function quote(s)
{
    return /^[A-Za-z0-9_=\-\"]+$/.test(s) ? s : "\"" + s + "\"";
}

function updateCmdLine()
{
    var res = activeMode;
    $(".main .part").each(function(){
        res = space(res, $(this).data("part")());
    });
    $("#txt").val(res);
    validate();
}

/////////////////////////////////////////////////////////////////////
// CONTENT CREATION

function createModes(m)
{
    $(".main input").live("input", updateCmdLine);
    return div([div({"class":"left"}, createTree(m,null))
              ,div({"class":"main"}, [])
              ,div({"class":"bottom"}, createCmdLine())
              ]);
}

function createTree(m,path)
{
    var path2 = path === null ? "" : space(path, m.names[0]);
    var xs = [];
    var lnk = div({"class":"link"},m.names[0]);
    xs.push(lnk);
    var mod = createMode(m);
    lnk.click(function(){
        $(".main").children().detach();
        $(".main").append(mod);
        $(".active").removeClass("active");
        lnk.addClass("active");
        activeMode = path2;
        updateCmdLine();
    });

    for (var i = 0; i < m.modes.unnamed.length; i++)
        xs.push(createTree(m.modes.unnamed[i], path2));
    for (var i = 0; i < m.modes.named.length; i++)
    {
        xs.push(bold(m.modes.named[i][0]));
        for (var j = 0; j < m.modes.named[i][1].length; j++)
            xs.push(createTree(m.modes.named[i][1][j], path2));
    }

    return div({"class":"group"},xs);
}

function createCmdLine()
{
    return $(
        "<form onsubmit='clickOK()'>" +
        "Command line arguments:
" +
        "<input type='text' id='txt' autocomplete='off'>
" +
        "<input type='submit' id='ok' style='font-weight:bold;' value='OK' />" +
        "<input type='button' value='Cancel' onclick='clickCancel()' />" +
        "
Error:
" +
        "</form>");
}

function createMode(m)
{
    var trs = [];
    function row(a,b,c)
    {
        var res = b === undefined
                  ? tr(td({colspan:3},a))
                  : tr({"class":"top"},[td({style:"padding-top:5px;text-align:right;"},a),td(b),td({"class":"doc",style:"padding-left:15px;padding-top:5px;"},c)]);
        trs.push(res);
        return res;
    }

    row(m.help);

    // Arguments
    var reqArgs = m.args[0];
    var optArgs = m.args[1];
    if (reqArgs.length !== 0 || optArgs !== null)
        row(h3("Arguments"));

    for (var i = 0; i < reqArgs.length; i++)
    {
        var a = reqArgs[i];
        row("Argument " + i, [], textField(a.type), a.require ? "required" : "");
    }
    if (optArgs !== null)
        row("Arguments", entries(optArgs.type, quote), [], optArgs.require ? "required" : "");

    function addFlag(flag)
    {
        var name = (flag.names[0].length === 1 ? "-" : "--") + flag.names[0];
        var it = italic({"class":"part"}, "No value");
        it.data("part", function(){return name;});

        var part = function(s){
            return quote(name + (flag.info.type === "rare" && s == "" ? "" : "=") + s);
        };
        row(tt(name), entries(flag.type, part, flag.info.type === "none" ? it : undefined), flag.help);
    }

    var unnamedFlags = m.flags.unnamed;
    if (unnamedFlags.length > 0)
        row(h3("Flags"));
    for (var i = 0; i < unnamedFlags.length; i++)
        addFlag(unnamedFlags[i]);

    var namedFlags = m.flags.named;
    for (var i = 0; i < namedFlags.length; i++)
    {
        row(h3(namedFlags[i][0]));
        for (var j = 0; j < namedFlags[i][1].length; j++)
            addFlag(namedFlags[i][1][j]);
    }

    if (m.helpSuffix.length > 0)
        row(h3("Help information"));
    for (var i = 0; i < m.helpSuffix.length; i++)
        row(div({style:"white-space:pre-wrap;"},escapeHTML(m.helpSuffix[i])));

    return table(trs);
}

function entries(help, part, custom)
{
    var add = addButton();
    var bot = tr([td(add), td({width:"150px","class":"doc",style:"padding-top:3px;"},help)]);
    add.click(function(){
        var del = delButton();
        var inp = input({"class":"part"},[]);
        inp.data("part", function(){return part(inp.val());});
        var x = tr([td(del), td(custom === undefined ? inp : custom.clone(true))]);
        bot.before(x);
        if (custom === undefined)
            inp.focus();
        del.click(function(){
            x.remove();
            updateCmdLine();
        });
        updateCmdLine();
    });
    return table({"class":"entries"},bot);
}

function textField(typ)
{
    return input({value:typ},[]);
}

function addButton()
{
    return div({"class":"add"},"")
}

function delButton()
{
    return div({"class":"del"},"")
}

/////////////////////////////////////////////////////////////////////
// TAG LIBRARY

function tag(name, attrs, content)
{
    if (content == undefined)
    {
        content = attrs;
        attrs = undefined;
    }
    var res = $("<" + name + ">");

    function addAttr(xs)
    {
        if ($.isArray(xs))
        {
            for (var i = 0; i < xs.length; i++)
                addAttr(xs[i]);
        }
        else
        {
            for (var i in xs)
                res.attr(i, xs[i]);
        }
    }

    function addContent(xs)
    {
        if ($.isArray(xs))
        {
            for (var i = 0; i < xs.length; i++)
                addContent(xs[i]);
        }
        else
            res.append(xs);
    }

    if (attrs !== undefined)
        addAttr(attrs);
    if (content !== undefined)
        addContent(content);
    return res;
}

function div(attrs, content){return tag("div", attrs, content);}
function tt(attrs, content){return tag("tt", attrs, content);}
function td(attrs, content){return tag("td", attrs, content);}
function tr(attrs, content){return tag("tr", attrs, content);}
function table(attrs, content){return tag("table", attrs, content);}
function h3(attrs, content){return tag("h3", attrs, content);}
function italic(attrs, content){return tag("i", attrs, content);}
function bold(attrs, content){return tag("b", attrs, content);}
function input(attrs, content){return tag("input", attrs, content);}
function hyperlink(attrs, content){return tag("a", attrs, content);}

function escapeHTML(x)
{
    return x;
}
<commit_msg>Fix cmdargs-browser for newer jquery<commit_after>"use strict";

var unloading = false;

$(function(){
    $("#body").replaceWith(createModes(mode));
    $(".link:first").click();
    $("#txt").focus();
    $("#txt").change(validate).keyup(validate);
    $(window).unload(function(){
        if (!unloading) send("/cancel");
    });
});

function send(url, val) // String -> Optional String -> Maybe String
{
    var res;
    $.ajax(
        {async:false
        ,data:{arg:val}
        ,dataType:"text"
        ,url:url
        ,success:function(x){res = x}});
    return res;
}

function validate()
{
    if (unloading) return;
    var s = $("#txt").val();
    var res = send("/check", $("#txt").val());
    if (res == "") {
        $("#err").hide();
        $('#ok').removeAttr('disabled');
    } else {
        $("#err-content").text(res);
        $("#err").show();
        $('#ok').attr('disabled', 'disabled');
    }
}

function clickOK()
{
    unloading = true;
    send("/ok", $("#txt").val());
    window.close();
    return false;
}

function clickCancel()
{
    unloading = true;
    send("/cancel");
    window.close();
}

var activeMode = "";

function space(a, b)
{
    return a === "" || b === "" ? a + b : a + " " + b;
}

function quote(s)
{
    return /^[A-Za-z0-9_=\-\"]+$/.test(s) ? s : "\"" + s + "\"";
}

function updateCmdLine()
{
    var res = activeMode;
    $(".main .part").each(function(){
        res = space(res, $(this).data("part")());
    });
    $("#txt").val(res);
    validate();
}

/////////////////////////////////////////////////////////////////////
// CONTENT CREATION

function createModes(m)
{
    $(document).on("input", ".main input", updateCmdLine);
    return div([div({"class":"left"}, createTree(m,null))
              ,div({"class":"main"}, [])
              ,div({"class":"bottom"}, createCmdLine())
              ]);
}

function createTree(m,path)
{
    var path2 = path === null ? "" : space(path, m.names[0]);
    var xs = [];
    var lnk = div({"class":"link"},m.names[0]);
    xs.push(lnk);
    var mod = createMode(m);
    lnk.click(function(){
        $(".main").children().detach();
        $(".main").append(mod);
        $(".active").removeClass("active");
        lnk.addClass("active");
        activeMode = path2;
        updateCmdLine();
    });

    for (var i = 0; i < m.modes.unnamed.length; i++)
        xs.push(createTree(m.modes.unnamed[i], path2));
    for (var i = 0; i < m.modes.named.length; i++)
    {
        xs.push(bold(m.modes.named[i][0]));
        for (var j = 0; j < m.modes.named[i][1].length; j++)
            xs.push(createTree(m.modes.named[i][1][j], path2));
    }

    return div({"class":"group"},xs);
}

function createCmdLine()
{
    return $(
        "<form onsubmit='clickOK()'>" +
        "Command line arguments:
" +
        "<input type='text' id='txt' autocomplete='off'>
" +
        "<input type='submit' id='ok' style='font-weight:bold;' value='OK' />" +
        "<input type='button' value='Cancel' onclick='clickCancel()' />" +
        "
Error:
" +
        "</form>");
}

function createMode(m)
{
    var trs = [];
    function row(a,b,c)
    {
        var res = b === undefined
                  ? tr(td({colspan:3},a))
                  : tr({"class":"top"},[td({style:"padding-top:5px;text-align:right;"},a),td(b),td({"class":"doc",style:"padding-left:15px;padding-top:5px;"},c)]);
        trs.push(res);
        return res;
    }

    row(m.help);

    // Arguments
    var reqArgs = m.args[0];
    var optArgs = m.args[1];
    if (reqArgs.length !== 0 || optArgs !== null)
        row(h3("Arguments"));

    for (var i = 0; i < reqArgs.length; i++)
    {
        var a = reqArgs[i];
        row("Argument " + i, [], textField(a.type), a.require ? "required" : "");
    }
    if (optArgs !== null)
        row("Arguments", entries(optArgs.type, quote), [], optArgs.require ? "required" : "");

    function addFlag(flag)
    {
        var name = (flag.names[0].length === 1 ? "-" : "--") + flag.names[0];
        var it = italic({"class":"part"}, "No value");
        it.data("part", function(){return name;});

        var part = function(s){
            return quote(name + (flag.info.type === "rare" && s == "" ? "" : "=") + s);
        };
        row(tt(name), entries(flag.type, part, flag.info.type === "none" ? it : undefined), flag.help);
    }

    var unnamedFlags = m.flags.unnamed;
    if (unnamedFlags.length > 0)
        row(h3("Flags"));
    for (var i = 0; i < unnamedFlags.length; i++)
        addFlag(unnamedFlags[i]);

    var namedFlags = m.flags.named;
    for (var i = 0; i < namedFlags.length; i++)
    {
        row(h3(namedFlags[i][0]));
        for (var j = 0; j < namedFlags[i][1].length; j++)
            addFlag(namedFlags[i][1][j]);
    }

    if (m.helpSuffix.length > 0)
        row(h3("Help information"));
    for (var i = 0; i < m.helpSuffix.length; i++)
        row(div({style:"white-space:pre-wrap;"},escapeHTML(m.helpSuffix[i])));

    return table(trs);
}

function entries(help, part, custom)
{
    var add = addButton();
    var bot = tr([td(add), td({width:"150px","class":"doc",style:"padding-top:3px;"},help)]);
    add.click(function(){
        var del = delButton();
        var inp = input({"class":"part"},[]);
        inp.data("part", function(){return part(inp.val());});
        var x = tr([td(del), td(custom === undefined ? inp : custom.clone(true))]);
        bot.before(x);
        if (custom === undefined)
            inp.focus();
        del.click(function(){
            x.remove();
            updateCmdLine();
        });
        updateCmdLine();
    });
    return table({"class":"entries"},bot);
}

function textField(typ)
{
    return input({value:typ},[]);
}

function addButton()
{
    return div({"class":"add"},"")
}

function delButton()
{
    return div({"class":"del"},"")
}

/////////////////////////////////////////////////////////////////////
// TAG LIBRARY

function tag(name, attrs, content)
{
    if (content == undefined)
    {
        content = attrs;
        attrs = undefined;
    }
    var res = $("<" + name + ">");

    function addAttr(xs)
    {
        if ($.isArray(xs))
        {
            for (var i = 0; i < xs.length; i++)
                addAttr(xs[i]);
        }
        else
        {
            for (var i in xs)
                res.attr(i, xs[i]);
        }
    }

    function addContent(xs)
    {
        if ($.isArray(xs))
        {
            for (var i = 0; i < xs.length; i++)
                addContent(xs[i]);
        }
        else
            res.append(xs);
    }

    if (attrs !== undefined)
        addAttr(attrs);
    if (content !== undefined)
        addContent(content);
    return res;
}

function div(attrs, content){return tag("div", attrs, content);}
function tt(attrs, content){return tag("tt", attrs, content);}
function td(attrs, content){return tag("td", attrs, content);}
function tr(attrs, content){return tag("tr", attrs, content);}
function table(attrs, content){return tag("table", attrs, content);}
function h3(attrs, content){return tag("h3", attrs, content);}
function italic(attrs, content){return tag("i", attrs, content);}
function bold(attrs, content){return tag("b", attrs, content);}
function input(attrs, content){return tag("input", attrs, content);}
function hyperlink(attrs, content){return tag("a", attrs, content);}

function escapeHTML(x)
{
    return x;
}
<|endoftext|>


<commit_before>var map = null;
var initZoom = 12;
var oms = null;

var ggIcon = L.icon({
	iconUrl: 'style/img/gg-icon.png',
	iconSize: [40, 40],
	shadowSize: [0, 0]
});

function main(geoip){
	var coords = geoip.location;
	var zoom = initZoom;
	map = L.map('map').setView([coords.latitude, coords.longitude], zoom);
	var tile = L.tileLayer('http://otile{s}.mqcdn.com/tiles/1.0.0/sat/{z}/{x}/{y}.jpg', {
		attribution: 'OpenStreetMap',
		subdomains: '1234'
	});
	//var oms = new OverlappingMarkerSpiderfier(map);
	tile.addTo(map);
	var marker = L.marker([coords.latitude, coords.longitude]);
	marker.addTo(map);
	marker.bindPopup('
Your Location
').openPopup();
	//getFirebaseGraffiti();
	getGraffiti({}, 100, function(list){
		plotGraffiti(list.slice(30));
		listenToFirebase();
	}, 'location-markers');
}

function plotGraffiti(list){
	for(var i = 0; i < list.length; i++){
		var g = list[i];
		var html = '
';
			html += '
' + g.surface + '
';
			html += '
opened ' + moment(g.requested).fromNow() + '
';
			html += '
';
		var marker = L.marker([g.lat(), g.lon()]);
		marker.addTo(map);
		marker.bindPopup(html);
	}
}

function getFirebaseGraffiti(){
	getGraffiti({}, 50, function(list){
		var fb = new Firebase('https://firedates.firebaseio.com/graffiti');
		fb.once('value', function(snapshot){
			var data = snapshot.val();
			for(var i in data){
				var g = list.pop();
				var html = '';
				var marker = L.marker([g.lat(), g.lon()], {icon: ggIcon});
				marker.addTo(map);
				marker.bindPopup(html);
			}
			plotGraffiti(list);
		});
	})
}

function listenToFirebase(){
	var fb = new Firebase('https://firedates.firebaseio.com/graffiti');
	fb.on('child_added', function(childSnapshot, prevChildKey){
		var data = childSnapshot.val();
		var g = storage.get('location-markers').pop();
		var html = '';
		var marker = L.marker([g.lat(), g.lon()], {
			icon: ggIcon,
			riseOnHover: true
		});
		marker.addTo(map);
		map.panTo(new L.LatLng(g.lat(), g.lon()));
		marker.bindPopup(html);
		marker.openPopup();
	});
}<commit_msg>Increase list call<commit_after>var map = null;
var initZoom = 12;
var oms = null;

var ggIcon = L.icon({
	iconUrl: 'style/img/gg-icon.png',
	iconSize: [40, 40],
	shadowSize: [0, 0]
});

function main(geoip){
	var coords = geoip.location;
	var zoom = initZoom;
	map = L.map('map').setView([coords.latitude, coords.longitude], zoom);
	var tile = L.tileLayer('http://otile{s}.mqcdn.com/tiles/1.0.0/sat/{z}/{x}/{y}.jpg', {
		attribution: 'OpenStreetMap',
		subdomains: '1234'
	});
	//var oms = new OverlappingMarkerSpiderfier(map);
	tile.addTo(map);
	var marker = L.marker([coords.latitude, coords.longitude]);
	marker.addTo(map);
	marker.bindPopup('
Your Location
').openPopup();
	//getFirebaseGraffiti();
	getGraffiti({}, 300, function(list){
		plotGraffiti(list.slice(100));
		listenToFirebase();
	}, 'location-markers');
}

function plotGraffiti(list){
	for(var i = 0; i < list.length; i++){
		var g = list[i];
		var html = '
';
			html += '
' + g.surface + '
';
			html += '
opened ' + moment(g.requested).fromNow() + '
';
			html += '
';
		var marker = L.marker([g.lat(), g.lon()]);
		marker.addTo(map);
		marker.bindPopup(html);
	}
}

function getFirebaseGraffiti(){
	getGraffiti({}, 50, function(list){
		var fb = new Firebase('https://firedates.firebaseio.com/graffiti');
		fb.once('value', function(snapshot){
			var data = snapshot.val();
			for(var i in data){
				var g = list.pop();
				var html = '';
				var marker = L.marker([g.lat(), g.lon()], {icon: ggIcon});
				marker.addTo(map);
				marker.bindPopup(html);
			}
			plotGraffiti(list);
		});
	})
}

function listenToFirebase(){
	var fb = new Firebase('https://firedates.firebaseio.com/graffiti');
	fb.on('child_added', function(childSnapshot, prevChildKey){
		var data = childSnapshot.val();
		var g = storage.get('location-markers').pop();
		var html = '';
		var marker = L.marker([g.lat(), g.lon()], {
			icon: ggIcon,
			riseOnHover: true
		});
		marker.addTo(map);
		map.panTo(new L.LatLng(g.lat(), g.lon()));
		marker.bindPopup(html);
		marker.openPopup();
	});
}<|endoftext|>


<commit_before>'use strict';

const assert = require('assert');
const CronJob = require('cron').CronJob;
const Config = require('../config');
const Storage = require('storj-service-storage-models');
const log = require('../logger');

function StorageEventsCron(config) {
  if (!(this instanceof StorageEventsCron)) {
    return new StorageEventsCron(config);
  }

  assert(config instanceof Config, 'Invalid config supplied');

  this._config = config;
}

StorageEventsCron.CRON_TIME = '* */10 * * * *'; // every ten minutes
StorageEventsCron.MAX_RUN_TIME = 600000; // 10 minutes
StorageEventsCron.FINALITY_TIME = 10800000; // 3 hours

StorageEventsCron.prototype.start = function(callback) {
  log.info('starting the storage events cron');

  this.storage = new Storage(
    this._config.storage.mongoUrl,
    this._config.storage.mongoOpts,
    { logger: log }
  );

  this.job = new CronJob({
    cronTime: StorageEventsCron.CRON_TIME,
    onTick: this.run.bind(this),
    start: false,
    timeZone: 'UTC'
  });

  this.job.start();

  setImmediate(callback);
};

StorageEventsCron.prototype._resolveCodes = function(event, user) {
  const threshold = this._config.application.unknownReportThreshold;

  let success = event.success;
  let successModified = false;
  let unknown = success ? false : true;
  if (unknown) {
    resolveCodes();
  }

  function resolveCodes() {
    const failureCode = 1100;

    const clientCode = event.clientReport ?
          event.clientReport.exchangeResultCode : undefined;
    const farmerCode = event.farmerReport ?
          event.farmerReport.exchangeResultCode : undefined;

    /* jshint eqeqeq: false */
    if (farmerCode == failureCode && !clientCode) {
      success = false;
      unknown = false;
      return;
    }

    if (farmerCode == failureCode && clientCode == failureCode) {
      success = false;
      unknown = false;
      return;
    }

    if (!farmerCode && clientCode == failureCode) {
      success = false;
      unknown = false;
      return;
    }

    if (user.exceedsUnknownReportsThreshold(threshold)) {
      successModified = true;
      success = true;
      unknown = true;
      return;
    }
  }

  return {
    success: success,
    successModified: successModified,
    unknown: unknown
  };

};

StorageEventsCron.prototype._resolveEvent = function(event, callback) {

  this.storage.models.User.findOne({_id: event.user}, (err, user) => {
    if (err) {
      return callback(err);
    }

    const {success, successModified, unknown} = this._resolveCodes(event);

    if (successModified) {
      // TODO also give reputation points to farmer for successful
      // transfer for the storage event
      event.success = success;
      event.save((err) => {
        if (err) {
          return callback(err);
        }
        finalize();
      });
    } else {
      finalize();
    }

    function finalize() {
      user.updateUnknownReports(unknown, event.timestamp, (err) => {
        if (err) {
          return callback(err);
        }

        callback(null, event.timestamp);
      });
    }
  });
};

StorageEventsCron.prototype._run = function(lastTimestamp, callback) {

  const StorageEvent = this.storage.models.StorageEvent;
  const finalityTime = new Date(Date.now() - StorageEventsCron.FINALITY_TIME);

  const cursor = StorageEvent.find({
    timestamp: {
      $lt: finalityTime,
      $gt: lastTimestamp
    },
    user: {
      $exists: true,
      $ne: null
    }
  }).sort({timestamp: 1}).cursor();

  const timeout = setTimeout(() => {
    finish(new Error('Job exceeded max duration'));
  }, StorageEventsCron.MAX_RUN_TIME);

  let callbackCalled = false;

  function finish(err) {
    clearTimeout(timeout);
    cursor.close();
    if (!callbackCalled) {
      callbackCalled = true;
      callback(err, lastTimestamp);
    }
  }

  cursor.on('error', finish);

  cursor.on('data', (event) => {
    cursor.pause();
    this._resolveEvent(event, (err, _lastTimestamp) => {
      if (err) {
        return finish(err);
      }
      lastTimestamp = _lastTimestamp;
      cursor.resume();
    });
  });

  cursor.on('end', finish);
};

StorageEventsCron.prototype.run = function() {
  const name = 'StorageEventsFinalityCron';
  const Cron = this.storage.models.CronJob;
  Cron.lock(name, StorageEventsCron.MAX_RUN_TIME, (err, locked, res) => {
    if (err) {
      return log.error('%s lock failed, reason: %s', name, err.message);
    }
    if (!locked) {
      return log.warn('%s already running', name);
    }

    log.info('Starting %s cron job', name);

    let lastTimestamp = new Date(0);
    if (res &&
        res.value &&
        res.value.rawData &&
        res.value.rawData.lastTimestamp) {
      lastTimestamp = new Date(res.value.rawData.lastTimestamp);
    } else {
      log.warn('%s cron has unknown lastTimestamp', name);
    }

    this._run(lastTimestamp, (err, _lastTimestamp) => {
      if (err) {
        let message = err.message ? err.message : 'unknown';
        log.error('Error running %s, reason: %s', name, message);
      }

      log.info('Stopping %s cron', name);
      const rawData = {};
      if (_lastTimestamp) {
        rawData.lastTimestamp = _lastTimestamp.getTime();
      }

      Cron.unlock(name, rawData, (err) => {
        if (err) {
          return log.error('%s unlock failed, reason: %s', name, err.message);
        }
      });

    });
  });
};

module.exports = StorageEventsCron;
<commit_msg>Add missing argument<commit_after>'use strict';

const assert = require('assert');
const CronJob = require('cron').CronJob;
const Config = require('../config');
const Storage = require('storj-service-storage-models');
const log = require('../logger');

function StorageEventsCron(config) {
  if (!(this instanceof StorageEventsCron)) {
    return new StorageEventsCron(config);
  }

  assert(config instanceof Config, 'Invalid config supplied');

  this._config = config;
}

StorageEventsCron.CRON_TIME = '* */10 * * * *'; // every ten minutes
StorageEventsCron.MAX_RUN_TIME = 600000; // 10 minutes
StorageEventsCron.FINALITY_TIME = 10800000; // 3 hours

StorageEventsCron.prototype.start = function(callback) {
  log.info('starting the storage events cron');

  this.storage = new Storage(
    this._config.storage.mongoUrl,
    this._config.storage.mongoOpts,
    { logger: log }
  );

  this.job = new CronJob({
    cronTime: StorageEventsCron.CRON_TIME,
    onTick: this.run.bind(this),
    start: false,
    timeZone: 'UTC'
  });

  this.job.start();

  setImmediate(callback);
};

StorageEventsCron.prototype._resolveCodes = function(event, user) {
  const threshold = this._config.application.unknownReportThreshold;

  let success = event.success;
  let successModified = false;
  let unknown = success ? false : true;
  if (unknown) {
    resolveCodes();
  }

  function resolveCodes() {
    const failureCode = 1100;

    const clientCode = event.clientReport ?
          event.clientReport.exchangeResultCode : undefined;
    const farmerCode = event.farmerReport ?
          event.farmerReport.exchangeResultCode : undefined;

    /* jshint eqeqeq: false */
    if (farmerCode == failureCode && !clientCode) {
      success = false;
      unknown = false;
      return;
    }

    if (farmerCode == failureCode && clientCode == failureCode) {
      success = false;
      unknown = false;
      return;
    }

    if (!farmerCode && clientCode == failureCode) {
      success = false;
      unknown = false;
      return;
    }

    if (user.exceedsUnknownReportsThreshold(threshold)) {
      successModified = true;
      success = true;
      unknown = true;
      return;
    }
  }

  return {
    success: success,
    successModified: successModified,
    unknown: unknown
  };

};

StorageEventsCron.prototype._resolveEvent = function(event, callback) {

  this.storage.models.User.findOne({_id: event.user}, (err, user) => {
    if (err) {
      return callback(err);
    }

    const {success, successModified, unknown} = this._resolveCodes(event, user);

    if (successModified) {
      // TODO also give reputation points to farmer for successful
      // transfer for the storage event
      event.success = success;
      event.save((err) => {
        if (err) {
          return callback(err);
        }
        finalize();
      });
    } else {
      finalize();
    }

    function finalize() {
      user.updateUnknownReports(unknown, event.timestamp, (err) => {
        if (err) {
          return callback(err);
        }

        callback(null, event.timestamp);
      });
    }
  });
};

StorageEventsCron.prototype._run = function(lastTimestamp, callback) {

  const StorageEvent = this.storage.models.StorageEvent;
  const finalityTime = new Date(Date.now() - StorageEventsCron.FINALITY_TIME);

  const cursor = StorageEvent.find({
    timestamp: {
      $lt: finalityTime,
      $gt: lastTimestamp
    },
    user: {
      $exists: true,
      $ne: null
    }
  }).sort({timestamp: 1}).cursor();

  const timeout = setTimeout(() => {
    finish(new Error('Job exceeded max duration'));
  }, StorageEventsCron.MAX_RUN_TIME);

  let callbackCalled = false;

  function finish(err) {
    clearTimeout(timeout);
    cursor.close();
    if (!callbackCalled) {
      callbackCalled = true;
      callback(err, lastTimestamp);
    }
  }

  cursor.on('error', finish);

  cursor.on('data', (event) => {
    cursor.pause();
    this._resolveEvent(event, (err, _lastTimestamp) => {
      if (err) {
        return finish(err);
      }
      lastTimestamp = _lastTimestamp;
      cursor.resume();
    });
  });

  cursor.on('end', finish);
};

StorageEventsCron.prototype.run = function() {
  const name = 'StorageEventsFinalityCron';
  const Cron = this.storage.models.CronJob;
  Cron.lock(name, StorageEventsCron.MAX_RUN_TIME, (err, locked, res) => {
    if (err) {
      return log.error('%s lock failed, reason: %s', name, err.message);
    }
    if (!locked) {
      return log.warn('%s already running', name);
    }

    log.info('Starting %s cron job', name);

    let lastTimestamp = new Date(0);
    if (res &&
        res.value &&
        res.value.rawData &&
        res.value.rawData.lastTimestamp) {
      lastTimestamp = new Date(res.value.rawData.lastTimestamp);
    } else {
      log.warn('%s cron has unknown lastTimestamp', name);
    }

    this._run(lastTimestamp, (err, _lastTimestamp) => {
      if (err) {
        let message = err.message ? err.message : 'unknown';
        log.error('Error running %s, reason: %s', name, message);
      }

      log.info('Stopping %s cron', name);
      const rawData = {};
      if (_lastTimestamp) {
        rawData.lastTimestamp = _lastTimestamp.getTime();
      }

      Cron.unlock(name, rawData, (err) => {
        if (err) {
          return log.error('%s unlock failed, reason: %s', name, err.message);
        }
      });

    });
  });
};

module.exports = StorageEventsCron;
<|endoftext|>


<commit_before>Ext.define('Cetera.panel.Materials', {

    extend:'Ext.grid.Panel',

    mat_type: 0,

	requires: 'Cetera.model.Material',

    border: false,
    stripeRows: true,
    multiSelect: true,

    columns: [
        {
            header: "",
            width: 25,
            sortable: false,
            dataIndex: 'icon',
            renderer: function (value) {
                if (value == '1')
                    return '';
                    else return '';
            }
        },
        {header: Config.Lang.srt, width: 50, dataIndex: 'tag'},
        {header: "ID", width: 50, dataIndex: 'id'},
        {
			header: Config.Lang.title, width: 75, dataIndex: 'name', flex:1,
            renderer: function (value,meta,rec) {
				if (rec.get('locked'))
				{
					value += '
' + Ext.String.format(Config.Lang.materialLocked, rec.get('locked_login')) + '';
				}
                return value;
            }
		},
        {header: "Alias", width: 175, dataIndex: 'alias'},
        {header: Config.Lang.date, width: 105, dataIndex: 'dat', renderer: Ext.util.Format.dateRenderer('d.m.Y H:i')},
        {header: Config.Lang.author, width: 100, dataIndex: 'autor'},
		{
			header: Config.Lang.catalog,
			width: 100,
			dataIndex: 'catalog',
			getSortParam: function(){ return 'E.name'; }
		}
    ],

    reload: function() {
        this.store.load({params:{start: 0}});
    },

	editorClass: function() {
		return 'MaterialEditor' + this.mat_type.charAt(0).toUpperCase() + this.mat_type.substr(1, this.mat_type.length-1 );
	},

    edit: function(id) {
        if (this.editWindow) this.editWindow.destroy();
        this.editWindow = Ext.create('Cetera.window.MaterialEdit', {
            listeners: {
                close: {
                    fn: function(win){
                        this.store.load();
                        this.stopInactivityTimer();
                    },
                    scope: this
                }
            }
        });
        this.editWindow.preview = this.preview;

        var win = this.editWindow;
        win.show();

        Ext.Loader.loadScript({
            url: 'include/ui_material_edit.php?type='+this.mat_type+'&id='+id+'&height='+this.editWindow.height,
			scope: this,
            onLoad: function() {
                var cc = Ext.create( this.editorClass() , {win: win});
                if (cc) {
					cc.show();
					this.fireEvent('material_editor_ready', win, cc);
				}
            }
        });

        // Таймер неактивности
        this.clearInactivityTimeout();
        this.timeoutTask = Ext.TaskManager.start({
             run: function() {

                if (this.globalTimeout <= 0) {
                    // таймаут сработал, останавливаем таймер
                    this.stopInactivityTimer();
                    this.editWindow.materialForm.saveAction(0,0,1);
                } else {
                    this.globalTimeout = this.globalTimeout - 1;
                }

             },
             interval: 1000,
             scope: this
        });

        Ext.EventManager.addListener("main_body", 'click', this.clearInactivityTimeout, this);
        Ext.EventManager.addListener("main_body", 'keypress', this.clearInactivityTimeout, this);

    },

    clearInactivityTimeout: function() {
        this.globalTimeout = 28800;
    },

    stopInactivityTimer: function() {
         Ext.TaskManager.stop(this.timeoutTask);
    },

    deleteMat: function() {
        Ext.MessageBox.confirm(Config.Lang.materialDelete, Config.Lang.r_u_sure, function(btn) {
            if (btn == 'yes') this.call('delete');
        }, this);
    },

    call: function(action, cat) {
        Ext.Ajax.request({
            url: 'include/action_materials.php',
            params: {
                action: action,
                type: this.mat_type,
                'sel[]': this.getSelected(),
                cat: cat
            },
            scope: this,
            success: function(resp) {
                this.store.load();
            }
        });
    },

    move: function(action) {

        var title = Config.Lang.move;
        if (action == 'copy')
            title = Config.Lang.copy;

        if (!this.siteTree) {
            this.siteTree = Ext.create('Cetera.window.SiteTree', {
                expand   : '/root/item-0',
                nolink : 1,
                rule   : '5u6',
				norootselect: 1,
                only   : this.mat_type
            });
            this.siteTree.on('select', function(res) {
                this.call(this.siteTree.action, res.id)
            },this);
        }
        this.siteTree.action = action;
        this.siteTree.setTitle(title);
        this.siteTree.show();
    },

    getSelected: function() {
        var a = this.getSelectionModel().getSelection();
        ret = [];
        for (var i=0; i<a.length; i++) ret[i] = a[i].getId();
        return ret;
    },

	getToolbar: function() {
        return Ext.create('Ext.toolbar.Toolbar', {items: [
            {
                itemId:  'tb_mat_new',
                iconCls: 'icon-new',
                tooltip: Config.Lang.newMaterial,
                handler: function () { this.edit(0); },
                scope:   this
            },
            {
                itemId: 'tb_mat_edit',
                disabled: true,
                iconCls:'icon-edit',
                tooltip: Config.Lang.edit,
                handler: function () { this.edit(this.getSelectionModel().getSelection()[0].getId()); },
                scope: this
            },
            {
                itemId: 'tb_mat_delete',
                disabled: true,
                iconCls:'icon-delete',
                tooltip: Config.Lang.delete,
                handler: function () { this.deleteMat(); },
                scope: this
            },
            '-',
            {
                itemId: 'tb_mat_pub',
                disabled: true,
                iconCls:'icon-pub',
                tooltip: Config.Lang.publish,
                handler: function() { this.call('pub'); },
                scope: this
            },
            {
                itemId: 'tb_mat_unpub',
                disabled: true,
                iconCls:'icon-unpub',
                tooltip: Config.Lang.unpublish,
                handler: function() { this.call('unpub'); },
                scope: this
            },
            '-',
            {
                itemId: 'tb_mat_move',
                disabled: true,
                iconCls:'icon-move',
                tooltip: Config.Lang.move,
                handler: function () { this.move('move'); },
                scope: this
            },
            {
                itemId: 'tb_mat_copy',
                disabled: true,
                iconCls:'icon-copy',
                tooltip: Config.Lang.copy,
                handler: function () { this.move('copy'); },
                scope: this
            }
        ]});
	},

	viewConfig: {
		getRowClass: function(record){
			return record.get("locked") ? "locked" : "";
		}
	},

    initComponent : function() {

		this.store = Ext.create('Ext.data.JsonStore', {

			autoDestroy: true,
			remoteSort: true,

			model: Cetera.model.Material,

			totalProperty: 'total',
			pageSize: Config.defaultPageSize,
			sorters: [{property: "dat", direction: "DESC"}],
			proxy: {
				type: 'ajax',
				url: 'include/data_materials.php',
				simpleSortMode: true,
				reader: {
					type: 'json',
					root: 'rows'
				},
				extraParams: {
					'id'   : 0,
					'type' : this.mat_type,
					limit  : Config.defaultPageSize
				}
			}

		});

        this.bbar = Ext.create('Ext.PagingToolbar', {
            store: this.store,
            items: [Config.Lang.filter + ': ', Ext.create('Cetera.field.Search', {
                store: this.store,
                paramName: 'query',
                width:200
            })]
        });

        this.tbar = this.getToolbar();
		this.toolbar = this.tbar;

        this.callParent();

        this.getSelectionModel().on({
            'selectionchange' : function(sm){
                var hs = sm.hasSelection();
                var sf = this.store.sorters.first().property;

                this.toolbar.getComponent('tb_mat_edit').setDisabled(!hs);
                this.toolbar.getComponent('tb_mat_delete').setDisabled(!hs);
                this.toolbar.getComponent('tb_mat_pub').setDisabled(!hs);
                this.toolbar.getComponent('tb_mat_unpub').setDisabled(!hs);
                this.toolbar.getComponent('tb_mat_move').setDisabled(!hs);
                this.toolbar.getComponent('tb_mat_copy').setDisabled(!hs);
            },
            'beforeselect' : function(t , record, index, eOpts) {
                if (record.get('disabled')) return false;
            },
            scope:this
        });

		if (this.mat_type)
		{
			this.on({
				'celldblclick' : function() {
					this.edit(this.getSelectionModel().getSelection()[0].getId());
				},
				scope: this
			});
			this.reload();
		}

    }

});<commit_msg>refactoring<commit_after>Ext.define('Cetera.panel.Materials', {

    extend:'Ext.grid.Panel',

    mat_type: 0,

	requires: 'Cetera.model.Material',

    border: false,
    stripeRows: true,
    multiSelect: true,

    columns: [
        {
            header: "",
            width: 25,
            sortable: false,
            dataIndex: 'icon',
            renderer: function (value) {
                if (value == '1')
                    return '';
                    else return '';
            }
        },
        {header: Config.Lang.srt, width: 50, dataIndex: 'tag'},
        {header: "ID", width: 50, dataIndex: 'id'},
        {
			header: Config.Lang.title, width: 75, dataIndex: 'name', flex:1,
            renderer: function (value,meta,rec) {
				if (rec.get('locked'))
				{
					value += '
' + Ext.String.format(Config.Lang.materialLocked, rec.get('locked_login')) + '';
				}
                return value;
            }
		},
        {header: "Alias", width: 175, dataIndex: 'alias'},
        {header: Config.Lang.date, width: 105, dataIndex: 'dat', renderer: Ext.util.Format.dateRenderer('d.m.Y H:i')},
        {header: Config.Lang.author, width: 100, dataIndex: 'autor'},
		{
			header: Config.Lang.catalog,
			width: 100,
			dataIndex: 'catalog',
			getSortParam: function(){ return 'E.name'; }
		}
    ],

    reload: function() {
        this.store.load({params:{start: 0}});
    },

	editorClass: function() {
		return 'MaterialEditor' + this.mat_type.charAt(0).toUpperCase() + this.mat_type.substr(1, this.mat_type.length-1 );
	},

    edit: function(id) {
        if (this.editWindow) this.editWindow.destroy();
        this.editWindow = Ext.create('Cetera.window.MaterialEdit', {
            listeners: {
                close: {
                    fn: function(win){
                        this.store.load();
                        this.stopInactivityTimer();
                    },
                    scope: this
                }
            }
        });
        this.editWindow.preview = this.preview;

        var win = this.editWindow;
        win.show();

        Ext.Loader.loadScript({
            url: 'include/ui_material_edit.php?type='+this.mat_type+'&id='+id+'&height='+this.editWindow.height,
			scope: this,
            onLoad: function() {
                var cc = Ext.create( this.editorClass() , {win: win});
                if (cc) {
					cc.show();
					this.fireEvent('material_editor_ready', win, cc);
				}
            }
        });

        // Таймер неактивности
        this.clearInactivityTimeout();
        this.timeoutTask = Ext.TaskManager.start({
             run: function() {

                if (this.globalTimeout <= 0) {
                    // таймаут сработал, останавливаем таймер
                    this.stopInactivityTimer();
                    this.editWindow.materialForm.saveAction(0,0,1);
                } else {
                    this.globalTimeout = this.globalTimeout - 1;
                }

             },
             interval: 1000,
             scope: this
        });

        Ext.EventManager.addListener("main_body", 'click', this.clearInactivityTimeout, this);
        Ext.EventManager.addListener("main_body", 'keypress', this.clearInactivityTimeout, this);

    },

    clearInactivityTimeout: function() {
        this.globalTimeout = 28800;
    },

    stopInactivityTimer: function() {
         Ext.TaskManager.stop(this.timeoutTask);
    },

    deleteMat: function() {
        Ext.MessageBox.confirm(Config.Lang.materialDelete, Config.Lang.r_u_sure, function(btn) {
            if (btn == 'yes') this.call('delete');
        }, this);
    },

    call: function(action, cat) {
        Ext.Ajax.request({
            url: 'include/action_materials.php',
            params: {
                action: action,
                type: this.mat_type,
                'sel[]': this.getSelected(),
                cat: cat
            },
            scope: this,
            success: function(resp) {
                this.store.load();
            }
        });
    },

    move: function(action) {

        var title = Config.Lang.move;
        if (action == 'copy')
            title = Config.Lang.copy;

        if (!this.siteTree) {
            this.siteTree = Ext.create('Cetera.window.SiteTree', {
                expand   : '/root/item-0',
                nolink : 1,
                rule   : '5u6',
				norootselect: 1,
                only   : this.mat_type
            });
            this.siteTree.on('select', function(res) {
                this.call(this.siteTree.action, res.id)
            },this);
        }
        this.siteTree.action = action;
        this.siteTree.setTitle(title);
        this.siteTree.show();
    },

    getSelected: function() {
        var a = this.getSelectionModel().getSelection();
        ret = [];
        for (var i=0; i<a.length; i++) ret[i] = a[i].getId();
        return ret;
    },

	getToolbar: function() {
        return Ext.create('Ext.toolbar.Toolbar', {items: [
            {
                itemId:  'tb_mat_new',
                iconCls: 'icon-new',
                tooltip: Config.Lang.newMaterial,
                handler: function () { this.edit(0); },
                scope:   this
            },
            {
                itemId: 'tb_mat_edit',
                disabled: true,
                iconCls:'icon-edit',
                tooltip: Config.Lang.edit,
                handler: function () { this.edit(this.getSelectionModel().getSelection()[0].getId()); },
                scope: this
            },
            {
                itemId: 'tb_mat_delete',
                disabled: true,
                iconCls:'icon-delete',
                tooltip: Config.Lang.delete,
                handler: function () { this.deleteMat(); },
                scope: this
            },
            '-',
            {
                itemId: 'tb_mat_pub',
                disabled: true,
                iconCls:'icon-pub',
                tooltip: Config.Lang.publish,
                handler: function() { this.call('pub'); },
                scope: this
            },
            {
                itemId: 'tb_mat_unpub',
                disabled: true,
                iconCls:'icon-unpub',
                tooltip: Config.Lang.unpublish,
                handler: function() { this.call('unpub'); },
                scope: this
            },
            '-',
            {
                itemId: 'tb_mat_move',
                disabled: true,
                iconCls:'icon-move',
                tooltip: Config.Lang.move,
                handler: function () { this.move('move'); },
                scope: this
            },
            {
                itemId: 'tb_mat_copy',
                disabled: true,
                iconCls:'icon-copy',
                tooltip: Config.Lang.copy,
                handler: function () { this.move('copy'); },
                scope: this
            }
        ]});
	},

	viewConfig: {
		getRowClass: function(record){
			return record.get("locked") ? "locked" : "";
		}
	},

	getStore: function() {
		return Ext.create('Ext.data.JsonStore', {

			autoDestroy: true,
			remoteSort: true,

			model: Cetera.model.Material,

			totalProperty: 'total',
			pageSize: Config.defaultPageSize,
			sorters: [{property: "dat", direction: "DESC"}],
			proxy: {
				type: 'ajax',
				url: 'include/data_materials.php',
				simpleSortMode: true,
				reader: {
					type: 'json',
					root: 'rows'
				},
				extraParams: {
					'id'   : 0,
					'type' : this.mat_type,
					limit  : Config.defaultPageSize
				}
			}

		});
	},

	getBBar: function() {
		return Ext.create('Ext.PagingToolbar', {
            store: this.store,
            items: [Config.Lang.filter + ': ', Ext.create('Cetera.field.Search', {
                store: this.store,
                paramName: 'query',
                width:200
            })]
        });
	},

	onSelectionChange: function(sm){
		var hs = sm.hasSelection();
		var sf = this.store.sorters.first().property;

		this.toolbar.getComponent('tb_mat_edit').setDisabled(!hs);
		this.toolbar.getComponent('tb_mat_delete').setDisabled(!hs);
		this.toolbar.getComponent('tb_mat_pub').setDisabled(!hs);
		this.toolbar.getComponent('tb_mat_unpub').setDisabled(!hs);
		this.toolbar.getComponent('tb_mat_move').setDisabled(!hs);
		this.toolbar.getComponent('tb_mat_copy').setDisabled(!hs);
	},

	onBeforeSelect: function(t , record, index, eOpts) {
		if (record.get('disabled')) return false;
	},

	onCellDblclick: function() {
		this.edit(this.getSelectionModel().getSelection()[0].getId());
	},

    initComponent : function() {

		this.store = this.getStore();
        this.bbar = this.getBBar();
        this.tbar = this.getToolbar();
		this.toolbar = this.tbar;

        this.callParent();

        this.getSelectionModel().on({
            'selectionchange' : this.onSelectionChange,
            'beforeselect' : this.onBeforeSelect,
            scope:this
        });

		if (this.mat_type) {
			this.on({
				'celldblclick' : this.onCellDblclick,
				scope: this
			});
			this.reload();
		}

    }

});<|endoftext|>


<commit_before><commit_msg>initial verifications module<commit_after>"use strict";

var http = require('http');
var querystring = require('querystring');

function setup_response_handler(req, callback) {
  if (typeof callback !== "function") {
    //console.log("missing callback");
    return;
  }
  req.on('response',
    function (res) {
      var response = '';
      res.setEncoding('utf8');
      res.on('data',
        function (chunk) {
          response += chunk;
        });
      res.on('end',
        function () {
          var err = null;
          try {
            response = JSON.parse(response);
            if (200 != res.statusCode) {
              err = new Error(response.error.message);
              err.name = response.error.type;
              err.code = response.error.code;
              err.param = response.error.param;
              response = null;
            }
          } catch (e) {
            err = new Error("Invalid JSON response");
            response = null;
          }
          callback(err, response);
        });
    });
  req.on('error', function (error) {
    callback(error);
  });
}

module.exports = function(api_key){
  var auth = 'Basic ' + new Buffer(api_key + ":").toString('base64');

  function serialize(obj, prefix) {
      var str = [];
      for(var p in obj) {
          var k = prefix ? prefix + "[" + p + "]" : p, v = obj[p];
          str.push(typeof v == "object" ?
              serialize(v, k) :
              encodeURIComponent(k) + "=" + encodeURIComponent(v));
      }
      return str.join("&");
  }

  function _request(method, path, data, callback){

    var request_data = serialize(data);

    var request_options = {
      host: 'blockscore-api.dev',
      port: '80',
      path: path,
      method: method,
      headers: {
        'Authorization': auth,
        'Accept': 'application/json',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': request_data.length
      }
    };

    var req = http.request(request_options);
    setup_response_handler(req, callback);
    req.write(request_data);
    req.end();
  }

  function post(path, data, callback) {
    _request('POST', path, data, callback);
  }

  function get(path, data, callback) {
      _request('GET', path, data, callback);
  }

  function del(path, data, callback) {
      _request('DELETE', path, data, callback);
  }

  function normalizeArguments() {
      var args = arguments[0];
      if(typeof args[0] == 'object' && typeof args[1] == 'function' && !args[2])
          return { count: args[0].count, offset: args[0].offset, cb: args[1] };
      else
          return { count: args[0], offset: args[1], cb: args[2] };
  }
  return {
    verifications: {
      create: function (data, cb) {
        post("/v1/verifications", data, cb);
      },
      retrieve: function (verification_id, cb) {
        if (!(verification_id && typeof verification_id === 'string')) {
          cb("verification_id required");
        }
        get("/v1/verifications/" + verification_id, {}, cb);
      },
      list: function (count, offset, cb) {
        var nArgs = normalizeArguments(arguments);
        get("/v1/verifications", {
          count: nArgs.count,
          offset: nArgs.offset
        }, nArgs.cb);
      }
    }
  };
}<|endoftext|>


<commit_before>/*
 * Copyright 2013 Telefonica Investigación y Desarrollo, S.A.U
 *
 * This file is part of iotagent-lwm2m-lib
 *
 * iotagent-lwm2m-lib is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * iotagent-lwm2m-lib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with iotagent-lwm2m-lib.
 * If not, seehttp://www.gnu.org/licenses/.
 *
 * For those usages not covered by the GNU Affero General Public License
 * please contact with::[contacto@tid.es]
 */

'use strict';

var libLwm2m2 = require('../..'),
    coap = require('coap'),
    Readable = require('stream').Readable,
    should = require('should');

describe('Client registration interface tests', function() {
    beforeEach(function (done) {
        libLwm2m2.start(null, done);
    });

    afterEach(function(done) {
        libLwm2m2.stop(done);
    });

    describe('When a client registration requests doesn\'t indicate a endpoint name arrives', function() {
        it('should fail with a 4.00 Bad Request');
    });
    describe('When a client registration requests doesn\'t indicate a endpoint name arrives', function () {
        it('should fail with a 4.00 Bad Request if the request doesn\'t indicate a lifetime');
    });
    describe('When a client registration requests doesn\'t indicate a endpoint name arrives', function () {
        it('should fail with a 4.00 Bad Request if the request doesn\'t indicate a binding');
    });
    describe('When a correct client registration requests arrives', function () {
        var requestUrl =  {
                host: 'localhost',
                port: 5683,
                method: 'POST',
                pathname: '/rd',
                query: 'ep=ROOM001&lt=86400&lwm2m=1.0&b=U'
            },
            payload = '</1>, </2>, </3>, </4>, </5>';

        it('should return a 2.01 Created code', function(done) {
            var req = coap.request(requestUrl),
                rs = new Readable();

            rs.push(payload);
            rs.push(null);
            rs.pipe(req);

            req.on('response', function(res) {
                res.code.should.equal('2.01');
                done();
            });
        });
    });
});
<commit_msg>ADD Device registration routing<commit_after>/*
 * Copyright 2013 Telefonica Investigación y Desarrollo, S.A.U
 *
 * This file is part of iotagent-lwm2m-lib
 *
 * iotagent-lwm2m-lib is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * iotagent-lwm2m-lib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with iotagent-lwm2m-lib.
 * If not, seehttp://www.gnu.org/licenses/.
 *
 * For those usages not covered by the GNU Affero General Public License
 * please contact with::[contacto@tid.es]
 */

'use strict';

var libLwm2m2 = require('../..'),
    coap = require('coap'),
    Readable = require('stream').Readable,
    should = require('should');

describe('Client registration interface tests', function() {
    beforeEach(function (done) {
        libLwm2m2.start(null, done);
    });

    afterEach(function(done) {
        libLwm2m2.stop(done);
    });

    describe('When a client registration requests doesn\'t indicate a endpoint name arrives', function() {
        var requestUrl =  {
                host: 'localhost',
                port: 5683,
                method: 'POST',
                pathname: '/rd',
                query: 'lt=86400&lwm2m=1.0&b=U'
            },
            payload = '</1>, </2>, </3>, </4>, </5>';

        it('should fail with a 4.00 Bad Request', function(done) {
            var req = coap.request(requestUrl),
                rs = new Readable();

            rs.push(payload);
            rs.push(null);
            rs.pipe(req);

            req.on('response', function(res) {
                res.code.should.equal('4.00');
                done();
            });
        });
    });
    describe('When a client registration requests doesn\'t indicate a endpoint name arrives', function () {
        it('should fail with a 4.00 Bad Request if the request doesn\'t indicate a lifetime');
    });
    describe('When a client registration requests doesn\'t indicate a endpoint name arrives', function () {
        it('should fail with a 4.00 Bad Request if the request doesn\'t indicate a binding');
    });
    describe('When a correct client registration requests arrives', function () {
        var requestUrl =  {
                host: 'localhost',
                port: 5683,
                method: 'POST',
                pathname: '/rd',
                query: 'ep=ROOM001&lt=86400&lwm2m=1.0&b=U'
            },
            payload = '</1>, </2>, </3>, </4>, </5>';

        it('should return a 2.01 Created code', function(done) {
            var req = coap.request(requestUrl),
                rs = new Readable();

            rs.push(payload);
            rs.push(null);
            rs.pipe(req);

            req.on('response', function(res) {
                res.code.should.equal('2.01');
                done();
            });
        });
    });
});
<|endoftext|>


<commit_before>const parse = require('github-url-from-git');
const { XmlDocument } = require('xmldoc');
const got = require('../../util/got');

module.exports = {
  getQueryUrl,
  getPkgReleases,
  getDefaultFeed,
};

// https://api.nuget.org/v3/index.json is a default official nuget feed
const defaultNugetFeed = 'https://api.nuget.org/v3/index.json';
const cacheNamespace = 'datasource-nuget';

function getDefaultFeed() {
  return defaultNugetFeed;
}

async function getQueryUrl(url) {
  // https://docs.microsoft.com/en-us/nuget/api/search-query-service-resource
  const resourceType = 'SearchQueryService';
  const cacheKey = `${url}:${resourceType}`;
  const cachedResult = await renovateCache.get(cacheNamespace, cacheKey);

  // istanbul ignore if
  if (cachedResult) {
    return cachedResult;
  }

  try {
    const servicesIndexRaw = await got(url, { json: true, platform: 'nuget' });
    if (servicesIndexRaw.statusCode !== 200) {
      logger.debug(
        { dependency: url, servicesIndexRaw },
        `nuget registry failure: status code != 200`
      );
      return null;
    }
    const searchQueryService = servicesIndexRaw.body.resources.find(
      resource => resource['@type'] === resourceType
    );
    const searchQueryServiceId = searchQueryService['@id'];

    const cacheMinutes = 60;
    await renovateCache.set(
      cacheNamespace,
      cacheKey,
      searchQueryServiceId,
      cacheMinutes
    );
    return searchQueryServiceId;
  } catch (e) {
    logger.debug(
      { e },
      `nuget registry failure: can't get SearchQueryService form ${url}`
    );
    return null;
  }
}

async function getPkgReleases(registryUrl, feedUrl, pkgName) {
  const queryUrl = `${feedUrl}?q=PackageId:${pkgName}`;
  const dep = {
    pkgName,
  };
  try {
    const pkgUrlListRaw = await got(queryUrl, {
      json: true,
      platform: 'nuget',
    });
    if (pkgUrlListRaw.statusCode !== 200) {
      logger.debug(
        { dependency: pkgName, pkgUrlListRaw },
        `nuget registry failure: status code != 200`
      );
      return null;
    }

    // There are no pkgName is current feed
    if (pkgUrlListRaw.body.totalHits === 0) {
      return null;
    }

    dep.releases = (pkgUrlListRaw.body.data[0].versions || []).map(item => ({
      version: item.version,
    }));

    try {
      // For nuget.org we have a way to get nuspec file
      if (registryUrl.toLowerCase() === defaultNugetFeed.toLowerCase()) {
        const nugetOrgApi = `https://api.nuget.org/v3-flatcontainer/${pkgName.toLowerCase()}/${
          [...dep.releases].pop().version
        }/${pkgName.toLowerCase()}.nuspec`;
        const result = await got(nugetOrgApi, { platform: 'nuget' });
        const nuspec = new XmlDocument(result.body);
        if (nuspec) {
          const sourceUrl = parse(
            nuspec.valueWithPath('metadata.repository@url')
          );
          if (sourceUrl) {
            dep.sourceUrl = sourceUrl;
          }
        }
      } else if (
        Object.prototype.hasOwnProperty.call(
          pkgUrlListRaw.body.data[0],
          'projectUrl'
        )
      ) {
        dep.sourceUrl = parse(pkgUrlListRaw.body.data[0].projectUrl);
      }
    } catch (err) /* istanbul ignore next */ {
      logger.debug(
        { err, pkgName, feedUrl },
        `nuget registry failure: can't parse pkg info for project url`
      );
    }

    return dep;
  } catch (err) {
    logger.debug(
      { err, pkgName, feedUrl },
      'nuget registry failure: Unknown error'
    );
    return null;
  }
}
<commit_msg>fix(nuget): opt in to semver 2.0.0 and prereleases<commit_after>const parse = require('github-url-from-git');
const { XmlDocument } = require('xmldoc');
const got = require('../../util/got');

module.exports = {
  getQueryUrl,
  getPkgReleases,
  getDefaultFeed,
};

// https://api.nuget.org/v3/index.json is a default official nuget feed
const defaultNugetFeed = 'https://api.nuget.org/v3/index.json';
const cacheNamespace = 'datasource-nuget';

function getDefaultFeed() {
  return defaultNugetFeed;
}

async function getQueryUrl(url) {
  // https://docs.microsoft.com/en-us/nuget/api/search-query-service-resource
  const resourceType = 'SearchQueryService';
  const cacheKey = `${url}:${resourceType}`;
  const cachedResult = await renovateCache.get(cacheNamespace, cacheKey);

  // istanbul ignore if
  if (cachedResult) {
    return cachedResult;
  }

  try {
    const servicesIndexRaw = await got(url, { json: true, platform: 'nuget' });
    if (servicesIndexRaw.statusCode !== 200) {
      logger.debug(
        { dependency: url, servicesIndexRaw },
        `nuget registry failure: status code != 200`
      );
      return null;
    }
    const searchQueryService = servicesIndexRaw.body.resources.find(
      resource => resource['@type'] === resourceType
    );
    const searchQueryServiceId = searchQueryService['@id'];

    const cacheMinutes = 60;
    await renovateCache.set(
      cacheNamespace,
      cacheKey,
      searchQueryServiceId,
      cacheMinutes
    );
    return searchQueryServiceId;
  } catch (e) {
    logger.debug(
      { e },
      `nuget registry failure: can't get SearchQueryService form ${url}`
    );
    return null;
  }
}

async function getPkgReleases(registryUrl, feedUrl, pkgName) {
  const queryUrl = `${feedUrl}?q=PackageId:${pkgName}&semVerLevel=2.0.0&prerelease=true`;
  const dep = {
    pkgName,
  };
  try {
    const pkgUrlListRaw = await got(queryUrl, {
      json: true,
      platform: 'nuget',
    });
    if (pkgUrlListRaw.statusCode !== 200) {
      logger.debug(
        { dependency: pkgName, pkgUrlListRaw },
        `nuget registry failure: status code != 200`
      );
      return null;
    }

    // There are no pkgName is current feed
    if (pkgUrlListRaw.body.totalHits === 0) {
      return null;
    }

    dep.releases = (pkgUrlListRaw.body.data[0].versions || []).map(item => ({
      version: item.version,
    }));

    try {
      // For nuget.org we have a way to get nuspec file
      if (registryUrl.toLowerCase() === defaultNugetFeed.toLowerCase()) {
        const nugetOrgApi = `https://api.nuget.org/v3-flatcontainer/${pkgName.toLowerCase()}/${
          [...dep.releases].pop().version
        }/${pkgName.toLowerCase()}.nuspec`;
        const result = await got(nugetOrgApi, { platform: 'nuget' });
        const nuspec = new XmlDocument(result.body);
        if (nuspec) {
          const sourceUrl = parse(
            nuspec.valueWithPath('metadata.repository@url')
          );
          if (sourceUrl) {
            dep.sourceUrl = sourceUrl;
          }
        }
      } else if (
        Object.prototype.hasOwnProperty.call(
          pkgUrlListRaw.body.data[0],
          'projectUrl'
        )
      ) {
        dep.sourceUrl = parse(pkgUrlListRaw.body.data[0].projectUrl);
      }
    } catch (err) /* istanbul ignore next */ {
      logger.debug(
        { err, pkgName, feedUrl },
        `nuget registry failure: can't parse pkg info for project url`
      );
    }

    return dep;
  } catch (err) {
    logger.debug(
      { err, pkgName, feedUrl },
      'nuget registry failure: Unknown error'
    );
    return null;
  }
}
<|endoftext|>


<commit_before>"use strict";

var https = require('https');
var fs = require('fs');

function setup_response_handler(req, callback) {
  if (typeof callback !== "function") {
    //console.log("missing callback");
    return;
  }
  req.on('response',
    function(res) {
      var response = '';
      res.setEncoding('utf8');
      res.on('data',
        function(chunk) {
          response += chunk;
        });
      res.on('end',
        function() {
          var err = null;
          try {
            response = JSON.parse(response);
            if (200 != res.statusCode && 201 != res.statusCode) {
              err = new Error(response.error.message);
              err.name = response.error.type;
              err.code = response.error.code;
              err.param = response.error.param;
              response = null;
            }
          } catch (e) {
            err = new Error("Invalid JSON response");
            response = null;
          }
          callback(err, response);
        });
    });
  req.on('error', function(error) {
    callback(error);
  });
}

module.exports = function(api_key) {
  var auth = 'Basic ' + new Buffer(api_key + ":").toString('base64');

  function _request(method, path, data, callback) {
    var request_data = JSON.stringify(data);

    var request_options = {
      host: 'api.blockscore.com',
      port: '443',
      path: path,
      method: method,
      headers: {
        'Authorization': auth,
        'Accept': 'application/vnd.blockscore+json;version=3',
        'User-Agent': 'blockscore-node/3.0.0 (https://github.com/BlockScore/blockscore-node)',
        'Content-Type': 'application/json',
        'Content-Length': request_data.length
      },
      agent: false
    };

    var req = https.request(request_options);
    setup_response_handler(req, callback);
    req.write(request_data);
    req.end();
  }

  function post(path, data, callback) {
    _request('POST', path, data, callback);
  }

  function get(path, data, callback) {
    _request('GET', path, data, callback);
  }

  function del(path, data, callback) {
    _request('DELETE', path, data, callback);
  }

  function normalizeArguments() {
    var args = arguments[0];
    if (typeof args[0] == 'object' && typeof args[1] == 'function' && !args[2])
      return {
        count: args[0].count,
        offset: args[0].offset,
        cb: args[1]
      };
    else
      return {
        count: args[0],
        offset: args[1],
        cb: args[2]
      };
  }

  return {
    verifications: {
      create: function(data, cb) {
        post("/verifications", data, cb);
      },
      retrieve: function(verification_id, cb) {
        if (!(verification_id && typeof verification_id === 'string')) {
          return cb(new Error("verification_id required"));
        }
        get("/verifications/" + encodeURIComponent(verification_id), {}, cb);
      },
      list: function(count, offset, cb) {
        var nArgs = normalizeArguments(arguments);
        get("/verifications", {
          count: nArgs.count,
          offset: nArgs.offset
        }, nArgs.cb);
      }
    },
    questions: {
      create: function(verification_id, cb) {
        if (!(verification_id && typeof verification_id === 'string')) {
          return cb(new Error("verification_id required"));
        }
        var data = { verification_id: verification_id };
        post("/questions", data, cb);
      },
      score: function(data, cb) {
        post("/questions/score", data, cb);
      },
      retrieve: function(question_set_id, cb) {
        if (!(question_set_id && typeof question_set_id === 'string')) {
          return cb(new Error("question_set_id required"));
        }
        get("/questions/" + encodeURIComponent(question_set_id), {}, cb);
      }
    }
  };
};
<commit_msg>implemented watchlist_candidates functionality for node wrapper; still need to write tests for & document it<commit_after>"use strict";

var https = require('https');
var fs = require('fs');

function setup_response_handler(req, callback) {
  if (typeof callback !== "function") {
    //console.log("missing callback");
    return;
  }
  req.on('response',
    function(res) {
      var response = '';
      res.setEncoding('utf8');
      res.on('data',
        function(chunk) {
          response += chunk;
        });
      res.on('end',
        function() {
          var err = null;
          try {
            response = JSON.parse(response);
            if (200 != res.statusCode && 201 != res.statusCode) {
              err = new Error(response.error.message);
              err.name = response.error.type;
              err.code = response.error.code;
              err.param = response.error.param;
              response = null;
            }
          } catch (e) {
            err = new Error("Invalid JSON response");
            response = null;
          }
          callback(err, response);
        });
    });
  req.on('error', function(error) {
    callback(error);
  });
}

module.exports = function(api_key) {
  var auth = 'Basic ' + new Buffer(api_key + ":").toString('base64');

  function _request(method, path, data, callback) {
    var request_data = JSON.stringify(data);

    var request_options = {
      host: 'api.blockscore.com',
      port: '443',
      path: path,
      method: method,
      headers: {
        'Authorization': auth,
        'Accept': 'application/vnd.blockscore+json;version=3',
        'User-Agent': 'blockscore-node/3.0.0 (https://github.com/BlockScore/blockscore-node)',
        'Content-Type': 'application/json',
        'Content-Length': request_data.length
      },
      agent: false
    };

    var req = https.request(request_options);
    setup_response_handler(req, callback);
    req.write(request_data);
    req.end();
  }

  function post(path, data, callback) {
    _request('POST', path, data, callback);
  }

  function get(path, data, callback) {
    _request('GET', path, data, callback);
  }

  function del(path, data, callback) {
    _request('DELETE', path, data, callback);
  }

  function normalizeArguments() {
    var args = arguments[0];
    if (typeof args[0] == 'object' && typeof args[1] == 'function' && !args[2])
      return {
        count: args[0].count,
        offset: args[0].offset,
        cb: args[1]
      };
    else
      return {
        count: args[0],
        offset: args[1],
        cb: args[2]
      };
  }

  return {
    verifications: {
      create: function(data, cb) {
        post("/verifications", data, cb);
      },
      retrieve: function(verification_id, cb) {
        if (!(verification_id && typeof verification_id === 'string')) {
          return cb(new Error("verification_id required"));
        }
        get("/verifications/" + encodeURIComponent(verification_id), {}, cb);
      },
      list: function(count, offset, cb) {
        var nArgs = normalizeArguments(arguments);
        get("/verifications", {
          count: nArgs.count,
          offset: nArgs.offset
        }, nArgs.cb);
      }
    },
    questions: {
      create: function(verification_id, cb) {
        if (!(verification_id && typeof verification_id === 'string')) {
          return cb(new Error("verification_id required"));
        }
        var data = { verification_id: verification_id };
        post("/questions", data, cb);
      },
      score: function(data, cb) {
        post("/questions/score", data, cb);
      },
      retrieve: function(question_set_id, cb) {
        if (!(question_set_id && typeof question_set_id === 'string')) {
          return cb(new Error("question_set_id required"));
        }
        get("/questions/" + encodeURIComponent(question_set_id), {}, cb);
      }
    },
    watchlist_candidates: {
      create: function(data, cb) {
        post("/watchlist_candidates", data, cb);
      },
      retrieve: function(watchlist_candidate_id, cb) {
        if (!(watchlist_candidate_id && typeof watchlist_candidate_id === 'string')) {
          return cb(new Error("watchlist_candidate_id required"));
        }
        get("/watchlist_candidates/" + encodeURIComponent(watchlist_candidate_id), {}, cb);
      },
      list: function(count, offset, cb) {
        var nArgs = normalizeArguments(arguments);
        get("/watchlist_candidates/", {
          count: nArgs.count,
          offset: nArgs.offset
        }, nArgs.cb);
      }
    }

  };
};
<|endoftext|>


<commit_before>/*
 * Licensed to Cloudkick, Inc ('Cloudkick') under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * Cloudkick licenses this file to You 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 sys = require('sys');
var path = require('path');

var sprintf = require('extern/sprintf').sprintf;

var config = require('util/config');
var misc = require('util/misc');

var norris = require('norris');
var runit_templates = require('runit/templates/base');
var RunitServiceDirectory = require('runit/services').RunitServiceDirectory;

function get_service_dir() {
  return new RunitServiceDirectory(config.get().servicedir);
}

var create_service = function(instance_name, instance_path, application_type, callback) {
  var runit = get_service_dir();

  runit_templates.get_template(instance_name, instance_path, application_type, function(error, template) {
    if (error) {
      return callback(error);
    }

    runit.create_service_layout(instance_name, template, function(error) {
      if (error) {
        return callback(error);
      }

      callback();
    });
  });
};

exports.create_service = create_service;
<commit_msg>Fix function name<commit_after>/*
 * Licensed to Cloudkick, Inc ('Cloudkick') under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * Cloudkick licenses this file to You 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 sys = require('sys');
var path = require('path');

var sprintf = require('extern/sprintf').sprintf;

var config = require('util/config');
var misc = require('util/misc');

var norris = require('norris');
var runit_templates = require('runit/templates/base');
var RunitServiceDirectory = require('runit/services').RunitServiceDirectory;

function get_service_dir() {
  return new RunitServiceDirectory(config.get().servicedir);
}

var create_service = function(instance_name, instance_path, application_type, callback) {
  var runit = get_service_dir();

  runit_templates.get_application_template(instance_name, instance_path, application_type, function(error, template) {
    if (error) {
      return callback(error);
    }

    runit.create_service_layout(instance_name, template, function(error) {
      if (error) {
        return callback(error);
      }

      callback();
    });
  });
};

exports.create_service = create_service;
<|endoftext|>


<commit_before>/**
 * Created by daxlab on 19/3/16.
 */

var express = require('express');
var request = require('request');
var app = express();
var fs = require('fs');
var cheerio = require('cheerio');
var debug = require('debug')('api');
var bodyParser = require('body-parser');

app.use(bodyParser.json());

app.get('/', function(req, res) {
    res.send('A play store scrapper written by daxlab');
});

var PLAY_STORE_URL = 'https://play.google.com/store/apps/details?id=';

app.get('/api', function(req, res) {
    var scrapeUrl = PLAY_STORE_URL;
    debug('Incoming request ',req.body);
    scrapeUrl = scrapeUrl + req.query.package;
    request(scrapeUrl, function(err, response, html) {
        if(err) {
            debug('Error in request ', scrapeUrl);
        }
        else {
            var page = cheerio.load(html);
            var appData = {
                name: '',
                category: '',
                packageName: '',
                downloads: '',
                description: '',
                overallRating: '',
                overallRatingCount: '',
                rating5: '',
                rating4: '',
                rating3: '',
                rating2: '',
                rating1: ''
            };

            page('.document-title').filter(function() {
                var data = page(this);
                appData.name = data.children().first().text();
            });
            page('.document-subtitle.category').filter(function() {
                var data = page(this);
                appData.category = data.children().first().text();
            });
            page('.score').filter(function() {
                var data = page(this);
                appData.overallRating = data.text();
            });
            page('.reviews-stats').filter(function() {
                var data = page(this);
                appData.overallRatingCount = data.children().last().text();
                appData.overallRatingCount = appData.overallRatingCount.replace(/,/g,'');
            });
            page('.rating-bar-container.five').filter(function() {
                var data = page(this);
                appData.rating5 = data.children().last().text();
                appData.rating5 = appData.rating5.replace(/,/g,'');
            });
            page('.rating-bar-container.four').filter(function() {
                var data = page(this);
                appData.rating4 = data.children().last().text();
                appData.rating4 = appData.rating4.replace(/,/g,'');
            });
            page('.rating-bar-container.three').filter(function() {
                var data = page(this);
                appData.rating3 = data.children().last().text();
                appData.rating3 = appData.rating3.replace(/,/g,'');
            });
            page('.rating-bar-container.two').filter(function() {
                var data = page(this);
                appData.rating2 = data.children().last().text();
                appData.rating2 = appData.rating2.replace(/,/g,'');
            });
            page('.rating-bar-container.one').filter(function() {
                var data = page(this);
                appData.rating1 = data.children().last().text();
                appData.rating1 = appData.rating1.replace(/,/g,'');
            });
            page('.details-wrapper.apps').filter(function() {
                var data = page(this);
                appData.packageName = data.data('docid');
            });
            page('.show-more-content.text-body').filter(function() {
                var data = page(this);
                appData.description = data.children().text();
            });
            page('.details-section.metadata').filter(function() {
                var data = page(this);
                appData.downloads = data.find("[itemprop=numDownloads]").text();
                appData.downloads = appData.downloads.replace(/,/g, '').trim();
            });
            debug(appData);
            res.send(appData);

        }
    });
});

var port = process.env.PORT || 8000;
app.listen(port);<commit_msg>add post apps router<commit_after>/**
 * Created by daxlab on 19/3/16.
 */

'use strict';
var express = require('express');
var request = require('request');
var app = express();
var fs = require('fs');
var cheerio = require('cheerio');
var debug = require('debug')('api');
var bodyParser = require('body-parser');

app.use(bodyParser.json());

app.get('/', function(req, res) {
    res.send('A play store scrapper written by daxlab');
});

var PLAY_STORE_URL = 'https://play.google.com/store/apps/details?id=';

app.get('/api', function(req, res) {
    var scrapeUrl = PLAY_STORE_URL;
    debug('Incoming request ',req.body);
    scrapeUrl = scrapeUrl + req.query.package;
    request(scrapeUrl, function(err, response, html) {
        if(err) {
            debug('Error in request ', scrapeUrl);
        }
        else {
            var page = cheerio.load(html);
            var appData = {
                name: '',
                category: '',
                packageName: '',
                downloads: '',
                description: '',
                overallRating: '',
                overallRatingCount: '',
                rating5: '',
                rating4: '',
                rating3: '',
                rating2: '',
                rating1: ''
            };

            page('.document-title').filter(function() {
                var data = page(this);
                appData.name = data.children().first().text();
            });
            page('.document-subtitle.category').filter(function() {
                var data = page(this);
                appData.category = data.children().first().text();
            });
            page('.score').filter(function() {
                var data = page(this);
                appData.overallRating = data.text();
            });
            page('.reviews-stats').filter(function() {
                var data = page(this);
                appData.overallRatingCount = data.children().last().text();
                appData.overallRatingCount = appData.overallRatingCount.replace(/,/g,'');
            });
            page('.rating-bar-container.five').filter(function() {
                var data = page(this);
                appData.rating5 = data.children().last().text();
                appData.rating5 = appData.rating5.replace(/,/g,'');
            });
            page('.rating-bar-container.four').filter(function() {
                var data = page(this);
                appData.rating4 = data.children().last().text();
                appData.rating4 = appData.rating4.replace(/,/g,'');
            });
            page('.rating-bar-container.three').filter(function() {
                var data = page(this);
                appData.rating3 = data.children().last().text();
                appData.rating3 = appData.rating3.replace(/,/g,'');
            });
            page('.rating-bar-container.two').filter(function() {
                var data = page(this);
                appData.rating2 = data.children().last().text();
                appData.rating2 = appData.rating2.replace(/,/g,'');
            });
            page('.rating-bar-container.one').filter(function() {
                var data = page(this);
                appData.rating1 = data.children().last().text();
                appData.rating1 = appData.rating1.replace(/,/g,'');
            });
            page('.details-wrapper.apps').filter(function() {
                var data = page(this);
                appData.packageName = data.data('docid');
            });
            page('.show-more-content.text-body').filter(function() {
                var data = page(this);
                appData.description = data.children().text();
            });
            page('.details-section.metadata').filter(function() {
                var data = page(this);
                appData.downloads = data.find("[itemprop=numDownloads]").text();
                appData.downloads = appData.downloads.replace(/,/g, '').trim();
            });
            debug(appData);
            res.send(appData);

        }
    });
});

app.post('/postapps', function(req, res) {
    debug('Incoming apps json ', req.body);
    var msg = {
        status: 'ok'
    };
    // res.sendStatus(200);
    res.send(msg);
});

var port = process.env.PORT || 3000;
app.listen(port);<|endoftext|>


<commit_before>/**
 * @author Benjamin Hollis
 * @author Quoc-Viet Nguyen
 */
const { Cc, Ci, components } = require("chrome");
const xpcom = require("sdk/platform/xpcom");
const { JSONView } = require("./jsonview");
const prefs = require("./prefs");

// This component is an implementation of nsIStreamConverter that converts
// application/json to html
const JSON_TYPE = "application/json";
const CONTRACT_ID = "@mozilla.org/streamconv;1?from=" + JSON_TYPE + "&to=*/*";
const CLASS_ID = "{64890660-53c4-11dd-ae16-0800200c9a66}";
const GECKO_VIEWER = "Gecko-Content-Viewers";

// Create and register the service
var service = xpcom.Service({
  id: components.ID(CLASS_ID),
  contract: CONTRACT_ID,
  Component: JSONView,
  register: false,
  unregister: false
});

// Hack to remove FF8+'s built-in JSON-as-text viewer
var categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
var geckoViewer = categoryManager.getCategoryEntry(GECKO_VIEWER, JSON_TYPE);

function onLoad(options, callbacks) {
  console.debug(options.loadReason);
  if (!xpcom.isRegistered(service)) {
    xpcom.register(service);
  }
  // Remove built-in JSON viewer
  categoryManager.deleteCategoryEntry(GECKO_VIEWER, JSON_TYPE, false);
  prefs.register();
};

function onUnload(reason) {
  if (xpcom.isRegistered(service)) {
    xpcom.unregister(service);
  }
  // Re-add built-in JSON viewer
  categoryManager.addCategoryEntry(GECKO_VIEWER, JSON_TYPE, geckoViewer, false, false);
  prefs.unregister();
};

exports.main = onLoad;
exports.onUnload = onUnload;
<commit_msg>Fix opening .json files<commit_after>/**
 * @author Benjamin Hollis
 * @author Quoc-Viet Nguyen
 */
const { Cc, Ci, components } = require("chrome");
const xpcom = require("sdk/platform/xpcom");
const { JSONView } = require("./jsonview");
const prefs = require("./prefs");

// This component is an implementation of nsIStreamConverter that converts
// application/json to html
const JSON_TYPE = "application/json";
const CONTRACT_ID = "@mozilla.org/streamconv;1?from=" + JSON_TYPE + "&to=*/*";
const CLASS_ID = "{64890660-53c4-11dd-ae16-0800200c9a66}";
const GECKO_VIEWER = "Gecko-Content-Viewers";

// Create and register the service
var service = xpcom.Service({
  id: components.ID(CLASS_ID),
  contract: CONTRACT_ID,
  Component: JSONView,
  register: false,
  unregister: false
});

// Hack to remove FF8+'s built-in JSON-as-text viewer
var categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
var geckoViewer = categoryManager.getCategoryEntry(GECKO_VIEWER, JSON_TYPE);

function onLoad(options, callbacks) {
  console.debug(options.loadReason);
  if (!xpcom.isRegistered(service)) {
    xpcom.register(service);
  }
  // Remove built-in JSON viewer
  categoryManager.deleteCategoryEntry(GECKO_VIEWER, JSON_TYPE, false);

  // Tell Firefox that .json files are application/json
  categoryManager.addCategoryEntry('ext-to-type-mapping', 'json', 'application/json', false, true);
  prefs.register();
};

function onUnload(reason) {
  if (xpcom.isRegistered(service)) {
    xpcom.unregister(service);
  }
  // Re-add built-in JSON viewer
  categoryManager.addCategoryEntry(GECKO_VIEWER, JSON_TYPE, geckoViewer, false, false);
  prefs.unregister();
};

exports.main = onLoad;
exports.onUnload = onUnload;
<|endoftext|>