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 | ACTORS);
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);
85 var person_parser = new PersonParser (sqlite);
87 var downloader = new FtpDownloader (cancellable);
89 var parser = new IMDbGzipParser (cancellable);
91 downloader.progress.connect (on_progress);
93 if (MOVIES in flags) {
94 description_changed ("Downloading movie list ...");
95 downloader.download (url + "movies.list.gz", Path.build_filename (cache_dir, "movies.list.gz"));
97 percent_finished = 20;
98 if (GENRES in flags) {
99 description_changed ("Downloading genre data ...");
100 downloader.download (url + "genres.list.gz", Path.build_filename (cache_dir, "genres.list.gz"));
102 percent_finished = 40;
103 if (RATINGS in flags) {
104 description_changed ("Downloading rating data ...");
105 downloader.download (url + "ratings.list.gz", Path.build_filename (cache_dir, "ratings.list.gz"));
107 percent_finished = 60;
109 description_changed ("Downloading alternative titles ...");
110 downloader.download (url + "aka-titles.list.gz", Path.build_filename (cache_dir, "aka-titles.list.gz"));
112 percent_finished = 80;
113 if (PLOTS in flags) {
114 description_changed ("Downloading plots ...");
115 downloader.download (url + "plot.list.gz", Path.build_filename (cache_dir, "plot.list.gz"));
117 if (ACTORS in flags) {
118 description_changed ("Downloading actors ...");
119 downloader.download (url + "actors.list.gz", Path.build_filename (cache_dir, "actors.list.gz"));
120 description_changed ("Downloading actresses ...");
121 downloader.download (url + "actresses.list.gz", Path.build_filename (cache_dir, "actresses.list.gz"));
125 if (DIRECTORS in flags) {
126 description_changed ("Downloading directors ...");
127 downloader.download (url + "directors.list.gz", Path.build_filename (cache_dir, "directors.list.gz"));
131 // production-designers
132 if (WRITERS in flags) {
133 description_changed ("Downloading writers ...");
134 downloader.download (url + "writers.list.gz", Path.build_filename (cache_dir, "writers.list.gz"));
137 if (MOVIES in flags) {
138 description_changed ("Parsing movie list ...");
139 parser.parse (Path.build_filename (cache_dir, "movies.list.gz"), movie_parser);
141 percent_finished = 20;
142 if (GENRES in flags) {
143 description_changed ("Parsing genre data ...");
144 parser.parse (Path.build_filename (cache_dir, "genres.list.gz"), genre_parser);
146 percent_finished = 40;
147 if (RATINGS in flags) {
148 description_changed ("Parsing rating data ...");
149 parser.parse (Path.build_filename (cache_dir, "ratings.list.gz"), rating_parser);
151 percent_finished = 60;
153 description_changed ("Parsing alternative titles ...");
154 parser.parse (Path.build_filename (cache_dir, "aka-titles.list.gz"), aka_parser);
156 percent_finished = 80;
157 if (PLOTS in flags) {
158 description_changed ("Parsing plots ...");
159 parser.parse (Path.build_filename (cache_dir, "plot.list.gz"), plot_parser);
161 if (ACTORS in flags) {
162 description_changed ("Parsing actors ...");
163 parser.parse (Path.build_filename (cache_dir, "actors.list.gz"), person_parser);
164 person_parser.reset ();
165 description_changed ("Parsing actresses ...");
166 parser.parse (Path.build_filename (cache_dir, "actresses.list.gz"), person_parser);
169 if (e2 is IOError.CANCELLED)
170 stdout.printf ("Download cancelled.\n");
172 warning ("Failed to open/read stream: %s\n", e2.message);
175 description_changed ("Creating indices ...");
177 sqlite.create_aka_index ();
179 sqlite.create_votes_index ();
181 if (!cancellable.is_cancelled ()) {
182 stdout.printf ("Download complete.\n");
194 private void on_progress (int dltotal, int dlnow) {
196 progress (percent_finished + percent / 5);
197 stdout.printf ("%d %%\r", percent_finished + percent / 5);
199 stdout.printf ("%d / %d\r", dlnow, dltotal);
203 private void timeout_quit () {
204 source_id = Timeout.add (3000, quit);
207 private bool quit () {
218 public void show_desc (string desc) {
219 print ("DESC: \"%s\"\n", desc);
222 public static void main () {
223 Curl.global_init (Curl.GLOBAL_DEFAULT);
226 var downloader = new IMDbDownloaderCLI ();
227 downloader.description_changed.connect (downloader.show_desc);
229 Idle.add (downloader.do_download);
232 Curl.global_cleanup ();