673004952c6f7db70229d576d038befc2d5c95a4
[maevies] / examples / gmovies.c
1 /* Parses a Google movies web (previously downloaded on a file)
2  * using libxml2. Examples of Google movies web files can be
3  * found in the gmovies_data directory.
4  */ 
5
6 #include <libxml/HTMLparser.h>
7 #include <libxml/tree.h>
8 #include <rest/rest/rest-proxy.h>
9 #include <glib.h>
10 #include <stdio.h>
11 #include <unistd.h>
12
13
14 xmlNodePtr getSiblingByName(xmlNodePtr node, xmlChar* name, int nameLen)
15 {
16         
17         xmlNodePtr sibling = node->next;
18         while((sibling != NULL) && (strncmp(sibling->name, name, nameLen) != 0)) {
19                 sibling = sibling->next;
20         }
21         
22         return sibling;
23         
24
25
26 xmlNodePtr getChildByName(xmlNodePtr node, xmlChar* name, int nameLen)
27 {
28         return getSiblingByName(node->children, name, nameLen);
29 }
30
31 xmlNodePtr getFirstSiblingByAttributeValue(
32         xmlNodePtr sibling, xmlChar* attr, xmlChar * attrValue, int attrValueLen)
33 {
34         xmlNodePtr tempNode = sibling;
35         
36         while(tempNode != NULL) {
37                 xmlChar* value = xmlGetProp(tempNode, attr);
38                 if ((value != NULL) && (strncmp(value, attrValue, attrValueLen)) == 0)
39                         return tempNode;
40                 tempNode = tempNode->next;
41         }
42         
43         return NULL;
44 }
45
46
47 xmlNodePtr getFirstChildByAttributeValue(
48         xmlNodePtr node, xmlChar* attr, xmlChar * attrValue, int attrValueLen)
49 {
50         return getFirstSiblingByAttributeValue(node->children, attr, attrValue, attrValueLen);
51 }
52
53
54 xmlNodePtr jumpXSiblings(xmlNodePtr node, int siblings)
55 {
56         xmlNodePtr r = node;
57         
58         int i = 0;
59         for(; i<siblings; i++) {
60                 r = r->next;
61         }
62         
63         return r;
64 }
65
66 int isSeparatorTR(xmlNodePtr node)
67 {
68         return ((node != NULL) && (childrenCount(node) == 1));
69 }
70
71 int childrenCount(xmlNodePtr node)
72 {
73         int i=0;
74         xmlNodePtr nav = node->children;
75         while(nav != NULL) {
76                 i++;
77                 nav = nav->next;
78         }
79         
80         return i;
81 }
82
83
84 int startsTheatherData(xmlNodePtr node)
85 {
86         
87         if (strncmp(node->name, "tr", 2) == 0) {
88                 xmlNodePtr td = node->children;
89                 if ((td != NULL) && (strncmp(td->name, "td", 2) == 0)) {
90                         xmlChar* value = xmlGetProp(td, "colspan");
91                         return ((value != NULL) && (strncmp(value, "4", 1)) == 0);
92                 }
93         }
94         
95         return -1;
96 }
97
98
99 int main (int argc, char ** argv)
100 {
101
102         if (argc != 2) {
103                 printf("usage: gmovies file.html\n");
104                 exit(-1);
105         }
106
107         RestProxy *proxy;
108         RestProxyCall *call;
109         const gchar *payload;
110         const char *city = argv[1];
111         gssize len;
112
113         g_thread_init(NULL);
114         g_type_init();
115
116         proxy = rest_proxy_new(
117                         "http://www.google.com/movies",
118                         FALSE);
119         call = rest_proxy_new_call(proxy);
120
121         rest_proxy_call_add_params(call,
122                         "near", city,
123                         NULL);
124         rest_proxy_call_run(call, NULL, NULL);
125
126         payload = rest_proxy_call_get_payload(call);
127         len = rest_proxy_call_get_payload_length(call);
128         
129         //write(1, payload, len);
130         //printf("\n\n");
131
132         htmlDocPtr doc = htmlReadMemory(payload, len, "http://movies.google.com", "UTF-8", HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING);
133
134         xmlNodePtr root = xmlDocGetRootElement(doc);//html
135         
136         //get the body node
137         xmlNodePtr body = getSiblingByName(root->children, "body", 4);
138         
139         xmlNodePtr tempNode = getFirstChildByAttributeValue(body, "id", "results", 8); //the data is a div with id = results
140         
141         if (tempNode == NULL) {
142                 printf("results div not found.\n");
143                 exit(-1);
144         }
145         
146         tempNode = getFirstChildByAttributeValue(tempNode, "id", "movie_results", 14);
147         
148         if (tempNode == NULL) {
149                 printf("movie_results div not found.\n");
150                 exit(-1);
151         }
152         
153         tempNode = getFirstChildByAttributeValue(tempNode, "class", "movie_results", 14);
154         
155         if (tempNode == NULL) {
156                 printf("movie_results class not found.\n");
157                 exit(-1);
158         }
159         
160         //look for theaters
161         xmlNodePtr nav = tempNode->children;
162         while(nav != NULL) {
163                 tempNode = getFirstSiblingByAttributeValue(nav, "class", "theater", 7);
164                 tempNode = getFirstChildByAttributeValue(tempNode, "class", "desc", 4);
165                 if (tempNode != NULL) {                 
166                         tempNode = getFirstChildByAttributeValue(tempNode, "class", "name", 4);
167                         printf("Info = %s\n", xmlNodeGetContent(tempNode->children->children));
168                 }
169                 nav = nav->next;
170         }
171         
172         exit(0);
173         
174         //get the form node inside body, the data is in the next node (a table)
175         xmlNodePtr dataTable = getChildByName(body, "form", 4)->next;
176
177 /*
178         //tbody
179         xmlNodePtr elem = dataTable;
180         //xmlNodePtr nav = dataTable->children;
181         int i = 0;
182         while(nav != NULL) {
183                 elem = nav;
184                 if (startsTheatherData(elem)) {
185                         elem = elem->children; //td
186                         elem = elem->children; //a
187                         elem = elem->children; //b
188
189                         printf("Theather %d = %s\n", i++, xmlNodeGetContent(elem));
190                         printf("-------------------------------------------------\n");
191                         
192                         xmlNodePtr n1 = nav->next; //in this tr there is 4 td with 2 film data
193                         while(!startsTheatherData(n1) && !isSeparatorTR(n1)) {
194                                 elem = n1->children->next; //the first td is for rating
195                                 printf("%s\n", xmlNodeGetContent(elem->children->children));
196                                 if (childrenCount(n1->children) > 2) {
197                                         elem = elem->next->next; //the first td is for rating
198                                         printf("%s\n", xmlNodeGetContent(elem->children->children));
199                                 }
200                                 n1 = n1->next;
201                         }
202                         
203                         printf("\n\n");
204                 }
205                 nav = nav->next;
206         }
207 */
208
209 }