19
novembre
2012
Lecture de flux rss en Vala
novembre
2012
Un article de Rachel
Pas de commentaires
Voici une bibliothèque écrite en Vala permettant de lire les flux rss :
Elle dépend de
libxml-2.0 libsoup-2.4
using Xml;
using Soup;
namespace Media
{
namespace Rss
{
public class Image
{
string image_url;
string image_title;
string image_link;
public Image(Xml.Node* node)
{
for (Xml.Node* iter = node->children; iter != null; iter = iter->next){
switch(iter->name)
{
case "url":
image_url = iter->get_content();
break;
case "title":
image_title = iter->get_content();
break;
case "link":
image_link = iter->get_content();
break;
}
}
}
public string url {get{return image_url;}}
public string link {get{return image_link;}}
public string title {get{return image_title;}}
}
public class Flux
{
Xml.Doc* doc;
Xml.Node* channel;
List node_list;
string channel_title;
string channel_link;
string channel_desc;
string channel_language;
List channel_images;
public Flux.from_web(string web_uri)
{
Session session = new SessionAsync();
Message message = new Message("GET",web_uri);
session.send_message(message);
uint8[] data = message.response_body.data;
FileUtils.set_data("temp",data);
this("temp");
}
public Flux(string uri)
{
Parser.init();
node_list = new List();
channel_images = new List();
doc = Parser.parse_file(uri);
Xml.Node* node = doc->get_root_element();
channel = node->first_element_child();
for (Xml.Node* iter = channel->children; iter != null; iter = iter->next){
switch(iter->name)
{
case "item":
node_list.append(new Item(iter));
break;
case "language":
channel_language = iter->get_content();
break;
case "link":
channel_link = iter->get_content();
break;
case "title":
channel_title = iter->get_content();
break;
case "description":
channel_desc = iter->get_content();
break;
case "image":
channel_images.append(new Image(iter)) ;
break;
}
}
}
public string language {get{return channel_language;}}
public string title {get{return channel_title;}}
public string link {get{return channel_link;}}
public string description {get{return channel_desc;}}
public List images {get{return channel_images;}}
public List items
{
get{
return this.node_list;
}
}
}
public class Item
{
Xml.Node* xml_node;
string node_content;
Enclosure item_enclosure;
string item_title;
string item_guid;
string item_link;
Time pub_date;
Time parse_date (string date)
{
string[] tab = date.split(" ",0);
string[] months = new string[]{
"Jan","Feb",
"Mar","Apr",
"May","Jun",
"Jul","Aug",
"Sep","Oct",
"Nov","Dec"};
Time t = Time();
t.day = int.parse(tab[1]);
t.year = int.parse(tab[3])-1900;
for(int i = 0;iget_content();
for (Xml.Node* iter = node->children; iter != null; iter = iter->next){
switch(iter->name)
{
case "enclosure":
item_enclosure = new Enclosure(iter);
break;
case "title":
item_title = iter->get_content();
break;
case "pubDate":
pub_date = parse_date(iter->get_content());
break;
case "guid":
item_guid = iter->get_content();
break;
case "link":
item_link = iter->get_content();
break;
}
}
}
public string link {get{return item_link;}}
public string guid {get{return item_guid;}}
public string title {get{return item_title;}}
public string content {get{return node_content;}}
public Enclosure enclosure {get{return item_enclosure;}}
public Time pubDate {get{return pub_date;}}
}
public class Enclosure
{
string enclosure_url;
uint64 enclosure_length;
string enclosure_type;
public Enclosure(Xml.Node* node){
enclosure_url = node->get_prop("url");
enclosure_length = uint64.parse(node->get_prop("length"));
enclosure_type = node->get_prop("type");
}
public string url {get{return enclosure_url;}}
public uint64 length {get{return enclosure_length;}}
public string mime_type {get{return enclosure_type;}}
}
}
}
using Soup;
namespace Media
{
namespace Rss
{
public class Image
{
string image_url;
string image_title;
string image_link;
public Image(Xml.Node* node)
{
for (Xml.Node* iter = node->children; iter != null; iter = iter->next){
switch(iter->name)
{
case "url":
image_url = iter->get_content();
break;
case "title":
image_title = iter->get_content();
break;
case "link":
image_link = iter->get_content();
break;
}
}
}
public string url {get{return image_url;}}
public string link {get{return image_link;}}
public string title {get{return image_title;}}
}
public class Flux
{
Xml.Doc* doc;
Xml.Node* channel;
List node_list;
string channel_title;
string channel_link;
string channel_desc;
string channel_language;
List channel_images;
public Flux.from_web(string web_uri)
{
Session session = new SessionAsync();
Message message = new Message("GET",web_uri);
session.send_message(message);
uint8[] data = message.response_body.data;
FileUtils.set_data("temp",data);
this("temp");
}
public Flux(string uri)
{
Parser.init();
node_list = new List();
channel_images = new List();
doc = Parser.parse_file(uri);
Xml.Node* node = doc->get_root_element();
channel = node->first_element_child();
for (Xml.Node* iter = channel->children; iter != null; iter = iter->next){
switch(iter->name)
{
case "item":
node_list.append(new Item(iter));
break;
case "language":
channel_language = iter->get_content();
break;
case "link":
channel_link = iter->get_content();
break;
case "title":
channel_title = iter->get_content();
break;
case "description":
channel_desc = iter->get_content();
break;
case "image":
channel_images.append(new Image(iter)) ;
break;
}
}
}
public string language {get{return channel_language;}}
public string title {get{return channel_title;}}
public string link {get{return channel_link;}}
public string description {get{return channel_desc;}}
public List images {get{return channel_images;}}
public List items
{
get{
return this.node_list;
}
}
}
public class Item
{
Xml.Node* xml_node;
string node_content;
Enclosure item_enclosure;
string item_title;
string item_guid;
string item_link;
Time pub_date;
Time parse_date (string date)
{
string[] tab = date.split(" ",0);
string[] months = new string[]{
"Jan","Feb",
"Mar","Apr",
"May","Jun",
"Jul","Aug",
"Sep","Oct",
"Nov","Dec"};
Time t = Time();
t.day = int.parse(tab[1]);
t.year = int.parse(tab[3])-1900;
for(int i = 0;iget_content();
for (Xml.Node* iter = node->children; iter != null; iter = iter->next){
switch(iter->name)
{
case "enclosure":
item_enclosure = new Enclosure(iter);
break;
case "title":
item_title = iter->get_content();
break;
case "pubDate":
pub_date = parse_date(iter->get_content());
break;
case "guid":
item_guid = iter->get_content();
break;
case "link":
item_link = iter->get_content();
break;
}
}
}
public string link {get{return item_link;}}
public string guid {get{return item_guid;}}
public string title {get{return item_title;}}
public string content {get{return node_content;}}
public Enclosure enclosure {get{return item_enclosure;}}
public Time pubDate {get{return pub_date;}}
}
public class Enclosure
{
string enclosure_url;
uint64 enclosure_length;
string enclosure_type;
public Enclosure(Xml.Node* node){
enclosure_url = node->get_prop("url");
enclosure_length = uint64.parse(node->get_prop("length"));
enclosure_type = node->get_prop("type");
}
public string url {get{return enclosure_url;}}
public uint64 length {get{return enclosure_length;}}
public string mime_type {get{return enclosure_type;}}
}
}
}