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 item = {
    id:e.item_id,
    num:e.item_num,
    name:e.title,
    name_ru:e.title_ru,
    icon:e.icon,
    description:e.description,
    description_ru:e.description_ru,
    descriptions:e.descriptions,
    bedrooms:e.bedroom_count,
    sleeps:e.capacityMax,
    bathrooms:e.bathroom_count,
    type:e.item_type,
    arr:e.area,
    district:e.area.id,
    location_rate:e.address_rank,
    nearby:e.area.title,
    glamourity:e.rank,
    reviews:e.reviews,
    images:e.images,
    improvements: e.improvements,
    price:e.price,
    min_period:{
        measure:"week", 
        amount:1
    }
    ,
    geolocation:{
        lat:e.latitude
        ,
        lng:e.longitude
    },
    suitability:[]
}
      if (e.isSmokingAllowed!='NO') {
          item.suitability.push('isSmokingAllowed') 
      }
      if (e.isPetFriendly!='NO') {
          item.suitability.push('isPetFriendly') 
      }
      if (e.hasWheelChairAccess!='NO') {
          item.suitability.push('hasWheelChairAccess') 
      }
    item.rating = 8;
    item.price.costs= e.costs
    item.price.detailed= [
            {
                "min_period": 2,
                "amount_per_night": 533,
                "amount_per_week": 5200,
                "amount_per_month": 16000,
                "capacity": "3-5"
            }
        ]
    item.price.additional_fees= {
            "text": "The monthly rate is for lettings of at least two months. Please contact us for shorter periods.<br>In all cases we have a 4 day minimum stay expect over the Christmas holidays where we have a one week minimum.",
            "list": [
                {
                    "id": "cleaning",
                    "amount": 50,
                  "required":true
                }
            ]
        }
  
    var result = acre.urlfetch(
      'https://api.mongolab.com/api/1/databases/ga/collections/for_rent?q='+encodeURIComponent('{id:"'+item.id+'"}')+'&amp;u=true&amp;apiKey=' +encodeURIComponent(cred[1])
      ,{method:"PUT",content:JSON.stringify(item), headers:{'Content-Type':'application/json;charset=utf-8'}}
      );
  
    console.log(e,item, 'result=',result.body);
    var body = JSON.parse(result.body);
    return body &amp;&amp; body._id &amp;&amp; body._id.$oid;
 }
    

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

Comments

Hide