1 #include <QtNetwork/QNetworkAccessManager>
2 #include <QtNetwork/QNetworkRequest>
9 #include "backendkicker.h"
10 #include "livescores.h"
12 BackendKicker::BackendKicker(QObject *parent) :
18 Match* BackendKicker::getMatch(QString hometeam, QString awayteam)
20 QListIterator<Match*> iter(m_matchlist);
23 while (iter.hasNext()) {
25 if (match->awayteam() == awayteam &&
26 match->hometeam() == hometeam) {
31 match = new Match(hometeam, awayteam, this);
32 m_matchlist.append(match);
34 emit matchListChanged();
39 QList<Match*> BackendKicker::matchList()
44 static QDateTime parseDate(QString datehtml)
46 static QDateTime lastParsedDate;
50 int month, day, hour, minute;
52 //qDebug() << "parseDate in: " << datehtml;
54 tokens = datehtml.split(QRegExp("[>.&;:<\"]"), QString::SkipEmptyParts);
55 date = QDate::currentDate();
58 if (tokens.count() < 6) {
59 return lastParsedDate;
62 month = (tokens.at(2)).toInt();
63 day = (tokens.at(1)).toInt();
64 hour = (tokens.at(4)).toInt();
65 minute = (tokens.at(5)).toInt();
67 lastParsedDate = QDateTime(QDate(date.year(), month, day),
70 return lastParsedDate;
73 static QString parseTeam(QString teamhtml)
77 //qDebug() << "parseTeam in: " << teamhtml;
79 teamhtml.truncate(teamhtml.indexOf("</a>"));
80 team = teamhtml.mid(teamhtml.lastIndexOf(">") + 1);
82 qDebug() << "parseTeam out: " << team;
86 static int parseScore(QString scorehtml, bool home, bool firsthalf)
92 qDebug() << "parseScore in: " << scorehtml;
94 tokens = scorehtml.split(QRegExp("[>&();:<]"), QString::SkipEmptyParts);
98 if (!home) { index++; }
99 if (firsthalf) { index += 3; }
101 score = tokens.at(index).toInt();
103 qDebug() << "parseScore out: " << score;
108 void BackendKicker::parsePage (QString htmlstr)
110 QStringList rawmatches;
111 QString hometeam, awayteam, tmp;
118 int homescore, awayscore;
119 int homescorefh, awayscorefh;
122 //qDebug() << "parsePage in: " << htmlstr;
124 rx.setPattern("<td class=\"first\">(.*)<td class=\"aligncenter last\">");
126 while ((pos = rx.indexIn(htmlstr, pos)) != -1) {
128 rawmatches.append(htmlstr.mid(pos, rx.matchedLength()));
129 qDebug() << "MATCH " << count << ":" << htmlstr.mid(pos, rx.matchedLength()) << "\n\n";
130 pos += rx.matchedLength();
133 rx.setPattern("<td.*>(.*)</td>");
135 QStringList::iterator i;
136 for (i = rawmatches.begin(); i != rawmatches.end(); ++i) {
139 while ((pos = rx.indexIn(*i, pos)) != -1) {
141 tmp = (*i).mid(pos, rx.matchedLength());
142 pos += rx.matchedLength();
145 date = parseDate(tmp);
148 hometeam = parseTeam(tmp);
151 awayteam = parseTeam(tmp);
154 homescore = parseScore(tmp, true, false);
155 homescorefh = parseScore(tmp, true, true);
156 awayscore = parseScore(tmp, false, false);
157 awayscorefh = parseScore(tmp, false, true);
163 match = getMatch(hometeam, awayteam);
164 match->setScore(homescore, awayscore);
170 void BackendKicker::update()
172 QString URL = "http://www.kicker.de/news/fussball/bundesliga/spieltag/1-bundesliga/2010-11/spieltag.html";
174 QNetworkAccessManager *manager = new QNetworkAccessManager(this);
175 connect(manager, SIGNAL(finished(QNetworkReply*)),
176 this, SLOT(dlndFinished(QNetworkReply*)));
178 qDebug() << "URL: " << URL;
179 manager->get(QNetworkRequest(QUrl(URL)));
182 void BackendKicker::dlndFinished(QNetworkReply *reply)
186 if (reply->error() != QNetworkReply::NoError) {
187 // TODO proper user friendly error handling here!
188 qDebug() << "dlnd failed: error: " << reply->error();
191 rawdata = reply->readAll();