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 {
24 public class GoogleMovie : Movie {
26 public string runtime;
28 public string showtimes;
31 public class GoogleParser : Object {
32 private MovieSource.ReceiveMovieFunction _get_callback;
38 public int next_tag_offset () {
40 while (current[++i] != '<' && current[i] != 0);
44 public void next_tag () {
47 current += next_tag_offset ();
50 public void finish_tag () {
51 while (current[0] != '>' && current[0] != 0)
53 if (current[0] == '>')
57 public weak string parse_tag (bool finish = true) throws Error {
61 while (current[++i].isalnum ());
63 throw new ParserError.EOF ("EOF in tag");
64 if (current[i] == '>')
67 tag = (string) (current + 1);
74 public void expect_tag (string tag) throws Error {
75 var found = parse_tag (true);
77 throw new ParserError.WRONG_TAG ("Wrong tag \"%s\", expected \"%s\"",
82 public string parse_text () {
83 string text = ((string) current).ndup (next_tag_offset ());
88 public void parse_attribute (string _attr, out string value) {
93 while (current[++i] != '=' && current[i] != '>' && current[i] != 0) {
96 attr = ((string) current).ndup (i);
102 while (!current[++i].isspace () && current[i] != '>' && current[i] != 0) {
103 if (current[i] == '"')
104 while (current[++i] != '"' && current[i] != 0);
107 if (current[0] == '"')
108 value = ((string) current).substring (1, i - 2);
110 value = ((string) current).ndup (i);
115 public void skip_whitespace () {
119 while (current[++i].isspace () && current[i] != 0);
123 public string? parse_tag_attribute (string tag, string attribute) throws Error {
124 var found = parse_tag (false);
126 throw new ParserError.WRONG_TAG ("Wrong tag \"%s\", expected \"%s\"",
130 string? value = null;
132 while (current[0] != '>' && current[0] != 0) {
133 parse_attribute (attribute, out value);
136 // Skip the closing '>' bracket
143 public void parse_movie () throws Error {
144 expect_tag ("a"); // rating
147 weak string rating_string = parse_tag_attribute ("img", "alt").offset (6); // "Rated " ->"0.0 out of 5.0"
148 double rating = rating_string.to_double ();
154 expect_tag ("/nobr");
155 expect_tag ("/nobr");
158 expect_tag ("/nobr");
160 expect_tag ("/font");
164 expect_tag ("a"); // <a href="/movies?near=city&mid=hexnumber"> --> link
166 var title = convert (parse_text ().replace ("'", "'"), -1, "utf-8", "iso-8859-1"); // FIXME
170 var runtime_and_fsk = parse_text ().replace (" ", " ").replace ("‎", "").split (" - ");
173 if (parse_tag () == "br") {
174 showtimes = parse_text ().replace (" ", ",");
175 expect_tag ("/font");
178 if (pattern == null) {
179 if (!title.has_prefix (_filter.title))
182 if (!pattern.match ((uint) title.length, title, null))
186 var movie = new GoogleMovie ();
188 movie.title = strip_tags (title);
190 movie.rating = (int) (rating * 10);
192 movie.cinema = cinema_name;
193 if (runtime_and_fsk.length >= 2) {
194 movie.runtime = runtime_and_fsk[0];
195 movie.fsk = runtime_and_fsk[1];
197 movie.showtimes = showtimes;
199 // TODO - could be configurable by settings
200 if (movie.runtime != null)
201 movie.secondary = "%s - %s - %s".printf (movie.runtime, cinema_name, showtimes);
203 movie.secondary = "%s - %s".printf (cinema_name, showtimes);
205 _get_callback (movie);
208 // FIXME - this is specific for Germany
209 private string strip_tags (string title) {
210 string tag_suffix = " (OmU)"; // original audio with subtitles
211 if (title.has_suffix (tag_suffix))
212 return title.substring (0, title.length - tag_suffix.length);
213 tag_suffix = " (OV)"; // original audio
214 if (title.has_suffix (tag_suffix))
215 return title.substring (0, title.length - tag_suffix.length);
219 public void parse_cinema () throws Error {
222 expect_tag ("a"); // --> link
224 string name = convert (parse_text ().replace ("&", "&"), -1, "utf-8", "iso-8859-1"); // FIXME
229 string address = parse_text ().replace (" ", " ");
230 expect_tag ("a"); // --> map
232 expect_tag ("/font");
237 // FIXME - store cinema address for movie detail window
240 public void parse (ref char[] buf) throws Error {
243 while (current[0] != 0) {
245 while (current[i++] != '>');
246 if (((string) current).has_prefix ("<a href=\"/movies/reviews?cid="))
248 else if (((string) current).has_prefix("<tr valign=top><td colspan=4><a href=\"/movies?near="))
256 public GoogleParser (MovieFilter filter, string? location, MovieSource.ReceiveMovieFunction callback) {
257 _get_callback = callback;
259 if (filter.title.chr(filter.title.length, '*') != null) {
260 pattern = new PatternSpec (filter.title);
265 // TODO - use google.de in Germany, also provides genres
266 string uri = "http://google.com/movies";
267 if (location != null && location != "")
268 uri += "?near=" + location;
269 File file = File.new_for_uri (uri);
270 InputStream stream = file.read (null);
272 char[] buf = new char[256*1024];
274 bool ok = stream.read_all (buf, buf.length, out nread, null);
279 stderr.printf ("Error: %s\n", e.message);