1 /* This file is part of Cinaest.
3 * Copyright (C) 2009 Philipp Zabel
5 * Cinaest is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * Cinaest is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with Cinaest. If not, see <http://www.gnu.org/licenses/>.
19 errordomain ParserError {
26 public string address;
29 public Cinema (string _name) {
34 public class GoogleMovie : Movie {
36 public string runtime;
38 public string showtimes;
41 public class GoogleParser : Object {
42 private MovieSource.ReceiveMovieFunction _get_callback;
45 public string location;
49 public int next_tag_offset () {
51 while (current[++i] != '<' && current[i] != 0);
55 public void next_tag () {
58 current += next_tag_offset ();
61 public void finish_tag () {
62 while (current[0] != '>' && current[0] != 0)
64 if (current[0] == '>')
68 public weak string parse_tag (bool finish = true) throws Error {
72 while (current[++i].isalnum ());
74 throw new ParserError.EOF ("EOF in tag");
75 if (current[i] == '>')
78 tag = (string) (current + 1);
85 public void expect_tag (string tag) throws Error {
86 var found = parse_tag (true);
88 throw new ParserError.WRONG_TAG ("Wrong tag \"%s\", expected \"%s\"",
93 public string parse_text () {
94 string text = ((string) current).ndup (next_tag_offset ());
99 public void parse_attribute (string _attr, out string value) {
104 while (current[++i] != '=' && current[i] != '>' && current[i] != 0) {
107 attr = ((string) current).ndup (i);
113 while (!current[++i].isspace () && current[i] != '>' && current[i] != 0) {
114 if (current[i] == '"')
115 while (current[++i] != '"' && current[i] != 0);
118 if (current[0] == '"')
119 value = ((string) current).substring (1, i - 2);
121 value = ((string) current).ndup (i);
126 public void skip_whitespace () {
130 while (current[++i].isspace () && current[i] != 0);
134 public string? parse_tag_attribute (string tag, string attribute) throws Error {
135 var found = parse_tag (false);
137 throw new ParserError.WRONG_TAG ("Wrong tag \"%s\", expected \"%s\"",
141 string? value = null;
143 while (current[0] != '>' && current[0] != 0) {
144 parse_attribute (attribute, out value);
147 // Skip the closing '>' bracket
154 public string unescape_unicode (string s) {
159 for (i = 0; i < l; i++) {
160 if (s[i] == '&' && s[i + 1] == '#') {
161 for (j = i + 2; j < l; j++) {
162 if (!s[j].isdigit ())
168 int codepoint = s.substring (i + 2, j - i - 2).to_int ();
169 char[] buf = new char[6];
170 ((unichar) codepoint).to_utf8 ((string) buf);
171 result += (string) buf;
176 if (s.offset (i).has_prefix ("&")) {
181 result += s.substring (i, 1);
187 public void parse_movie () throws Error {
188 expect_tag ("div"); // class=movie
189 expect_tag ("div"); // class=name
190 expect_tag ("a"); // href="/movies?near=city&mid=..."
191 expect_tag ("span"); // dir=ltr
192 var title = unescape_unicode (convert (parse_text (), -1, "utf-8", "iso-8859-1")); // FIXME
193 expect_tag ("/span");
196 expect_tag ("span"); // class=info
197 string[] runtime_and_fsk = {};
199 if (parse_tag () == "nobr") {
201 string rating_string = parse_tag_attribute ("img", "alt").offset (6); // "Rated " ->"0.0 out of 5.0"
202 rating = rating_string.to_double ();
207 expect_tag ("/nobr");
208 expect_tag ("/nobr");
209 runtime_and_fsk = parse_text ().replace ("‎", "").offset (3).split (" - ");
210 expect_tag ("/span");
212 expect_tag ("div"); // class=times
213 var showtimes = parse_text ().replace (" ", ",");
217 if (pattern == null) {
218 if (!title.has_prefix (_filter.title))
221 if (!pattern.match ((uint) title.length, title, null))
225 var movie = new GoogleMovie ();
227 movie.title = strip_tags (title);
229 movie.rating = (int) (rating * 10);
231 movie.cinema = last_cinema;
232 if (runtime_and_fsk.length >= 2) {
233 movie.runtime = runtime_and_fsk[0];
234 movie.fsk = runtime_and_fsk[1];
236 movie.showtimes = showtimes;
238 // TODO - could be configurable by settings
239 if (movie.runtime != null)
240 movie.secondary = "%s - %s - %s".printf (movie.runtime, last_cinema.name, showtimes);
242 movie.secondary = "%s - %s".printf (last_cinema.name, showtimes);
244 _get_callback (movie);
247 // FIXME - this is specific for Germany
248 private string strip_tags (string title) {
249 string tag_suffix = " (OmU)"; // original audio with subtitles
250 if (title.has_suffix (tag_suffix))
251 return title.substring (0, title.length - tag_suffix.length);
252 tag_suffix = " (OV)"; // original audio
253 if (title.has_suffix (tag_suffix))
254 return title.substring (0, title.length - tag_suffix.length);
258 public void parse_cinema () throws Error {
259 expect_tag ("div"); // class=theater
260 expect_tag ("div"); // class=desc id=theater_...
261 expect_tag ("h2"); // class=name
262 expect_tag ("a"); // href="/movies?near=city&tid=..."
263 expect_tag ("span"); // dir=ltr
264 var name = unescape_unicode (convert (parse_text (), -1, "utf-8", "iso-8859-1")); // FIXME
265 expect_tag ("/span");
268 expect_tag ("div"); // class=info
269 var address_and_phone = parse_text ().replace (" ", " ").split (" - ");
270 string address = null;
272 if (address_and_phone.length >= 2) {
273 address = address_and_phone[0];
274 phone = address_and_phone[1].replace (" ", "").replace ("-", "");
276 expect_tag ("a"); // target=_top
281 last_cinema = new Cinema (name);
282 last_cinema.address = address;
283 last_cinema.phone = phone;
286 public void parse (ref char[] buf) throws Error {
289 while (location == null && current[0] != 0) {
291 while (current[i++] != '>');
292 if (((string) current).has_prefix ("<a href=\"/movies?near=")) {
293 string href = parse_tag_attribute ("a", "href");
294 char* p = (char*) href.offset (13); // skip "/movies?near="
297 while (p[++j] != '&' && p[j] != 0);
298 p[0] = p[0].toupper ();
299 location = ((string) p).ndup (j);
304 while (current[0] != 0) {
306 while (current[i++] != '>');
307 if (((string) current).has_prefix ("<div class=movie>")) {
309 } else if (((string) current).has_prefix("<div class=theater>")) {
318 public async void query (MovieFilter filter, string? location, MovieSource.ReceiveMovieFunction callback, Cancellable? cancellable) {
319 _get_callback = callback;
321 if (filter.title.chr(filter.title.length, '*') != null) {
322 pattern = new PatternSpec (filter.title);
327 // TODO - use google.de in Germany, also provides genres
328 string uri = "http://google.com/movies";
329 if (location != null && location != "")
330 uri += "?near=" + location;
332 stdout.printf ("GET: %s\n", uri);
334 File file = File.new_for_uri (uri);
335 InputStream stream = yield file.read_async (Priority.DEFAULT_IDLE, null);
337 char[] buf = new char[256*1024];
340 while (total < 256*1024) {
341 nread = yield stream.read_async ((char *)buf + total, 256*1024 - total, Priority.DEFAULT_IDLE, cancellable);
343 if (cancellable.is_cancelled ())
351 stderr.printf ("Error: %s\n", e.message);