821c9a2affa8c2d71c598832a546974de1bcfe38
[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
76                         var downloader = new IMDbFtpDownloader (cancellable);
77                         downloader.progress_changed.connect (on_progress_changed);
78
79                         if (MOVIES in flags) {
80                                 description_changed ("Downloading movie list ...");
81                                 downloader.download (url + "movies.list.gz", movie_parser);
82                         }
83                         percent_finished = 33;
84                         if (GENRES in flags) {
85                                 description_changed ("Downloading genre data ...");
86                                 downloader.download (url + "genres.list.gz", genre_parser);
87                         }
88                         percent_finished = 66;
89                         if (RATINGS in flags) {
90                                 description_changed ("Downloading rating data ...");
91                                 downloader.download (url + "ratings.list.gz", rating_parser);
92                         }
93                 } catch (Error e2) {
94                         if (e2 is IOError.CANCELLED)
95                                 stdout.printf ("Download cancelled.\n");
96                         else
97                                 warning ("Failed to open/read stream: %s\n", e2.message);
98                 }
99
100                 description_changed ("Creating indices ...");
101                 if (MOVIES in flags)
102                         sqlite.create_votes_index ();
103
104                 if (!cancellable.is_cancelled ()) {
105                         stdout.printf ("Download complete.\n");
106                         progress (100);
107                 }
108
109                 sqlite = null;
110                 running = false;
111
112                 timeout_quit ();
113
114                 return null;
115         }
116
117         private void on_progress_changed (int percent) {
118                 progress (percent_finished + percent / 3);
119         }
120
121         private void timeout_quit () {
122                 source_id = Timeout.add (3000, quit);
123         }
124
125         private bool quit () {
126                 loop.quit ();
127
128                 // One-shot only
129                 return false;
130         }
131
132         public void run () {
133                 loop.run ();
134         }
135
136         public static void main () {
137                 Curl.global_init (Curl.GLOBAL_DEFAULT);
138
139                 try {
140                         var conn = DBus.Bus.get (DBus.BusType.SESSION);
141                         dynamic DBus.Object bus = conn.get_object ("org.freedesktop.DBus",
142                                                                    "/org/freedesktop/DBus",
143                                                                    "org.freedesktop.DBus");
144
145                         // Try to register service in session bus
146                         uint request_name_result = bus.request_name (DBUS_SERVICE, (uint) 0);
147
148                         if (request_name_result == DBus.RequestNameReply.PRIMARY_OWNER) {
149                                 // Start server
150                                 var server = new IMDbDownloadServer ();
151                                 conn.register_object (DBUS_OBJECT, server);
152
153                                 server.run ();
154                         } else {        
155                                 critical ("Service \"org.maemo.cinaest.IMDb\" already registered. Abort.\n");
156                         }
157                 } catch (Error e) {
158                         critical ("Oops: %s\n", e.message);
159                 }
160
161                 Curl.global_cleanup ();
162         }
163 }