network API structure

Following listings presents how network features are added to robot API. First one presents how robot API downloads weather and news from the Internet utilizing UBrowser module.

//example shows how to user UBrowser module for getting weather forecast from the Internet
function NetworkGetWeather(){
try{
var ret = robot.network.browser.Go("http://weather.yahooapis.com/forecastrss?w="+_Weather_location+"&u="+_Weather_scale);//we go to yahoo weather forecast rss 
if (!ret) return false;
//utilizing function GetAtributesByPath we read consecutive weather parameters
robot.network.weather.location.city = robot.network.browser.GetAtributesByPath("/rss/channel/yweather:location")[0][1];
robot.network.weather.location.region = robot.network.browser.GetAtributesByPath("/rss/channel/yweather:location")[1][1];
robot.network.weather.location.country = robot.network.browser.GetAtributesByPath("/rss/channel/yweather:location")[2][1];
robot.network.weather.units.temperature = robot.network.browser.GetAtributesByPath("/rss/channel/yweather:units")[0][1];
robot.network.weather.units.distance = robot.network.browser.GetAtributesByPath("/rss/channel/yweather:units")[1][1];
robot.network.weather.units.pressure = robot.network.browser.GetAtributesByPath("/rss/channel/yweather:units")[2][1];
robot.network.weather.units.speed = robot.network.browser.GetAtributesByPath("/rss/channel/yweather:units")[3][1];
robot.network.weather.wind.chill = robot.network.browser.GetAtributesByPath("/rss/channel/yweather:wind")[0][1].asFloat;
robot.network.weather.wind.direction = robot.network.browser.GetAtributesByPath("/rss/channel/yweather:wind")[1][1].asFloat;
robot.network.weather.wind.speed = robot.network.browser.GetAtributesByPath("/rss/channel/yweather:wind")[2][1].asFloat;
//reading the rest of parameters
return true;
}catch(var e){
echo("ERROR: %s" % e.message);
return false;
};
}|{};
//now we can simply call robot.network.weather.Get() to get new weather forecast
do (robot.network.weather) { 
function Get {call.NetworkGetWeather();};
};
//in similar matter we read news from rss channels:
function NetworkGetNews(address){
try{
var ret = robot.network.browser.Go(address); //we are going to website with news (usually rss channel)
if (!ret) return false;
var tmp_count = robot.network.browser.FindPosition("item",["-1"]).size; //number of found news
robot.network.news.count = tmp_count;
//empty tables to which we will read found news
robot.network.news.date = [];
robot.network.news.title = [];
robot.network.news.description = [];
robot.network.news.category = [];
 
//for every news use function robot.network.browser.GetTextByPath to read new date, title, description, etc.
for (var i=0;i<tmp_count;i++){
if (robot.network.browser.GetTextByPath("/rss/channel/item["+i+"]/description").size>0){
robot.network.news.date << robot.network.browser.GetTextByPath("/rss/channel/item["+i+"]/pubDate");
robot.network.news.title << robot.network.browser.GetTextByPath("/rss/channel/item["+i+"]/title");
 
if (_En_UTextToolNetwork)//if we enabled UTextTool we can clean the news description from HTML tags
robot.network.news.description << TextTool2.RemoveHTML(robot.network.browser.GetTextByPath("/rss/channel/item["+i+"]/description"))
else
robot.network.news.description << robot.network.browser.GetTextByPath("/rss/channel/item["+i+"]/description");
 
robot.network.news.category << robot.network.browser.GetTextByPath("/rss/channel/item["+i+"]/category");
};
};
return true;
}catch(var e){
echo("ERROR: %s" % e.message);
return false;
};
}|{}; 
//now we can simply call robot.network.news.Get() to get news from desired website
do (robot.network.news) { 
function Get(address) {call.NetworkGetNews(address);};
};
 

The following listing presents how sending and receiving e-mails where added to robot structure.

//API gives interface to two mailboxes - one owned by the robot, second by the user
//for this purpose two mail objects were created (lets assume we choose UMail_CURL)
//object for robot mailbox
var Global.Mail1=UMail.new(_Mail_SmtpAddr+":"+_Mail_SmtpPort,_Mail_ImapAddr+":"+_Mail_ImapPort,NGetLoginPassword(2),NGetLoginPassword(3));
//new components for robot.network.mail["robot"] structure
robot.network.mail["robot"].addComponent("send");
robot.network.mail["robot"].addComponent("receive");
//functions for robot mailbox
do(robot.network.mail["robot"])
{
function Send()     { call.Mail1.Send(); };
function Check()     { call.Mail1.Check(); };
function LoadContent(file)  { call.NLoadText(file);};
};
//object for user mailbox
var Global.Mail2=UMail.new(_Mail_SmtpAddr+":"+_Mail_SmtpPort,_Mail_ImapAddr+":"+_Mail_ImapPort,NGetLoginPassword(7),NGetLoginPassword(8));
//new components for robot.network.mail["user"] structure
robot.network.mail["user"].addComponent("send");
robot.network.mail["user"].addComponent("receive");
//functions for user mailbox
do(robot.network.mail["user"])
{
function Send()     { call.Mail2.Send(); };
function Check()     { call.Mail2.Check(); };
function LoadContent(file)  { call.NLoadText(file);};
};
//Both structures where extended with functions of receiving and sending e-mails, e.g. which were calling functions from corresponding objects (Mail1 for "robot", Mail2 for "user")
do(robot.network.mail["robot"].send)
{
function From(value)   { call.Mail1.sendFrom(value); };
//other sending functions
}
 

The following listing presents how Facebook features were added to robot structure

//like in case of e-mail, Facebook uses two instances of UFacebook module - one for robot Facebook account, second one for user account
//we create new structure component
robot.network.facebook["robot"].addComponent("user");
//instead of adding whole object into the structure we add its slots and changing their names
Facebook1.getSlot("userID").copy(robot.network.facebook["robot"].user,"ID");
Facebook1.getSlot("userName").copy(robot.network.facebook["robot"].user,"name");
Facebook1.getSlot("userFirstName").copy(robot.network.facebook["robot"].user,"firstName");
//.. other slots
//we do the same with the rest of Facebook API components: post, status, album, ...
//and the same for user Facebook account
 

The following listing presents how Google calendar and contacts were added to robot structure.

//adding calendar and events to robot.network.gcalendar["user"] structure
robot.network.gcalendar["user"].addComponent("calendar");  
robot.network.gcalendar["user"].addComponent("event");  
 
//module attributes are copied to robot structure with different names  
GCalendar1.getSlot("calendarID").copy(robot.network.gcalendar["user"].calendar,"ID");
GCalendar1.getSlot("calendarSummary").copy(robot.network.gcalendar["user"].calendar,"summary");
GCalendar1.getSlot("calendarDescription").copy(robot.network.gcalendar["user"].calendar,"description");
//other attributes
//functions of the structure call functions from GCalendar1 object
do(robot.network.gcalendar["user"].calendar)
{
function Get()           { call.GCalendar1.GetCalendars() ; };
};

 

 

EMYS and FLASH are Open Source and distributed according to the GPL v2.0 © Rev. 0.8.0, 27.04.2016

FLASH Documentation