3 class IMDbDownloadServer : Object, IMDbDownloader {
5 Cancellable cancellable;
10 unowned IMDbSqlite sqlite;
12 "ftp.fu-berlin.de/pub/misc/movies/database/",
13 "ftp.funet.fi/pub/mirrors/ftp.imdb.com/pub/",
14 "ftp.sunet.se/pub/tv+movies/imdb/"
17 delegate void ParseLineFunction (string line);
20 loop = new MainLoop (null, false);
21 cancellable = new Cancellable ();
24 // IMDbDownloader implementation
26 public void download (string mirror, int flags) throws DBus.Error {
28 message ("Download in progress. Abort.\n");
33 Source.remove (source_id);
36 message ("Download started (%x).", flags);
38 download_imdb_async.begin ("ftp://anonymous@" + mirror, flags, Priority.DEFAULT_IDLE);
39 message ("Download finished.");
42 public void cancel () throws DBus.Error {
43 cancellable.cancel ();
46 public string[] get_mirrors () throws DBus.Error {
52 async void download_imdb_async (string mirror, int flags, int io_priority) {
54 File movies = File.new_for_uri (mirror + "/movies.list.gz");
55 File genres = File.new_for_uri (mirror + "/genres.list.gz");
56 File ratings = File.new_for_uri (mirror + "/ratings.list.gz");
58 description_changed ("Connecting to FTP ...");
62 m = yield movies.find_enclosing_mount_async(io_priority, cancellable);
65 bool mounted = yield movies.mount_enclosing_volume (MountMountFlags.NONE, null, cancellable);
67 m = yield movies.find_enclosing_mount_async(io_priority, cancellable);
74 critical ("Failed to mount: %s\n", e1.message);
80 stdout.printf ("Mounted: %s\n", m.get_name ());
82 description_changed ("Querying file sizes ...");
86 if (MOVIES in flags) {
87 info = yield movies.query_info_async ("", FileQueryInfoFlags.NONE, io_priority, cancellable);
88 total += info.get_size ();
90 if (GENRES in flags) {
91 info = yield genres.query_info_async ("", FileQueryInfoFlags.NONE, io_priority, cancellable);
92 total += info.get_size ();
94 if (RATINGS in flags) {
95 info = yield ratings.query_info_async ("", FileQueryInfoFlags.NONE, io_priority, cancellable);
96 total += info.get_size ();
99 warning ("Failed to get size: %s\n", e3.message);
103 var cache_dir = Path.build_filename (Environment.get_user_cache_dir (), "cinaest");
104 DirUtils.create_with_parents (cache_dir, 0770);
106 var _sqlite = new IMDbSqlite (Path.build_filename (cache_dir, "imdb.db"));
111 var movie_parser = new MovieLineParser (sqlite);
112 var genre_parser = new GenreLineParser (sqlite);
113 var rating_parser = new RatingLineParser (sqlite);
116 if (MOVIES in flags) {
117 description_changed ("Downloading movie list ...");
118 yield download_async(movies, movie_parser, io_priority);
119 description_changed ("Creating title index ...");
120 sqlite.create_title_index ();
122 if (GENRES in flags) {
123 description_changed ("Downloading genre data ...");
124 yield download_async(genres, genre_parser, io_priority);
126 if (RATINGS in flags) {
127 description_changed ("Downloading rating data ...");
128 yield download_async(ratings, rating_parser, io_priority);
131 if (e2 is IOError.CANCELLED)
132 message ("Download cancelled.\n");
134 warning ("Failed to open/read stream: %s\n", e2.message);
138 bool unmounted = yield m.unmount(MountUnmountFlags.NONE, null);
140 warning ("Failed to unmount.\n");
143 warning ("Failed to unmount: %s\n", e4.message);
146 description_changed ("Creating indices ...");
147 sqlite.create_votes_index ();
149 if (!cancellable.is_cancelled ()) {
150 stdout.printf ("Download complete.\n");
160 private void timeout_quit () {
161 source_id = Timeout.add (3000, quit);
164 private bool quit () {
171 async void download_async (File f, LineParser parser, int io_priority) throws Error {
173 unowned string line = null;
175 var stream = new GzipInputStream (yield f.read_async (io_priority, cancellable));
176 var data = new DataInputStream(stream);
181 line = yield data.read_line_async (io_priority, cancellable, out l);
183 parser.parse_line (line);
187 int p = (int) (100 * (sofar + stream.total_in ()) / total);
193 } while (line != null);
195 sofar += stream.total_in ();
197 yield stream.close_async (io_priority, cancellable);
204 public static void main () {
206 var conn = DBus.Bus.get (DBus.BusType.SESSION);
207 dynamic DBus.Object bus = conn.get_object ("org.freedesktop.DBus",
208 "/org/freedesktop/DBus",
209 "org.freedesktop.DBus");
211 // Try to register service in session bus
212 uint request_name_result = bus.request_name (DBUS_SERVICE, (uint) 0);
214 if (request_name_result == DBus.RequestNameReply.PRIMARY_OWNER) {
216 var server = new IMDbDownloadServer ();
217 conn.register_object (DBUS_OBJECT, server);
221 critical ("Service \"org.maemo.cinaest.IMDb\" already registered. Abort.\n");
224 critical ("Oops: %s\n", e.message);