[svn r301] Get ProgramGuide info from XML+Http at gmyth_http trunk
authormorphbr
Wed Jan 24 21:52:23 2007 +0000 (2007-01-24)
branchtrunk
changeset 30001d60f80fa52
parent 299 a84d2982ff01
child 301 c3fdac309aec
[svn r301] Get ProgramGuide info from XML+Http at gmyth_http
gmyth/src/gmyth_http.c
gmyth/src/gmyth_http.h
gmyth/tests/http.c
     1.1 --- a/gmyth/src/gmyth_http.c	Wed Jan 24 21:03:05 2007 +0000
     1.2 +++ b/gmyth/src/gmyth_http.c	Wed Jan 24 21:52:23 2007 +0000
     1.3 @@ -34,6 +34,7 @@
     1.4  #include <assert.h>
     1.5  #include <libxml/parser.h>
     1.6  #include <libxml/tree.h>
     1.7 +#include <libxml/xpath.h>
     1.8  
     1.9  #include "gmyth_http.h"
    1.10  #include "gmyth_debug.h"
    1.11 @@ -45,6 +46,102 @@
    1.12    size_t size;
    1.13  };
    1.14  
    1.15 +
    1.16 +xmlXPathObjectPtr
    1.17 +getnodeset(xmlDocPtr doc, xmlChar *xpath)
    1.18 +{
    1.19 +
    1.20 +    xmlXPathContextPtr context;
    1.21 +    xmlXPathObjectPtr result;
    1.22 +
    1.23 +    context = xmlXPathNewContext(doc);
    1.24 +    result = xmlXPathEvalExpression(xpath, context);
    1.25 +
    1.26 +    if(xmlXPathNodeSetIsEmpty(result->nodesetval))
    1.27 +    {
    1.28 +        g_printf("No result\n");
    1.29 +        return NULL;
    1.30 +    }
    1.31 +
    1.32 +    xmlXPathFreeContext(context);
    1.33 +    return result;
    1.34 +}
    1.35 +
    1.36 +
    1.37 +xmlDocPtr XMLParse (const char *content, int length) 
    1.38 +{
    1.39 +    xmlDocPtr doc; /* the resulting document tree */
    1.40 +
    1.41 +    doc = xmlReadMemory(content, length, NULL, NULL, 0);
    1.42 +    if (doc == NULL) 
    1.43 +    {
    1.44 +        g_fprintf(stderr, "Failed to parse document\n");
    1.45 +        return NULL;
    1.46 +    }
    1.47 +
    1.48 +    return doc;
    1.49 +}
    1.50 +
    1.51 +xmlXPathObjectPtr getXPath (xmlChar *xpath, xmlDocPtr doc)
    1.52 +{
    1.53 +    xmlXPathObjectPtr result;
    1.54 +    result = getnodeset(doc, xpath);
    1.55 +    return result;
    1.56 +}
    1.57 +
    1.58 +void get_ProgramGuide_Properties(xmlNodePtr nodeTab, GMythEpg* epg)
    1.59 +{
    1.60 +	sscanf ((char *)xmlGetProp(nodeTab, (xmlChar *)"startChanId"), "%d", &(epg->startChanId));
    1.61 +	sscanf ((char *)xmlGetProp(nodeTab, (xmlChar *)"endChanId"), "%d", &(epg->endChanId));
    1.62 +
    1.63 +	epg->version = g_string_new((char *)xmlGetProp(nodeTab, (xmlChar *)"version"));
    1.64 +
    1.65 +	sscanf ((char *)xmlGetProp(nodeTab, (xmlChar *)"protoVer"), "%d", &(epg->protoVer));
    1.66 +	sscanf ((char *)xmlGetProp(nodeTab, (xmlChar *)"totalCount"), "%d", &(epg->totalCount));
    1.67 +	sscanf ((char *)xmlGetProp(nodeTab, (xmlChar *)"numOfChannels"), "%d", &(epg->numOfChannels));
    1.68 +
    1.69 +	epg->asOf = gmyth_util_string_to_time_val ((char *)xmlGetProp(nodeTab, (xmlChar *)"asOf"));
    1.70 +	epg->startTime = gmyth_util_string_to_time_val ((char *)xmlGetProp(nodeTab, (xmlChar *)"startTime"));
    1.71 +	epg->endTime = gmyth_util_string_to_time_val ((char *)xmlGetProp(nodeTab, (xmlChar *)"endTime"));
    1.72 +
    1.73 +	sscanf ((char *)xmlGetProp(nodeTab, (xmlChar *)"details"), "%d", &(epg->details));
    1.74 +
    1.75 +}
    1.76 +
    1.77 +void getEpg (xmlDocPtr doc)
    1.78 +{
    1.79 +    xmlXPathObjectPtr result;
    1.80 +    xmlNodeSetPtr nodeset;
    1.81 +    xmlChar *keyword;
    1.82 +	GMythEpg epg;
    1.83 +
    1.84 +    int i = 0;
    1.85 +    result = getXPath((xmlChar *)"/*", doc);
    1.86 +
    1.87 +
    1.88 +    if (result) 
    1.89 +    {
    1.90 +        nodeset = result->nodesetval;
    1.91 +        for (i=0; i < nodeset->nodeNr; i++) 
    1.92 +        {
    1.93 +            keyword = nodeset->nodeTab[i]->name;
    1.94 +
    1.95 +			if (g_ascii_strcasecmp(keyword, "ProgramGuide") == 0)
    1.96 +				get_ProgramGuide_Properties(nodeset->nodeTab[i], &epg);
    1.97 +// 
    1.98 +// 			else if g_ascii_strcasecmp(keyword, "Channel")
    1.99 +// 				get_Channel_Properties(nodeTab[i]);
   1.100 +// 
   1.101 +// 			else if g_ascii_strcasecmp(keyword, "Program")
   1.102 +// 				get_Program_Properties(nodeTab[i]);
   1.103 +
   1.104 +            g_printf("keyword: %s\n", keyword);
   1.105 +        }
   1.106 +
   1.107 +        xmlXPathFreeObject (result);
   1.108 +    }	
   1.109 +}
   1.110 +
   1.111  /* Aux functions got from libcurl */
   1.112  void *myrealloc(void *ptr, size_t size)
   1.113  {
   1.114 @@ -82,16 +179,16 @@
   1.115  {
   1.116      va_list args;
   1.117      va_start(args, command);
   1.118 -    char* s = NULL;
   1.119 +    gchar* s = NULL;
   1.120  
   1.121      g_string_append(command, "?");
   1.122  
   1.123      /* Sintax: var, value */
   1.124 -    while ( (s = va_arg(args, char *)) != NULL ) 
   1.125 +    while ( (s = va_arg(args, gchar *)) != NULL ) 
   1.126      {
   1.127          g_string_append(command, s);
   1.128          g_string_append(command, "=");
   1.129 -        s = va_arg(args, char *);
   1.130 +        s = va_arg(args, gchar *);
   1.131          g_string_append(command, s);
   1.132          g_string_append(command, "&");
   1.133      }
   1.134 @@ -110,12 +207,12 @@
   1.135  GString 
   1.136  *gmyth_http_request (GMythBackendInfo *backend_info, GString *command)
   1.137  {
   1.138 -
   1.139 +	
   1.140      LIBXML_TEST_VERSION
   1.141  
   1.142      size_t size = strlen(backend_info->hostname) + strlen(command->str) + 13;
   1.143 -    char *URL = malloc(sizeof(char)*size);
   1.144 -    snprintf(URL, size+1, "http://%s:6544/%s", backend_info->hostname, command->str);     
   1.145 +    gchar *URL = malloc(sizeof(gchar)*size);
   1.146 +    g_snprintf(URL, size+1, "http://%s:6544/%s", backend_info->hostname, command->str);     
   1.147  
   1.148      CURL *curl_handle;
   1.149      
   1.150 @@ -152,6 +249,8 @@
   1.151  
   1.152      if(chunk.memory)
   1.153      {
   1.154 +        xmlDocPtr doc = XMLParse(chunk.memory, strlen(chunk.memory));
   1.155 +        getEpg(doc);
   1.156          response = g_string_new(chunk.memory);
   1.157          free(chunk.memory);
   1.158      }
     2.1 --- a/gmyth/src/gmyth_http.h	Wed Jan 24 21:03:05 2007 +0000
     2.2 +++ b/gmyth/src/gmyth_http.h	Wed Jan 24 21:52:23 2007 +0000
     2.3 @@ -35,8 +35,11 @@
     2.4  #include <stdlib.h>
     2.5  #include <string.h>
     2.6  #include <stdarg.h>
     2.7 +#include <glib.h>
     2.8 +#include <glib/gprintf.h>
     2.9  
    2.10  #include "gmyth_backendinfo.h"
    2.11 +#include "gmyth_util.h"
    2.12  
    2.13  #include <curl/curl.h>
    2.14  #include <curl/types.h>
    2.15 @@ -49,7 +52,10 @@
    2.16  #define TYPE_TEXT_XML 0
    2.17  #define TYPE_FORM_URLENCODED 1
    2.18  
    2.19 -typedef struct _GMythPacket     GMythPacket;       
    2.20 +typedef struct _GMythPacket     GMythPacket;
    2.21 +typedef struct _GMythProgram    GMythProgram;
    2.22 +typedef struct _GMythChannel	GMythChannel;
    2.23 +typedef struct _GMythEpg		GMythEpg;
    2.24  
    2.25  struct _GMythPacket
    2.26  {
    2.27 @@ -57,6 +63,43 @@
    2.28      int    type;
    2.29  };
    2.30  
    2.31 +struct _GMythProgram
    2.32 +{
    2.33 +    GString* title;
    2.34 +	GString* subtitle;
    2.35 +	GString* catType;
    2.36 +    GString* category;
    2.37 +	gint	repeat;
    2.38 +	GTimeVal* startTime;
    2.39 +	GTimeVal* endTime;
    2.40 +};
    2.41 +
    2.42 +//gmyth_util_string_to_time_val ( const gchar* time_str )
    2.43 +
    2.44 +struct _GMythChannel
    2.45 +{
    2.46 +	GString* channelName;
    2.47 +	GString* chanNum;
    2.48 +	gint	chanId;
    2.49 +	gint	callSign;
    2.50 +	GSList	programList;
    2.51 +};
    2.52 +
    2.53 +struct _GMythEpg
    2.54 +{
    2.55 +	gint startChanId;
    2.56 +	gint endChanId;
    2.57 +	GString* version;
    2.58 +	gint protoVer;
    2.59 +	gint totalCount;
    2.60 +	gint numOfChannels;
    2.61 +	GTimeVal* asOf;
    2.62 +	GTimeVal* startTime;
    2.63 +	GTimeVal* endTime;
    2.64 +	gint details;
    2.65 +	GSList channelList;
    2.66 +};
    2.67 +
    2.68  GString* gmyth_http_create_command(GString *command, ...);
    2.69  GString *gmyth_http_request (GMythBackendInfo *backend_info, GString *command);
    2.70  
     3.1 --- a/gmyth/tests/http.c	Wed Jan 24 21:03:05 2007 +0000
     3.2 +++ b/gmyth/tests/http.c	Wed Jan 24 21:52:23 2007 +0000
     3.3 @@ -1,12 +1,13 @@
     3.4  #include <glib-object.h>
     3.5  #include "gmyth.h"
     3.6 +#include <glib.h>
     3.7  
     3.8  int
     3.9  main (int args, const char **argv)
    3.10  {
    3.11      GMythBackendInfo *backend_info;
    3.12 -    g_type_init ();
    3.13 -    g_thread_init (NULL);
    3.14 +    g_type_init();
    3.15 +    //g_thread_init(NULL);
    3.16  
    3.17      backend_info = gmyth_backend_info_new ();
    3.18