3 class IMDbDownloaderCLI : Object, IMDbDownloader {
5 Cancellable cancellable;
8 unowned IMDbSqlite sqlite;
9 // string mirrors[] = {
10 // "ftp.fu-berlin.de/pub/misc/movies/database/"
16 delegate void ParseLineFunction (string line);
19 loop = new MainLoop (null, false);
20 cancellable = new Cancellable ();
23 // IMDbDownloader implementation
25 public void download (string mirror, int _flags) throws DBus.Error {
27 stdout.printf ("Download in progress. Abort.\n");
32 Source.remove (source_id);
35 stdout.printf ("Download started (%x).", flags);
37 url = "ftp://anonymous@" + mirror;
40 Thread.create(download_thread, false);
41 } catch (ThreadError e) {
42 critical ("Failed to create download thread\n");
47 public void cancel () throws DBus.Error {
48 cancellable.cancel ();
51 public string[] get_mirrors () throws DBus.Error {
57 private bool do_download () {
58 download ("ftp.fu-berlin.de/pub/misc/movies/database/", MOVIES | GENRES | RATINGS | AKAS | PLOTS);
63 private void* download_thread () {
64 description_changed ("Connecting to FTP ...");
68 var cache_dir = Path.build_filename (Environment.get_user_cache_dir (), "cinaest");
69 DirUtils.create_with_parents (cache_dir, 0770);
71 var _sqlite = new IMDbSqlite (Path.build_filename (cache_dir, "imdb.db"));
76 var movie_parser = new MovieLineParser (sqlite);
77 var genre_parser = new GenreLineParser (sqlite);
78 var rating_parser = new RatingLineParser (sqlite);
79 var aka_parser = new AkaLineParser (sqlite);
80 var plot_parser = new PlotLineParser (sqlite);
82 var downloader = new IMDbFtpDownloader (cancellable);
83 downloader.progress_changed.connect (on_progress_changed);
85 if (MOVIES in flags) {
86 description_changed ("Downloading movie list ...");
87 downloader.download (url + "movies.list.gz", movie_parser);
89 percent_finished = 20;
90 if (GENRES in flags) {
91 description_changed ("Downloading genre data ...");
92 downloader.download (url + "genres.list.gz", genre_parser);
94 percent_finished = 40;
95 if (RATINGS in flags) {
96 description_changed ("Downloading rating data ...");
97 downloader.download (url + "ratings.list.gz", rating_parser);
99 percent_finished = 60;
101 description_changed ("Downloading alternative titles ...");
102 downloader.download (url + "aka-titles.list.gz", aka_parser);
104 percent_finished = 80;
105 if (PLOTS in flags) {
106 description_changed ("Downloading plots ...");
107 downloader.download (url + "plot.list.gz", plot_parser);
110 if (e2 is IOError.CANCELLED)
111 stdout.printf ("Download cancelled.\n");
113 warning ("Failed to open/read stream: %s\n", e2.message);
116 description_changed ("Creating indices ...");
118 sqlite.create_aka_index ();
120 sqlite.create_votes_index ();
122 if (!cancellable.is_cancelled ()) {
123 stdout.printf ("Download complete.\n");
135 private void on_progress_changed (int percent) {
136 progress (percent_finished + percent / 5);
137 stdout.printf ("%d %%\r", percent_finished + percent / 5);
141 private void timeout_quit () {
142 source_id = Timeout.add (3000, quit);
145 private bool quit () {
156 public static void main () {
157 Curl.global_init (Curl.GLOBAL_DEFAULT);
160 var downloader = new IMDbDownloaderCLI ();
162 Idle.add (downloader.do_download);
165 Curl.global_cleanup ();