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 () {
59 download ("ftp.fu-berlin.de/pub/misc/movies/database/", MOVIES | GENRES | RATINGS | AKAS | PLOTS);
61 print ("Error: %s\n", e.message);
67 private void* download_thread () {
68 description_changed ("Connecting to FTP ...");
72 var cache_dir = Path.build_filename (Environment.get_user_cache_dir (), "cinaest");
73 DirUtils.create_with_parents (cache_dir, 0770);
75 var _sqlite = new IMDbSqlite (Path.build_filename (cache_dir, "imdb.db"));
80 var movie_parser = new MovieLineParser (sqlite);
81 var genre_parser = new GenreLineParser (sqlite);
82 var rating_parser = new RatingLineParser (sqlite);
83 var aka_parser = new AkaLineParser (sqlite);
84 var plot_parser = new PlotLineParser (sqlite);
86 var downloader = new FtpDownloader (cancellable);
88 var parser = new IMDbGzipParser (cancellable);
90 downloader.progress.connect (on_progress);
92 if (MOVIES in flags) {
93 description_changed ("Downloading movie list ...");
94 downloader.download (url + "movies.list.gz", Path.build_filename (cache_dir, "movies.list.gz"));
96 percent_finished = 20;
97 if (GENRES in flags) {
98 description_changed ("Downloading genre data ...");
99 downloader.download (url + "genres.list.gz", Path.build_filename (cache_dir, "genres.list.gz"));
101 percent_finished = 40;
102 if (RATINGS in flags) {
103 description_changed ("Downloading rating data ...");
104 downloader.download (url + "ratings.list.gz", Path.build_filename (cache_dir, "ratings.list.gz"));
106 percent_finished = 60;
108 description_changed ("Downloading alternative titles ...");
109 downloader.download (url + "aka-titles.list.gz", Path.build_filename (cache_dir, "aka-titles.list.gz"));
111 percent_finished = 80;
112 if (PLOTS in flags) {
113 description_changed ("Downloading plots ...");
114 downloader.download (url + "plot.list.gz", Path.build_filename (cache_dir, "plot.list.gz"));
117 if (MOVIES in flags) {
118 description_changed ("Parsing movie list ...");
119 parser.parse (Path.build_filename (cache_dir, "movies.list.gz"), movie_parser);
121 percent_finished = 20;
122 if (GENRES in flags) {
123 description_changed ("Parsing genre data ...");
124 parser.parse (Path.build_filename (cache_dir, "genres.list.gz"), genre_parser);
126 percent_finished = 40;
127 if (RATINGS in flags) {
128 description_changed ("Parsing rating data ...");
129 parser.parse (Path.build_filename (cache_dir, "ratings.list.gz"), rating_parser);
131 percent_finished = 60;
133 description_changed ("Parsing alternative titles ...");
134 parser.parse (Path.build_filename (cache_dir, "aka-titles.list.gz"), aka_parser);
136 percent_finished = 80;
137 if (PLOTS in flags) {
138 description_changed ("Parsing plots ...");
139 parser.parse (Path.build_filename (cache_dir, "plot.list.gz"), plot_parser);
142 if (e2 is IOError.CANCELLED)
143 stdout.printf ("Download cancelled.\n");
145 warning ("Failed to open/read stream: %s\n", e2.message);
148 description_changed ("Creating indices ...");
150 sqlite.create_aka_index ();
152 sqlite.create_votes_index ();
154 if (!cancellable.is_cancelled ()) {
155 stdout.printf ("Download complete.\n");
167 private void on_progress (int dltotal, int dlnow) {
169 progress (percent_finished + percent / 5);
170 stdout.printf ("%d %%\r", percent_finished + percent / 5);
172 stdout.printf ("%d / %d\r", dlnow, dltotal);
176 private void timeout_quit () {
177 source_id = Timeout.add (3000, quit);
180 private bool quit () {
191 public void show_desc (string desc) {
192 print ("DESC: \"%s\"\n", desc);
195 public static void main () {
196 Curl.global_init (Curl.GLOBAL_DEFAULT);
199 var downloader = new IMDbDownloaderCLI ();
200 downloader.description_changed.connect (downloader.show_desc);
202 Idle.add (downloader.do_download);
205 Curl.global_cleanup ();