Add IMDb CLI downloader to test the IMDb downloader
authorPhilipp Zabel <philipp.zabel@gmail.com>
Sun, 3 Jan 2010 16:06:45 +0000 (17:06 +0100)
committerPhilipp Zabel <philipp.zabel@gmail.com>
Wed, 14 Jul 2010 21:34:08 +0000 (23:34 +0200)
src/imdb/imdb-downloader-cli.vala [new file with mode: 0644]

diff --git a/src/imdb/imdb-downloader-cli.vala b/src/imdb/imdb-downloader-cli.vala
new file mode 100644 (file)
index 0000000..07da9ea
--- /dev/null
@@ -0,0 +1,167 @@
+using GLib;
+
+class IMDbDownloaderCLI : Object, IMDbDownloader {
+       MainLoop loop;
+       Cancellable cancellable;
+       bool running;
+       uint source_id;
+       unowned IMDbSqlite sqlite;
+//     string mirrors[] = {
+//             "ftp.fu-berlin.de/pub/misc/movies/database/"
+//     };
+       string url;
+       int flags;
+       int percent_finished;
+
+       delegate void ParseLineFunction (string line);
+
+       construct {
+               loop = new MainLoop (null, false);
+               cancellable = new Cancellable ();
+       }
+
+       // IMDbDownloader implementation
+
+       public void download (string mirror, int _flags) throws DBus.Error {
+               if (running) {
+                       stdout.printf ("Download in progress. Abort.\n");
+                       return;
+               }
+               running = true;
+               if (source_id != 0) {
+                       Source.remove (source_id);
+               }
+
+               stdout.printf ("Download started (%x).", flags);
+               progress (0);
+               url = "ftp://anonymous@" + mirror;
+               flags = _flags;
+               try {
+                       Thread.create(download_thread, false);
+               } catch (ThreadError e) {
+                       critical ("Failed to create download thread\n");
+                       return;
+               }
+       }
+
+       public void cancel () throws DBus.Error {
+               cancellable.cancel ();
+       }
+
+       public string[] get_mirrors () throws DBus.Error {
+               return null;
+       }
+
+       // Private methods
+
+       private bool do_download () {
+               download ("ftp.fu-berlin.de/pub/misc/movies/database/", MOVIES | GENRES | RATINGS | AKAS | PLOTS);
+
+               return false;
+       }
+
+       private void* download_thread () {
+               description_changed ("Connecting to FTP ...");
+               progress (0);
+               percent_finished = 0;
+
+               var cache_dir = Path.build_filename (Environment.get_user_cache_dir (), "cinaest");
+               DirUtils.create_with_parents (cache_dir, 0770);
+
+               var _sqlite = new IMDbSqlite (Path.build_filename (cache_dir, "imdb.db"));
+               sqlite = _sqlite;
+               _sqlite.clear ();
+
+               try {
+                       var movie_parser = new MovieLineParser (sqlite);
+                       var genre_parser = new GenreLineParser (sqlite);
+                       var rating_parser = new RatingLineParser (sqlite);
+                       var aka_parser = new AkaLineParser (sqlite);
+                       var plot_parser = new PlotLineParser (sqlite);
+
+                       var downloader = new IMDbFtpDownloader (cancellable);
+                       downloader.progress_changed.connect (on_progress_changed);
+
+                       if (MOVIES in flags) {
+                               description_changed ("Downloading movie list ...");
+                               downloader.download (url + "movies.list.gz", movie_parser);
+                       }
+                       percent_finished = 20;
+                       if (GENRES in flags) {
+                               description_changed ("Downloading genre data ...");
+                               downloader.download (url + "genres.list.gz", genre_parser);
+                       }
+                       percent_finished = 40;
+                       if (RATINGS in flags) {
+                               description_changed ("Downloading rating data ...");
+                               downloader.download (url + "ratings.list.gz", rating_parser);
+                       }
+                       percent_finished = 60;
+                       if (AKAS in flags) {
+                               description_changed ("Downloading alternative titles ...");
+                               downloader.download (url + "aka-titles.list.gz", aka_parser);
+                       }
+                       percent_finished = 80;
+                       if (PLOTS in flags) {
+                               description_changed ("Downloading plots ...");
+                               downloader.download (url + "plot.list.gz", plot_parser);
+                       }
+               } catch (Error e2) {
+                       if (e2 is IOError.CANCELLED)
+                               stdout.printf ("Download cancelled.\n");
+                       else
+                               warning ("Failed to open/read stream: %s\n", e2.message);
+               }
+
+               description_changed ("Creating indices ...");
+               if (AKAS in flags)
+                       sqlite.create_aka_index ();
+               if (MOVIES in flags)
+                       sqlite.create_votes_index ();
+
+               if (!cancellable.is_cancelled ()) {
+                       stdout.printf ("Download complete.\n");
+                       progress (100);
+               }
+
+               sqlite = null;
+               running = false;
+
+               timeout_quit ();
+
+               return null;
+       }
+
+       private void on_progress_changed (int percent) {
+               progress (percent_finished + percent / 5);
+               stdout.printf ("%d %%\r", percent_finished + percent / 5);
+               stdout.flush ();
+       }
+
+       private void timeout_quit () {
+               source_id = Timeout.add (3000, quit);
+       }
+
+        private bool quit () {
+               loop.quit ();
+
+                // One-shot only
+                return false;
+        }
+
+       public void run () {
+               loop.run ();
+       }
+
+       public static void main () {
+               Curl.global_init (Curl.GLOBAL_DEFAULT);
+
+               // Start server
+               var downloader = new IMDbDownloaderCLI ();
+
+               Idle.add (downloader.do_download);
+               downloader.run ();
+
+               Curl.global_cleanup ();
+       }
+}