close

  
<p> var MF  = acre.require("MANIFEST");
var MQL = acre.require(MF.libBase+"MQL");

var NS = MF.NS;
var ITEM_TYPE = MF.NS + '/scheme/item';

function getItemById(id,qExt)
{
  
  var e = MQL.read({"type": ITEM_TYPE ,id:MF.TOPIC+'/'+id,"*": null
                    ,item_type:{id:null,key:null}
                    ,city_sector:{id:null,key:null}

,
  "resource": [{
    "kind": {
      "id": null, key:{value:null}
    },
    "url": null,
    "icon": null,
    "optional": "optional"
  }]
                    ,"z:state":"normal"
                    ,"limit":1}
                   ,qExt);
  if (!e){
    e = {item_id:id,name:"#"+id,state:'not_found'};
  }
  e.images = Array.scan(e.resource,[],function(i,v){if (v.kind.key.value=="picture")this.push(v)},[null,null]);
  //Array.scan(e.cost,[],function(i,v){v.kind=v.kind.key;},[null,null]);
  
  console.log('e='+id,e);
  return e ;
}


// writes result object in common way
function writeResultObject(obj)
{
  var json = JSON.stringify(obj,null,5);
  var callback= acre.request.params["callback"];
  acre.write(callback?(callback+'('+json+')'):json);
}

  
function getLocationUrl(lat, lng)
{
  return "http://maps.google.com/maps?f=q&amp;hl=en&amp;ie=UTF8&amp;z=14&amp;spn=0.052278,0.070982&amp;output=embed"
            +"&amp;q="+lat+","+lng
            +"&amp;ll="+lat+","+lng
            +"&amp;sll="+lat+","+lng
    
 }

  
function getLastTimestamp(lag1)
{
    var lag = acre.keystore.get('timestamp.com')[0];
    var q = MQL.read({"id": NS,"lastTimestamp": null,type:MF.NS + '/scheme/app'});
    var ts = q &amp;&amp; q["lastTimestamp"];
    var d1 = ts ? Date.shifted(Date.parse(ts,"yyyy-MM-ddTHH:mm:ss"),lag) : (new Date());
    var p1 = d1.format("yyyy_MM_dd_HH_mm");
    console.log('getLastTimestamp:',p1, ts);
    return p1;
}

function fetchFacebookApplicationAccessToken()
{
  //MQL.checkAuthorized();
    var cred = acre.keystore.get('graph.facebook.com');
    var access_token = acre.urlfetch('https://graph.facebook.com/oauth/access_token?type=client_cred'
      +'&amp;client_id='+cred[0]
      +'&amp;client_secret='+cred[1]);
    access_token = String.after(access_token.body,'access_token=')
  
    console.log('access_token=',access_token);
    return access_token;
}
    
     
function postToFacebook(e, access_token)
{
    var cred = acre.keystore.get('ga.page.fb.com');
    var result = acre.urlfetch('https://graph.facebook.com/feed?access_token=' +encodeURIComponent(cred[1])
          
          ,{method:"POST",content:'message='+encodeURIComponent(e.title) 
          +'&amp;id='+encodeURIComponent('129253963800913')
          +'&amp;link='+encodeURIComponent(e.link)
          +'&amp;picture='+encodeURIComponent(e.image)
          +'&amp;description='+encodeURIComponent(e.description||'')
          +'&amp;caption='+encodeURIComponent(e.caption||'')});
  
    console.log('result=',result.body);
    var body = JSON.parse(result.body);
    return body &amp;&amp; body.id;
 }
    
function postToMongoLab(e)
{
    var cred = acre.keystore.get('ga.mongolab.com');
    var result = acre.urlfetch(
      'https://api.mongolab.com/api/1/databases/ga/collections/for_rent?apiKey=' +encodeURIComponent(cred[1])
      ,{method:"POST",content:JSON.stringify(e), headers:{'Content-Type':'application/json;charset=utf-8'}}
      );
  
    console.log(e, 'result=',result.body);
    var body = JSON.parse(result.body);
    return body &amp;&amp; body.id;
 }
    

if (acre.request.script.id==acre.current_script.id)
{
  writeResultObject(this);
} </p>

Comments

Hide