393a21fce2e36d6265eef60522e248d9cab853d9
[cinaest] / src / imdb / imdb-downloader-cli.vala
1 using GLib;
2
3 class IMDbDownloaderCLI : Object, IMDbDownloader {
4         MainLoop loop;
5         Cancellable cancellable;
6         bool running;
7         uint source_id;
8         unowned IMDbSqlite sqlite;
9 //      string mirrors[] = {
10 //              "ftp.fu-berlin.de/pub/misc/movies/database/"
11 //      };
12         string url;
13         int flags;
14         int percent_finished;
15
16         delegate void ParseLineFunction (string line);
17
18         construct {
19                 loop = new MainLoop (null, false);
20                 cancellable = new Cancellable ();
21         }
22
23         // IMDbDownloader implementation
24
25         public void download (string mirror, int _flags) throws DBus.Error {
26                 if (running) {
27                         stdout.printf ("Download in progress. Abort.\n");
28                         return;
29                 }
30                 running = true;
31                 if (source_id != 0) {
32                         Source.remove (source_id);
33                 }
34
35                 stdout.printf ("Download started (%x).", flags);
36                 progress (0);
37                 url = "ftp://anonymous@" + mirror;
38                 flags = _flags;
39                 try {
40                         Thread.create(download_thread, false);
41                 } catch (ThreadError e) {
42                         critical ("Failed to create download thread\n");
43                         return;
44                 }
45         }
46
47         public void cancel () throws DBus.Error {
48                 cancellable.cancel ();
49         }
50
51         public string[] get_mirrors () throws DBus.Error {
52                 return null;
53         }
54
55         // Private methods
56
57         private bool do_download () {
58                 try {
59                         download ("ftp.fu-berlin.de/pub/misc/movies/database/", MOVIES | GENRES | RATINGS | AKAS | PLOTS);
60                 } catch (Error e) {
61                         print ("Error: %s\n", e.message);
62                 }
63
64                 return false;
65         }
66
67         private void* download_thread () {
68                 description_changed ("Connecting to FTP ...");
69                 progress (0);
70                 percent_finished = 0;
71
72                 var cache_dir = Path.build_filename (Environment.get_user_cache_dir (), "cinaest");
73                 DirUtils.create_with_parents (cache_dir, 0770);
74
75                 var _sqlite = new IMDbSqlite (Path.build_filename (cache_dir, "imdb.db"));
76                 sqlite = _sqlite;
77                 _sqlite.clear ();
78
79                 try {
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
86                         var downloader = new FtpDownloader (cancellable);
87
88                         var parser = new IMDbGzipParser (cancellable);
89
90                         downloader.progress.connect (on_progress);
91
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"));
95                         }
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"));
100                         }
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"));
105                         }
106                         percent_finished = 60;
107                         if (AKAS in flags) {
108                                 description_changed ("Downloading alternative titles ...");
109                                 downloader.download (url + "aka-titles.list.gz", Path.build_filename (cache_dir, "aka-titles.list.gz"));
110                         }
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"));
115                         }
116
117                         if (MOVIES in flags) {
118                                 description_changed ("Parsing movie list ...");
119                                 parser.parse (Path.build_filename (cache_dir, "movies.list.gz"), movie_parser);
120                         }
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);
125                         }
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);
130                         }
131                         percent_finished = 60;
132                         if (AKAS in flags) {
133                                 description_changed ("Parsing alternative titles ...");
134                                 parser.parse (Path.build_filename (cache_dir, "aka-titles.list.gz"), aka_parser);
135                         }
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);
140                         }
141                 } catch (Error e2) {
142                         if (e2 is IOError.CANCELLED)
143                                 stdout.printf ("Download cancelled.\n");
144                         else
145                                 warning ("Failed to open/read stream: %s\n", e2.message);
146                 }
147
148                 description_changed ("Creating indices ...");
149                 if (AKAS in flags)
150                         sqlite.create_aka_index ();
151                 if (MOVIES in flags)
152                         sqlite.create_votes_index ();
153
154                 if (!cancellable.is_cancelled ()) {
155                         stdout.printf ("Download complete.\n");
156                         progress (100);
157                 }
158
159                 sqlite = null;
160                 running = false;
161
162                 timeout_quit ();
163
164                 return null;
165         }
166
167         private void on_progress (int dltotal, int dlnow) {
168         /*
169                 progress (percent_finished + percent / 5);
170                 stdout.printf ("%d %%\r", percent_finished + percent / 5);
171         */
172                 stdout.printf ("%d / %d\r", dlnow, dltotal);
173                 stdout.flush ();
174         }
175
176         private void timeout_quit () {
177                 source_id = Timeout.add (3000, quit);
178         }
179
180         private bool quit () {
181                 loop.quit ();
182
183                 // One-shot only
184                 return false;
185         }
186
187         public void run () {
188                 loop.run ();
189         }
190
191         public void show_desc (string desc) {
192                 print ("DESC: \"%s\"\n", desc);
193         }
194
195         public static void main () {
196                 Curl.global_init (Curl.GLOBAL_DEFAULT);
197
198                 // Start server
199                 var downloader = new IMDbDownloaderCLI ();
200                 downloader.description_changed.connect (downloader.show_desc);
201
202                 Idle.add (downloader.do_download);
203                 downloader.run ();
204
205                 Curl.global_cleanup ();
206         }
207 }