a8462c0ad51931be580c9f4fcb65e119cd7df543
[cinaest] / src / imdb / imdb-plaintext-downloader.vala
1 using GLib;
2
3 class IMDbDownloadServer : 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                 "ftp.funet.fi/pub/mirrors/ftp.imdb.com/pub/",
12                 "ftp.sunet.se/pub/tv+movies/imdb/"
13         };
14         string url;
15         int flags;
16         int percent_finished;
17
18         delegate void ParseLineFunction (string line);
19
20         construct {
21                 loop = new MainLoop (null, false);
22                 cancellable = new Cancellable ();
23         }
24
25         // IMDbDownloader implementation
26
27         public void download (string mirror, int _flags) throws DBus.Error {
28                 if (running) {
29                         stdout.printf ("Download in progress. Abort.\n");
30                         return;
31                 }
32                 running = true;
33                 if (source_id != 0) {
34                         Source.remove (source_id);
35                 }
36
37                 stdout.printf ("Download started (%x).", flags);
38                 progress (0);
39                 url = "ftp://anonymous@" + mirror;
40                 flags = _flags;
41                 try {
42                         Thread.create(download_thread, false);
43                 } catch (ThreadError e) {
44                         critical ("Failed to create download thread\n");
45                         return;
46                 }
47         }
48
49         public void cancel () throws DBus.Error {
50                 cancellable.cancel ();
51         }
52
53         public string[] get_mirrors () throws DBus.Error {
54                 return mirrors;
55         }
56
57         // Private methods
58
59         private void* download_thread () {
60                 description_changed ("Connecting to FTP ...");
61                 progress (0);
62                 percent_finished = 0;
63
64                 var cache_dir = Path.build_filename (Environment.get_user_cache_dir (), "cinaest");
65                 DirUtils.create_with_parents (cache_dir, 0770);
66
67                 var _sqlite = new IMDbSqlite (Path.build_filename (cache_dir, "imdb.db"));
68                 sqlite = _sqlite;
69                 _sqlite.clear ();
70
71                 try {
72                         var movie_parser = new MovieLineParser (sqlite);
73                         var genre_parser = new GenreLineParser (sqlite);
74                         var rating_parser = new RatingLineParser (sqlite);
75                         var aka_parser = new AkaLineParser (sqlite);
76                         var plot_parser = new PlotLineParser (sqlite);
77
78                         var downloader = new FtpDownloader (cancellable);
79                         downloader.progress.connect (on_progress);
80
81                         var parser = new IMDbGzipParser (cancellable);
82
83                         if (MOVIES in flags) {
84                                 description_changed ("Downloading movie list ...");
85                                 downloader.download (url + "movies.list.gz", Path.build_filename (cache_dir, "movies.list.gz"));
86                         }
87                         percent_finished = 20;
88                         if (GENRES in flags) {
89                                 description_changed ("Downloading genre data ...");
90                                 downloader.download (url + "genres.list.gz", Path.build_filename (cache_dir, "genres.list.gz"));
91                         }
92                         percent_finished = 40;
93                         if (RATINGS in flags) {
94                                 description_changed ("Downloading rating data ...");
95                                 downloader.download (url + "ratings.list.gz", Path.build_filename (cache_dir, "ratings.list.gz"));
96                         }
97                         percent_finished = 60;
98                         if (AKAS in flags) {
99                                 description_changed ("Downloading alternative titles ...");
100                                 downloader.download (url + "aka-titles.list.gz", Path.build_filename (cache_dir, "aka-titles.list.gz"));
101                         }
102                         percent_finished = 80;
103                         if (PLOTS in flags) {
104                                 description_changed ("Downloading plots ...");
105                                 downloader.download (url + "plot.list.gz", Path.build_filename (cache_dir, "plot.list.gz"));
106                         }
107
108                         if (MOVIES in flags) {
109                                 description_changed ("Parsing movie list ...");
110                                 parser.parse (Path.build_filename (cache_dir, "movies.list.gz"), movie_parser);
111                         }
112                         percent_finished = 20;
113                         if (GENRES in flags) {
114                                 description_changed ("Parsing genre data ...");
115                                 parser.parse (Path.build_filename (cache_dir, "genres.list.gz"), genre_parser);
116                         }
117                         percent_finished = 40;
118                         if (RATINGS in flags) {
119                                 description_changed ("Parsing rating data ...");
120                                 parser.parse (Path.build_filename (cache_dir, "ratings.list.gz"), rating_parser);
121                         }
122                         percent_finished = 60;
123                         if (AKAS in flags) {
124                                 description_changed ("Parsing alternative titles ...");
125                                 parser.parse (Path.build_filename (cache_dir, "aka-titles.list.gz"), aka_parser);
126                         }
127                         percent_finished = 80;
128                         if (PLOTS in flags) {
129                                 description_changed ("Parsing plots ...");
130                                 parser.parse (Path.build_filename (cache_dir, "plot.list.gz"), plot_parser);
131                         }
132                 } catch (Error e2) {
133                         if (e2 is IOError.CANCELLED)
134                                 stdout.printf ("Download cancelled.\n");
135                         else
136                                 warning ("Failed to open/read stream: %s\n", e2.message);
137                 }
138
139                 description_changed ("Creating indices ...");
140                 if (AKAS in flags)
141                         sqlite.create_aka_index ();
142                 if (MOVIES in flags)
143                         sqlite.create_votes_index ();
144
145                 if (!cancellable.is_cancelled ()) {
146                         stdout.printf ("Download complete.\n");
147                         progress (100);
148                 }
149
150                 sqlite = null;
151                 running = false;
152
153                 timeout_quit ();
154
155                 return null;
156         }
157
158         private void on_progress (int dltotal, int dlnow) {
159                 stdout.printf ("%d / %d\r", dlnow, dltotal);
160                 if (dltotal > 0)
161                         progress (99*dlnow/dltotal/100);
162         }
163
164         private void timeout_quit () {
165                 source_id = Timeout.add (3000, quit);
166         }
167
168         private bool quit () {
169                 loop.quit ();
170
171                 // One-shot only
172                 return false;
173         }
174
175         public void run () {
176                 loop.run ();
177         }
178
179         public static void main () {
180                 Curl.global_init (Curl.GLOBAL_DEFAULT);
181
182                 try {
183                         var conn = DBus.Bus.get (DBus.BusType.SESSION);
184                         dynamic DBus.Object bus = conn.get_object ("org.freedesktop.DBus",
185                                                                    "/org/freedesktop/DBus",
186                                                                    "org.freedesktop.DBus");
187
188                         // Try to register service in session bus
189                         uint request_name_result = bus.request_name (DBUS_SERVICE, (uint) 0);
190
191                         if (request_name_result == DBus.RequestNameReply.PRIMARY_OWNER) {
192                                 // Start server
193                                 var server = new IMDbDownloadServer ();
194                                 conn.register_object (DBUS_OBJECT, server);
195
196                                 server.run ();
197                         } else {        
198                                 critical ("Service \"org.maemo.cinaest.IMDb\" already registered. Abort.\n");
199                         }
200                 } catch (Error e) {
201                         critical ("Oops: %s\n", e.message);
202                 }
203
204                 Curl.global_cleanup ();
205         }
206 }