3 class IMDbDownloadServer : Object, IMDbDownloader {
5 Cancellable cancellable;
8 unowned IMDbSqlite sqlite;
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/"
18 delegate void ParseLineFunction (string line);
21 loop = new MainLoop (null, false);
22 cancellable = new Cancellable ();
25 // IMDbDownloader implementation
27 public void download (string mirror, int _flags) throws DBus.Error {
29 stdout.printf ("Download in progress. Abort.\n");
34 Source.remove (source_id);
37 stdout.printf ("Download started (%x).", flags);
39 url = "ftp://anonymous@" + mirror;
42 Thread.create(download_thread, false);
43 } catch (ThreadError e) {
44 critical ("Failed to create download thread\n");
49 public void cancel () throws DBus.Error {
50 cancellable.cancel ();
53 public string[] get_mirrors () throws DBus.Error {
59 private void* download_thread () {
60 description_changed ("Connecting to FTP ...");
64 var cache_dir = Path.build_filename (Environment.get_user_cache_dir (), "cinaest");
65 DirUtils.create_with_parents (cache_dir, 0770);
67 var _sqlite = new IMDbSqlite (Path.build_filename (cache_dir, "imdb.db"));
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);
78 var downloader = new FtpDownloader (cancellable);
79 downloader.progress.connect (on_progress);
81 var parser = new IMDbGzipParser (cancellable);
82 parser.progress.connect (on_progress);
84 if (MOVIES in flags) {
85 description_changed ("Downloading movie list ...");
86 downloader.download (url + "movies.list.gz", Path.build_filename (cache_dir, "movies.list.gz"));
88 percent_finished = 10;
89 if (GENRES in flags) {
90 description_changed ("Downloading genre data ...");
91 downloader.download (url + "genres.list.gz", Path.build_filename (cache_dir, "genres.list.gz"));
93 percent_finished = 20;
94 if (RATINGS in flags) {
95 description_changed ("Downloading rating data ...");
96 downloader.download (url + "ratings.list.gz", Path.build_filename (cache_dir, "ratings.list.gz"));
98 percent_finished = 30;
100 description_changed ("Downloading alternative titles ...");
101 downloader.download (url + "aka-titles.list.gz", Path.build_filename (cache_dir, "aka-titles.list.gz"));
103 percent_finished = 40;
104 if (PLOTS in flags) {
105 description_changed ("Downloading plots ...");
106 downloader.download (url + "plot.list.gz", Path.build_filename (cache_dir, "plot.list.gz"));
109 percent_finished = 50;
110 if (MOVIES in flags) {
111 description_changed ("Parsing movie list ...");
112 parser.parse (Path.build_filename (cache_dir, "movies.list.gz"), movie_parser);
114 percent_finished = 60;
115 if (GENRES in flags) {
116 description_changed ("Parsing genre data ...");
117 parser.parse (Path.build_filename (cache_dir, "genres.list.gz"), genre_parser);
119 percent_finished = 70;
120 if (RATINGS in flags) {
121 description_changed ("Parsing rating data ...");
122 parser.parse (Path.build_filename (cache_dir, "ratings.list.gz"), rating_parser);
124 percent_finished = 80;
126 description_changed ("Parsing alternative titles ...");
127 parser.parse (Path.build_filename (cache_dir, "aka-titles.list.gz"), aka_parser);
129 percent_finished = 90;
130 if (PLOTS in flags) {
131 description_changed ("Parsing plots ...");
132 parser.parse (Path.build_filename (cache_dir, "plot.list.gz"), plot_parser);
135 if (e2 is IOError.CANCELLED)
136 stdout.printf ("Download cancelled.\n");
138 warning ("Failed to open/read stream: %s\n", e2.message);
141 description_changed ("Creating indices ...");
143 sqlite.create_aka_index ();
145 sqlite.create_votes_index ();
147 if (!cancellable.is_cancelled ()) {
148 stdout.printf ("Download complete.\n");
160 private void on_progress (int dltotal, int dlnow) {
161 stdout.printf ("%d / %d\r", dlnow, dltotal);
163 int p = percent_finished + 10*dlnow/dltotal;
169 private void timeout_quit () {
170 source_id = Timeout.add (3000, quit);
173 private bool quit () {
184 public static void main () {
185 Curl.global_init (Curl.GLOBAL_DEFAULT);
188 var conn = DBus.Bus.get (DBus.BusType.SESSION);
189 dynamic DBus.Object bus = conn.get_object ("org.freedesktop.DBus",
190 "/org/freedesktop/DBus",
191 "org.freedesktop.DBus");
193 // Try to register service in session bus
194 uint request_name_result = bus.request_name (DBUS_SERVICE, (uint) 0);
196 if (request_name_result == DBus.RequestNameReply.PRIMARY_OWNER) {
198 var server = new IMDbDownloadServer ();
199 conn.register_object (DBUS_OBJECT, server);
203 critical ("Service \"org.maemo.cinaest.IMDb\" already registered. Abort.\n");
206 critical ("Oops: %s\n", e.message);
209 Curl.global_cleanup ();