1 #include <QtNetwork/QNetworkAccessManager>
2 #include <QtNetwork/QNetworkRequest>
10 #include "backendkicker.h"
12 BackendKicker::BackendKicker(QObject *parent) :
15 QSettings settings("David Solbach", "BuliScores");
16 this->setLeague(settings.value("League", "1. Bundesliga").toString());
21 Match* BackendKicker::getMatch(QString hometeam, QString awayteam, QDateTime date)
23 QListIterator<Match*> iter(m_matchlist);
26 while (iter.hasNext()) {
28 if (match->awayTeam() == awayteam &&
29 match->homeTeam() == hometeam) {
34 match = new Match(hometeam, awayteam, date, this);
35 m_matchlist.append(match);
37 emit matchListChanged();
42 QList<Match*> BackendKicker::matchList()
47 static QDateTime parseDate(QString datehtml)
49 static QDateTime lastParsedDate;
53 int month, day, hour, minute;
55 //qDebug() << "parseDate in: " << datehtml;
57 tokens = datehtml.split(QRegExp("[>.&;:<\"]"), QString::SkipEmptyParts);
58 date = QDate::currentDate();
61 if (tokens.count() < 6) {
62 return lastParsedDate;
65 month = (tokens.at(2)).toInt();
66 day = (tokens.at(1)).toInt();
67 hour = (tokens.at(4)).toInt();
68 minute = (tokens.at(5)).toInt();
70 lastParsedDate = QDateTime(QDate(date.year(), month, day),
73 return lastParsedDate;
76 static QString parseTeam(QString teamhtml)
80 //qDebug() << "parseTeam in: " << teamhtml;
82 teamhtml.truncate(teamhtml.indexOf("</a>"));
83 team = teamhtml.mid(teamhtml.lastIndexOf(">") + 1);
85 qDebug() << "parseTeam out: " << team;
89 static int parseScore(Match* match, QString scorehtml)
94 qDebug() << "parseScore in: " << scorehtml;
95 tokens = scorehtml.split(QRegExp("[>&();:<]"), QString::SkipEmptyParts);
98 if (tokens.count() == 7) {
99 // no extra color tag -> either not started, halftime or finished
100 if (tokens.at(4) == "-") {
101 // no first half results -> match not started yet
102 match->setState(Match::NotStarted);
103 } else if (tokens.at(1) == "-") {
104 // second half has not been started but there are first
105 // half results -> currently half time
106 match->setScore(tokens.at(4).toInt(), tokens.at(5).toInt());
107 match->setState(Match::HalfTime);
109 // no color tag and no "-" -> game is finished
110 match->setScore(tokens.at(1).toInt(), tokens.at(3).toInt());
111 match->setState(Match::Finished);
114 // there is a color tag which means that either first
115 // half or second half are currently running
116 if (tokens.at(4).contains("color")) {
117 // first half score marked red -> first half running
118 match->setScore(tokens.at(5).toInt(), tokens.at(6).toInt());
119 match->setState(Match::FirstHalf);
120 } else if (tokens.at(1).contains("color")) {
121 // second half score marked res -> second half running
122 match->setState(Match::SecondHalf);
123 match->setScore(tokens.at(2).toInt(), tokens.at(3).toInt());
127 qDebug() << "match out: " << match;
132 void BackendKicker::parsePage (QString htmlstr)
134 QStringList rawmatches;
135 QString hometeam, awayteam, tmp;
143 //qDebug() << "parsePage in: " << htmlstr;
145 rx.setPattern("<td class=\"first\">(.*)<td class=\"aligncenter last\">");
147 while ((pos = rx.indexIn(htmlstr, pos)) != -1) {
149 rawmatches.append(htmlstr.mid(pos, rx.matchedLength()));
150 qDebug() << "MATCH " << count << ":" << htmlstr.mid(pos, rx.matchedLength()) << "\n\n";
151 pos += rx.matchedLength();
154 rx.setPattern("<td.*>(.*)</td>");
156 QStringList::iterator i;
157 for (i = rawmatches.begin(); i != rawmatches.end(); ++i) {
160 while ((pos = rx.indexIn(*i, pos)) != -1) {
162 tmp = (*i).mid(pos, rx.matchedLength());
163 pos += rx.matchedLength();
166 date = parseDate(tmp);
169 hometeam = parseTeam(tmp);
172 awayteam = parseTeam(tmp);
173 match = getMatch(hometeam, awayteam, date);
176 parseScore(match, tmp);
187 void BackendKicker::setLeague(QString league)
189 if (league == "1. Bundesliga") {
190 m_URL = "http://www.kicker.de/news/fussball/bundesliga/spieltag/1-bundesliga/2010-11/spieltag.html";
191 } else if (league == "2. Bundesliga") {
192 m_URL = "http://www.kicker.de/news/fussball/bundesliga/spieltag/2-bundesliga/2010-11/spieltag.html";
193 } else if (league == "tipp3 Bundesliga") {
194 m_URL = "http://www.kicker.de/news/fussball/intligen/oesterreich/tipp3-bundesliga/2010-11/spieltag.html";
198 void BackendKicker::update()
200 QNetworkAccessManager *manager = new QNetworkAccessManager(this);
201 connect(manager, SIGNAL(finished(QNetworkReply*)),
202 this, SLOT(dlndFinished(QNetworkReply*)));
204 qDebug() << "URL: " << m_URL;
205 manager->get(QNetworkRequest(QUrl(m_URL)));
208 void BackendKicker::dlndFinished(QNetworkReply *reply)
212 if (reply->error() != QNetworkReply::NoError) {
213 // TODO proper user friendly error handling here!
214 qDebug() << "dlnd failed: error: " << reply->error();
217 rawdata = reply->readAll();