1 // Copyright 2010 Jochen Becher
3 // This file is part of MovieSchedule.
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.
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.
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/>.
18 #include "theatersearchclient.h"
20 #include "data/cinemakey.h"
21 #include "data/cinema.h"
22 #include "data/cinemaschedule.h"
23 #include "utils/assertedlocker.h"
25 #include <QXmlStreamReader>
28 TheaterSearchClient::TheaterSearchClient(CinemaSchedule *cinema_schedule, QObject *parent)
29 : AbstractSearchClient(parent),
30 _cinema_schedule(cinema_schedule)
34 void TheaterSearchClient::SearchTheater(const QString &town)
36 _semaphore.Activate(GetSearchTaskId());
37 setObjectName(QString("TheaterSearchClient:%1").arg(town));
42 void TheaterSearchClient::CancelAllRunningSearchs()
44 _semaphore.CancelAll();
47 void TheaterSearchClient::Search(int start)
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);
64 void TheaterSearchClient::ReplyFinished(QNetworkReply *reply)
66 QXmlStreamReader xml(reply);
67 State state = PARSE_HTML;
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")) {
84 theater_movies_url = attr_href;
86 state = PARSE_THEATER_LINK;
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;
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())) {
117 CinemaKey key(theater_name, theater_address);
118 Cinema *cinema = _cinema_schedule->FindCinema(key);
120 cinema = _cinema_schedule->AddCinema(key);
122 if (!theater_movies_url.isEmpty()) {
123 cinema->SetMoviesUrl(theater_movies_url);
125 if (!theater_phone.isEmpty()) {
126 cinema->SetTelephone(theater_phone);
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();
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());
146 } else if (!_semaphore.IsActive(GetSearchTaskId())) {
147 emit Cancelled(GetSearchTaskId());
148 emit SearchFinished(GetSearchTaskId(), false);
152 emit Reply(GetSearchTaskId(), true);
153 Search(GetStartIndex() + found);
155 emit Reply(GetSearchTaskId(), false);
156 emit SearchFinished(GetSearchTaskId(), true);
160 reply->deleteLater();
163 SearchClientSemaphore TheaterSearchClient::_semaphore;