IMDb: Use SQLite indices instead of (half) MD5 hashes for lookup
[cinaest] / src / imdb / imdb-plaintext-downloader.vala
index 84a48b9..bddfff0 100644 (file)
@@ -116,6 +116,8 @@ class IMDbDownloadServer : Object, IMDbDownloader {
                        if (MOVIES in flags) {
                                description_changed ("Downloading movie list ...");
                                yield download_async(movies, movie_parser, io_priority);
+                               description_changed ("Creating title index ...");
+                               sqlite.create_title_index ();
                        }
                        if (GENRES in flags) {
                                description_changed ("Downloading genre data ...");
@@ -132,11 +134,6 @@ class IMDbDownloadServer : Object, IMDbDownloader {
                                warning ("Failed to open/read stream: %s\n", e2.message);
                }
 
-               if (!cancellable.is_cancelled ()) {
-                       stdout.printf ("Download complete.\n");
-                       progress (100);
-               }
-
                try {
                        bool unmounted = yield m.unmount(MountUnmountFlags.NONE, null);
                        if (!unmounted) {
@@ -146,6 +143,14 @@ class IMDbDownloadServer : Object, IMDbDownloader {
                        warning ("Failed to unmount: %s\n", e4.message);
                }
 
+               description_changed ("Creating indices ...");
+               sqlite.create_votes_index ();
+
+               if (!cancellable.is_cancelled ()) {
+                       stdout.printf ("Download complete.\n");
+                       progress (100);
+               }
+
                sqlite = null;
                running = false;
 
@@ -182,7 +187,8 @@ class IMDbDownloadServer : Object, IMDbDownloader {
                        int p = (int) (100 * (sofar + stream.total_in ()) / total);
                        if (p > percent) {
                                percent = p;
-                               progress (p);
+                               if (p < 100)
+                                       progress (p);
                        }
                } while (line != null);
 
@@ -320,7 +326,7 @@ class RatingLineParser : LineParser {
        public RatingLineParser (IMDbSqlite _sqlite) {
                base (_sqlite);
                try {
-                       re_rating = new Regex ("^      .+ +[0-9]+ +([0-9.]+) +(.+)$");
+                       re_rating = new Regex ("^      .+ +([0-9]+) +([0-9.]+) +(.+)$");
                } catch (RegexError e) {
                        critical ("Failed to initialize regex: %s\n", e.message);
                }
@@ -337,9 +343,10 @@ class RatingLineParser : LineParser {
                        return;
 
                string title;
-               string rating = matchinfo.fetch (1);
+               string votes = matchinfo.fetch (1);
+               string rating = matchinfo.fetch (2);
                try {
-                       title = convert(matchinfo.fetch (2), -1, "utf-8", "latin1");
+                       title = convert(matchinfo.fetch (3), -1, "utf-8", "latin1");
                } catch (ConvertError e) {
                        return;
                }
@@ -351,6 +358,6 @@ class RatingLineParser : LineParser {
                if (skip_title (title))
                        return;
 
-               sqlite.movie_set_rating (title, (int) (rating.to_double () * 10));
+               sqlite.movie_set_rating (title, (int) (rating.to_double () * 10), votes.to_int ());
        }
 }