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 class GoogleImageSearchParser : Object {
27 public void skip_string () throws Error {
28 if (current[0] == 0) {
29 throw new ParserError.EOF ("Buffer ends before end of string");
31 if (current[0] != '"') {
32 throw new ParserError.STRING_ERROR ("String should start with '\"'");
36 while (current[++i] != '"' && current[i] != 0);
37 if (current[i] == 0) {
38 throw new ParserError.EOF ("Buffer ends before end of string");
41 if (current[0] == ',') {
46 public string? parse_string () throws Error {
47 if (current[0] == 0) {
48 throw new ParserError.EOF ("Buffer ends before end of string");
50 if (current[0] != '"') {
51 throw new ParserError.STRING_ERROR ("String should start with '\"'");
55 while (current[++i] != '"' && current[i] != 0);
56 if (current[i] == 0) {
57 throw new ParserError.EOF ("Buffer ends before end of string");
59 string s = ((string) current).ndup (i);
61 if (current[0] == ',') {
67 private string? parse_result (bool thumbnail) throws Error {
72 while (current[++i] != ']' && current[i] != 0);
73 if (current[i] == 0) {
74 throw new ParserError.EOF ("Buffer ends before end of result");
76 skip_string (); // "/imgres?imgurl\x3dhttp..."
78 var code = parse_string ();
79 var uri = parse_string ();
80 skip_string (); // small width
81 skip_string (); // small height
82 skip_string (); // caption
85 skip_string (); // "width x height - size"
86 skip_string (); // "jpg"
87 skip_string (); // "domain.com"
90 var static_uri = parse_string (); // "http://t?.gstatic.com/images"
91 skip_string (); // "1"
94 return static_uri + "?q=tbn:" + code + uri;
100 public GoogleImageSearchParser (string buf) {
104 public string? run (bool thumbnail) throws Error {
105 current = ((string) current).str ("dyn.setResults([[");
106 if (current != null) {
108 return parse_result (thumbnail);