Movie list window: correct calculation of movies per screen
[cinaest] / src / poster / google-image-search-parser.vala
1 /* This file is part of Cinaest.
2  *
3  * Copyright (C) 2009 Philipp Zabel
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 errordomain ParserError {
20         STRING_ERROR,
21         EOF
22 }
23
24 class GoogleImageSearchParser : Object {
25         char* current;
26
27         public void skip_string () throws Error {
28                 if (current[0] == 0) {
29                         throw new ParserError.EOF ("Buffer ends before end of string");
30                 }
31                 if (current[0] != '"') {
32                         throw new ParserError.STRING_ERROR ("String should start with '\"'");
33                 }
34                 current++;
35                 int i = -1;
36                 while (current[++i] != '"' && current[i] != 0);
37                 if (current[i] == 0) {
38                         throw new ParserError.EOF ("Buffer ends before end of string");
39                 }
40                 current += i + 1;
41                 if (current[0] == ',') {
42                         current++;
43                 }
44         }
45
46         public string? parse_string () throws Error {
47                 if (current[0] == 0) {
48                         throw new ParserError.EOF ("Buffer ends before end of string");
49                 }
50                 if (current[0] != '"') {
51                         throw new ParserError.STRING_ERROR ("String should start with '\"'");
52                 }
53                 current++;
54                 int i = -1;
55                 while (current[++i] != '"' && current[i] != 0);
56                 if (current[i] == 0) {
57                         throw new ParserError.EOF ("Buffer ends before end of string");
58                 }
59                 string s = ((string) current).ndup (i);
60                 current += i + 1;
61                 if (current[0] == ',') {
62                         current++;
63                 }
64                 return s;
65         }
66
67         private string? parse_result (bool thumbnail) throws Error {
68                 current++; // skip [
69                 if (current[0] == 0)
70                         return null;
71                 int i = -1;
72                 while (current[++i] != ']' && current[i] != 0);
73                 if (current[i] == 0) {
74                         throw new ParserError.EOF ("Buffer ends before end of result");
75                 }
76                 skip_string (); // "/imgres?imgurl\x3dhttp..."
77                 skip_string (); // ""
78                 var code = parse_string ();
79                 var uri = parse_string ();
80                 skip_string (); // small width
81                 skip_string (); // small height 
82                 skip_string (); // caption
83                 skip_string (); // ""
84                 skip_string (); // ""
85                 skip_string (); // "width x height - size"
86                 skip_string (); // "jpg"
87                 skip_string (); // "domain.com"
88                 skip_string (); // ""
89                 skip_string (); // ""
90                 var static_uri = parse_string (); // "http://t?.gstatic.com/images"
91                 skip_string (); // "1"
92
93                 if (thumbnail) {
94                         return static_uri + "?q=tbn:" + code + uri;
95                 } else {
96                         return uri;
97                 }
98         }
99
100         public GoogleImageSearchParser (string buf) {
101                 current = buf;
102         }
103
104         public string? run (bool thumbnail) throws Error {
105                 current = ((string) current).str ("dyn.setResults([[");
106                 if (current != null) {
107                         current += 16;
108                         return parse_result (thumbnail);
109                 }
110                 return null;
111         }
112 }