Google backend: use libcurl, parse runtimes
[cinaest] / src / backends / google / google-parser.vala
index 439c0a3..d8e51a1 100644 (file)
@@ -36,17 +36,28 @@ class GoogleParser : Object {
        public string location;
        string _title;
        PatternSpec pattern;
+       CurlWrapper curlwrapper;
+       Regex re_runtime;
 
        public delegate void ReceiveMovie (GoogleMovie movie);
        public ReceiveMovie _get_callback;
 
-       private Html.Doc* get_html_document (ref char[] buf) {
-               return Html.Doc.read_memory (buf, (int) buf.length,
+       construct {
+               curlwrapper = new CurlWrapper ();
+               try {
+                       re_runtime = new Regex ("([0-9]+)hr ([0-9]+)min");
+               } catch (RegexError e) {
+                       critical ("Failed to initialize regex: %s\n", e.message);
+               }
+       }
+
+       private Html.Doc* get_html_document (string buf) {
+               return Html.Doc.read_memory ((char[]) buf, (int) buf.length,
                                             "http://movies.google.de", null, Html.ParserOption.NOERROR | Html.ParserOption.NOWARNING);
        }
 
-       public int parse (ref char[] buf) throws Error {
-               var doc = get_html_document (ref buf);
+       public int parse (string buf) throws Error {
+               var doc = get_html_document (buf);
                if (doc == null) {
                        stderr.printf ("Error: parsing failed\n");
                        return 0;
@@ -161,9 +172,15 @@ class GoogleParser : Object {
 
        private void parse_movie_info (Xml.Node* i, GoogleMovie movie) {
                var text = i->children;
-               if (text != null && text->name == "text")
+               if (text != null && text->name == "text") {
+                       MatchInfo match;
                        print ("\t\"%s\"\n", text->content);
-                       //      movie.runtime
+                       if (re_runtime.match (text->content, 0, out match)) {
+                               movie.runtime = match.fetch (1).to_int () * 3600 +
+                                               match.fetch (2).to_int () * 60;
+                       }
+                       movie.fsk = text->content.str ("Rated ").replace (" - ", "");
+               }
                for (var n = text->next; n != null; n = n->next) {
                        if (n->name == "nobr") {
                                movie.rating = parse_rating (n);
@@ -181,8 +198,8 @@ class GoogleParser : Object {
                                        if (img->name == "img") {
                                                var alt = img->get_prop ("alt"); // "Rated 0.0 out of 5.0"
                                                if (alt != null && alt != "")    //        ^
-                                                       return (int) (10 * alt.offset (6).to_double ());
                                                        print ("\trating: %s - %f\n", alt, alt.offset (6).to_double ());
+                                                       return (int) (10 * alt.offset (6).to_double ());
                                        }
                                }
                        }
@@ -234,22 +251,8 @@ class GoogleParser : Object {
 
                        stdout.printf ("GET: %s\n", uri);
 
-                       File file = File.new_for_uri (uri);
-                       InputStream stream = yield file.read_async (Priority.DEFAULT_IDLE, null);
-
-                       char[] buf = new char[256*1024];
-                       size_t nread;
-                       size_t total = 0;
-                       while (total < 256*1024) {
-                               nread = yield stream.read_async ((char *)buf + total, 256*1024 - total, Priority.DEFAULT_IDLE, cancellable);
-                               total += nread;
-                               if (cancellable.is_cancelled ())
-                                       return 0;
-                               if (nread == 0)
-                                       break;
-                       }
-                       buf[total] = 0;
-                       return parse (ref buf);
+                       string buf = yield curlwrapper.http_get (uri);
+                       return parse (buf);
                } catch (Error e) {
                        stderr.printf ("Error: %s\n", e.message);
                }