f4f02dc2270c359208ac38b9241379ea2e4750c6
[movie-schedule] / src / searchclients / theatersearchclient.cpp
1 // Copyright 2010 Jochen Becher
2 //
3 // This file is part of MovieSchedule.
4 //
5 // MovieSchedule 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 // MovieSchedule 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 MovieSchedule.  If not, see <http://www.gnu.org/licenses/>.
17
18 #include "theatersearchclient.h"
19
20 #include "data/cinemakey.h"
21 #include "data/cinema.h"
22 #include "data/cinemaschedule.h"
23 #include "utils/assertedlocker.h"
24
25 #include <QXmlStreamReader>
26 #include <iostream>
27
28 TheaterSearchClient::TheaterSearchClient(CinemaSchedule *cinema_schedule, QObject *parent)
29     : AbstractSearchClient(parent),
30     _cinema_schedule(cinema_schedule)
31 {
32 }
33
34 void TheaterSearchClient::SearchTheater(const QString &town)
35 {
36     _semaphore.Activate(GetSearchTaskId());
37     setObjectName(QString("TheaterSearchClient:%1").arg(town));
38     _town = town;
39     Search(0);
40 }
41
42 void TheaterSearchClient::CancelAllRunningSearchs()
43 {
44     _semaphore.CancelAll();
45 }
46
47 void TheaterSearchClient::Search(int start)
48 {
49     QUrl url("http://www.google.com/m/movies");
50     url.addQueryItem("loc", _town);
51     url.addQueryItem("sort", QString::number(0));
52     AbstractSearchClient::Search(url, start);
53 }
54
55 enum State {
56     PARSE_HTML,
57     PARSE_THEATER_LINK,
58     PARSE_THEATER_DIV,
59     PARSE_THEATER_BR,
60     PARSE_THEATER_SPAN,
61     PARSE_THEATER_PHONE
62 };
63
64 void TheaterSearchClient::ReplyFinished(QNetworkReply *reply)
65 {
66     QXmlStreamReader xml(reply);
67     State state = PARSE_HTML;
68     int found = 0;
69     QString theater_name;
70     QString theater_address;
71     QString theater_movies_url;
72     QString theater_phone;
73     while (!xml.atEnd()) {
74         QXmlStreamReader::TokenType token = xml.readNext();
75         if (token == QXmlStreamReader::StartElement) {
76             QString attr_href = xml.attributes().value("href").toString();
77             //std::cout << "name: " << qPrintable(xml.name().toString()) << ", href: " << qPrintable(attr_href) << std::endl;
78             if (state == PARSE_HTML && xml.name() == "a" && attr_href.startsWith("/m/movies")) {
79                 QUrl url = QUrl::fromEncoded(QString("http://www.google.com" + attr_href).toAscii(), QUrl::TolerantMode);
80                 //std::cout << "LINK " << qPrintable(attr_href) << std::endl;
81                 if (url.hasQueryItem("tid")) {
82                     theater_name = "";
83                     theater_address = "";
84                     theater_movies_url = attr_href;
85                     theater_phone = "";
86                     state = PARSE_THEATER_LINK;
87                 } else {
88                     state = PARSE_HTML;
89                 }
90             } else if (state == PARSE_THEATER_DIV && xml.name() == "br") {
91                 state = PARSE_THEATER_BR;
92             } else if (state == PARSE_THEATER_DIV && xml.name() == "span") {
93                 state = PARSE_THEATER_SPAN;
94             } else if (state == PARSE_THEATER_DIV && xml.name() == "a" && attr_href.startsWith("wtai:")) {
95                 state = PARSE_THEATER_PHONE;
96             } else if (state == PARSE_THEATER_DIV && xml.name() == "a") {
97                 state = PARSE_THEATER_BR;
98             } else {
99                 state = PARSE_HTML;
100             }
101         } else if (token == QXmlStreamReader::EndElement) {
102             if (state == PARSE_THEATER_LINK) {
103                 state = PARSE_THEATER_DIV;
104             } else if (state == PARSE_THEATER_BR) {
105                 state = PARSE_THEATER_DIV;
106             } else if (state == PARSE_THEATER_SPAN) {
107                 state = PARSE_THEATER_DIV;
108             } else if (state == PARSE_THEATER_PHONE) {
109                 state = PARSE_THEATER_DIV;
110             } else if (state == PARSE_THEATER_DIV) {
111                 if (!theater_name.isEmpty()) {
112                     AssertedWriteLocker locker(_cinema_schedule->GetLock());
113                     if (!_semaphore.IsActive(GetSearchTaskId())) {
114                         break;
115                     }
116                     ++found;
117                     CinemaKey key(theater_name, theater_address);
118                     Cinema *cinema = _cinema_schedule->FindCinema(key);
119                     if (cinema == 0) {
120                         cinema = _cinema_schedule->AddCinema(key);
121                     }
122                     if (!theater_movies_url.isEmpty()) {
123                         cinema->SetMoviesUrl(theater_movies_url);
124                     }
125                     if (!theater_phone.isEmpty()) {
126                         cinema->SetTelephone(theater_phone);
127                     }
128                 }
129                 state = PARSE_HTML;
130             }
131         } else if (token == QXmlStreamReader::Characters) {
132             if (state == PARSE_THEATER_LINK) {
133                 theater_name = xml.text().toString();
134             } else if (state == PARSE_THEATER_PHONE) {
135                 theater_phone = xml.text().toString();
136             } else if (state == PARSE_THEATER_SPAN) {
137                 theater_address = xml.text().toString();
138             }
139         }
140     }
141     if (xml.hasError()) {
142         emit SearchFinished(GetSearchTaskId(), false);
143         std::cout << "xml error (" << xml.lineNumber() << "/" << xml.columnNumber() << "): " << qPrintable(xml.errorString()) << std::endl;
144         emit Error(GetSearchTaskId());
145         deleteLater();
146     } else if (!_semaphore.IsActive(GetSearchTaskId())) {
147         emit Cancelled(GetSearchTaskId());
148         emit SearchFinished(GetSearchTaskId(), false);
149         deleteLater();
150     } else {
151         if (found > 0) {
152             emit Reply(GetSearchTaskId(), true);
153             Search(GetStartIndex() + found);
154         } else {
155             emit Reply(GetSearchTaskId(), false);
156             emit SearchFinished(GetSearchTaskId(), true);
157             deleteLater();
158         }
159     }
160     reply->deleteLater();
161 }
162
163 SearchClientSemaphore TheaterSearchClient::_semaphore;