1 #include <QtNetwork/QNetworkAccessManager>
2 #include <QtNetwork/QNetworkRequest>
9 #include <QApplication>
11 #include "backendkicker.h"
13 const int BackendKicker::INTERVAL_FAST = 180;
15 BackendKicker::BackendKicker(QObject *parent) :
16 MatchDayBackend(parent),
18 m_netaccmgr(new QNetworkAccessManager(this))
20 QSettings settings(qApp->organizationName(), qApp->applicationName());
22 this->selectLeague(settings.value("League", "1. Bundesliga").toString());
24 connect(m_netaccmgr, SIGNAL(finished(QNetworkReply*)),
25 this, SLOT(dlndFinished(QNetworkReply*)));
27 connect(&m_updateTimer, SIGNAL(timeout()),
28 this, SLOT(update()));
30 m_updateTimer.setSingleShot(true);
35 Match* BackendKicker::getMatch(QString hometeam, QString awayteam, QDateTime date)
37 QListIterator<Match*> iter(m_matchlist);
40 while (iter.hasNext()) {
42 if (match->awayTeam() == awayteam &&
43 match->homeTeam() == hometeam) {
48 match = new Match(hometeam, awayteam, date, this);
49 m_matchlist.append(match);
50 emit matchAdded(match);
55 QList<Match*> BackendKicker::matchList()
60 static QDateTime parseDate(QString datehtml)
62 static QDateTime lastParsedDate;
66 int month, day, hour, minute;
68 //qDebug() << "parseDate in: " << datehtml;
70 tokens = datehtml.split(QRegExp("[>.&;:<\"]"), QString::SkipEmptyParts);
71 date = QDate::currentDate();
74 if (tokens.count() < 6) {
75 return lastParsedDate;
78 month = (tokens.at(2)).toInt();
79 day = (tokens.at(1)).toInt();
80 hour = (tokens.at(4)).toInt();
81 minute = (tokens.at(5)).toInt();
83 lastParsedDate = QDateTime(QDate(date.year(), month, day),
86 return lastParsedDate;
89 static QString parseTeam(QString teamhtml)
93 //qDebug() << "parseTeam in: " << teamhtml;
95 teamhtml.truncate(teamhtml.indexOf("</a>"));
96 team = teamhtml.mid(teamhtml.lastIndexOf(">") + 1);
98 //qDebug() << "parseTeam out: " << team;
102 static void parseScore(Match* match, QString scorehtml)
106 //qDebug() << "parseScore in: " << scorehtml;
107 tokens = scorehtml.split(QRegExp("[>&();:<]"), QString::SkipEmptyParts);
108 //qDebug() << tokens;
110 if (tokens.count() == 7) {
111 // no extra color tag -> either not started, halftime or finished
112 if (tokens.at(4) == "-") {
113 // no first half results -> match not started yet
114 match->setState(Match::NotStarted);
115 } else if (tokens.at(1) == "-") {
116 // second half has not been started but there are first
117 // half results -> currently half time
118 match->setScore(tokens.at(4).toInt(), tokens.at(5).toInt());
119 match->setState(Match::HalfTime);
121 // no color tag and no "-" -> game is finished
122 match->setScore(tokens.at(1).toInt(), tokens.at(2).toInt());
123 match->setState(Match::Finished);
126 // there is a color tag which means that either first
127 // half or second half are currently running
128 if (tokens.at(4).contains("color")) {
129 // first half score marked red -> first half running
130 match->setScore(tokens.at(5).toInt(), tokens.at(6).toInt());
131 match->setState(Match::FirstHalf);
132 } else if (tokens.at(1).contains("color")) {
133 // second half score marked res -> second half running
134 match->setState(Match::SecondHalf);
135 match->setScore(tokens.at(2).toInt(), tokens.at(3).toInt());
139 qDebug() << "match state: " << match->state();
140 qDebug() << "match home: " << match->homeScore();
141 qDebug() << "match away: " << match->awayScore();
144 void BackendKicker::parsePage (QString htmlstr)
146 QStringList rawmatches;
147 QString hometeam, awayteam, tmp;
155 //qDebug() << "parsePage in: " << htmlstr;
157 rx.setPattern("<td class=\"first\">(.*)<td class=\"aligncenter last\">");
159 while ((pos = rx.indexIn(htmlstr, pos)) != -1) {
161 rawmatches.append(htmlstr.mid(pos, rx.matchedLength()));
162 //qDebug() << "MATCH " << count << ":" << htmlstr.mid(pos, rx.matchedLength()) << "\n\n";
163 pos += rx.matchedLength();
166 rx.setPattern("<td.*>(.*)</td>");
168 QStringList::iterator i;
169 for (i = rawmatches.begin(); i != rawmatches.end(); ++i) {
172 while ((pos = rx.indexIn(*i, pos)) != -1) {
174 tmp = (*i).mid(pos, rx.matchedLength());
175 pos += rx.matchedLength();
178 date = parseDate(tmp);
181 hometeam = parseTeam(tmp);
184 awayteam = parseTeam(tmp);
185 match = getMatch(hometeam, awayteam, date);
188 parseScore(match, tmp);
197 bool BackendKicker::selectLeague(QString league)
199 bool leagueIsSupported = true;
201 if (league == "1. Bundesliga") {
202 m_URL = "http://www.kicker.de/news/fussball/bundesliga/spieltag/1-bundesliga/2010-11/spieltag.html";
203 } else if (league == "2. Bundesliga") {
204 m_URL = "http://www.kicker.de/news/fussball/bundesliga/spieltag/2-bundesliga/2010-11/spieltag.html";
205 } else if (league == "tipp3 Bundesliga") {
206 m_URL = "http://www.kicker.de/news/fussball/intligen/oesterreich/tipp3-bundesliga/2010-11/spieltag.html";
208 leagueIsSupported = false;
212 this->m_matchlist.clear();
215 return leagueIsSupported;
218 void BackendKicker::update()
220 emit updateStarted();
222 qDebug() << "Start Update with URL: " << m_URL;
223 m_netaccmgr->get(QNetworkRequest(QUrl(m_URL)));
226 void BackendKicker::dlndFinished(QNetworkReply *reply)
229 int secstonextupdate;
231 if (reply->error() != QNetworkReply::NoError) {
232 // TODO proper user friendly error handling here!
233 qWarning() << "Download failed with error: " << reply->error();
236 rawdata = reply->readAll();
239 emit updateFinished();
241 secstonextupdate = secsToNextGame();
242 if (secstonextupdate < INTERVAL_FAST) {
243 secstonextupdate = INTERVAL_FAST;
244 } else if (secstonextupdate > 6 * 3600) {
245 secstonextupdate = 6 * 3600;
247 m_updateTimer.start(secstonextupdate * 1000);
248 qDebug() << "Update finished, next update in: " << secstonextupdate << "seconds.";
251 int BackendKicker::secsToNextGame()
253 QListIterator<Match*> iter(m_matchlist);
256 int secstonextgame = -1;
258 while (iter.hasNext()) {
260 if (match->state() == Match::FirstHalf ||
261 match->state() == Match::SecondHalf ||
262 match->state() == Match::HalfTime) {
265 return secstonextgame;
267 } else if (match->state() == Match::NotStarted) {
268 secstogame = QDateTime::currentDateTime().secsTo(match->date());
269 if (secstonextgame == -1) {
270 secstonextgame = secstogame;
271 } else if (secstogame < secstonextgame) {
272 secstonextgame = secstogame;
277 return secstonextgame;