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);
40 QList<Match*> BackendKicker::matchList()
45 static QDateTime parseDate(QString datehtml)
47 static QDateTime lastParsedDate;
51 int month, day, hour, minute;
53 //qDebug() << "parseDate in: " << datehtml;
55 tokens = datehtml.split(QRegExp("[>.&;:<\"]"), QString::SkipEmptyParts);
56 date = QDate::currentDate();
59 if (tokens.count() < 6) {
60 return lastParsedDate;
63 month = (tokens.at(2)).toInt();
64 day = (tokens.at(1)).toInt();
65 hour = (tokens.at(4)).toInt();
66 minute = (tokens.at(5)).toInt();
68 lastParsedDate = QDateTime(QDate(date.year(), month, day),
71 return lastParsedDate;
74 static QString parseTeam(QString teamhtml)
78 //qDebug() << "parseTeam in: " << teamhtml;
80 teamhtml.truncate(teamhtml.indexOf("</a>"));
81 team = teamhtml.mid(teamhtml.lastIndexOf(">") + 1);
83 qDebug() << "parseTeam out: " << team;
87 static int parseScore(Match* match, QString scorehtml)
92 qDebug() << "parseScore in: " << scorehtml;
93 tokens = scorehtml.split(QRegExp("[>&();:<]"), QString::SkipEmptyParts);
96 if (tokens.count() == 7) {
97 // no extra color tag -> either not started, halftime or finished
98 if (tokens.at(4) == "-") {
99 // no first half results -> match not started yet
100 match->setState(Match::NotStarted);
101 } else if (tokens.at(1) == "-") {
102 // second half has not been started but there are first
103 // half results -> currently half time
104 match->setScore(tokens.at(4).toInt(), tokens.at(5).toInt());
105 match->setState(Match::HalfTime);
107 // no color tag and no "-" -> game is finished
108 match->setScore(tokens.at(1).toInt(), tokens.at(3).toInt());
109 match->setState(Match::Finished);
112 // there is a color tag which means that either first
113 // half or second half are currently running
114 if (tokens.at(4).contains("color")) {
115 // first half score marked red -> first half running
116 match->setScore(tokens.at(5).toInt(), tokens.at(6).toInt());
117 match->setState(Match::FirstHalf);
118 } else if (tokens.at(1).contains("color")) {
119 // second half score marked res -> second half running
120 match->setState(Match::SecondHalf);
121 match->setScore(tokens.at(2).toInt(), tokens.at(3).toInt());
125 qDebug() << "match out: " << match;
130 void BackendKicker::parsePage (QString htmlstr)
132 QStringList rawmatches;
133 QString hometeam, awayteam, tmp;
141 //qDebug() << "parsePage in: " << htmlstr;
143 rx.setPattern("<td class=\"first\">(.*)<td class=\"aligncenter last\">");
145 while ((pos = rx.indexIn(htmlstr, pos)) != -1) {
147 rawmatches.append(htmlstr.mid(pos, rx.matchedLength()));
148 qDebug() << "MATCH " << count << ":" << htmlstr.mid(pos, rx.matchedLength()) << "\n\n";
149 pos += rx.matchedLength();
152 rx.setPattern("<td.*>(.*)</td>");
154 QStringList::iterator i;
155 for (i = rawmatches.begin(); i != rawmatches.end(); ++i) {
158 while ((pos = rx.indexIn(*i, pos)) != -1) {
160 tmp = (*i).mid(pos, rx.matchedLength());
161 pos += rx.matchedLength();
164 date = parseDate(tmp);
167 hometeam = parseTeam(tmp);
170 awayteam = parseTeam(tmp);
171 match = getMatch(hometeam, awayteam, date);
174 parseScore(match, tmp);
183 void BackendKicker::setLeague(QString league)
185 if (league == "1. Bundesliga") {
186 m_URL = "http://www.kicker.de/news/fussball/bundesliga/spieltag/1-bundesliga/2010-11/spieltag.html";
187 } else if (league == "2. Bundesliga") {
188 m_URL = "http://www.kicker.de/news/fussball/bundesliga/spieltag/2-bundesliga/2010-11/spieltag.html";
189 } else if (league == "tipp3 Bundesliga") {
190 m_URL = "http://www.kicker.de/news/fussball/intligen/oesterreich/tipp3-bundesliga/2010-11/spieltag.html";
194 this->m_matchlist.clear();
195 emit matchListChanged();
198 void BackendKicker::update()
200 //QString URL = "file:///home/david/Projects/git-buliscores/testdata/spieltag.html";
202 QNetworkAccessManager *manager = new QNetworkAccessManager(this);
203 connect(manager, SIGNAL(finished(QNetworkReply*)),
204 this, SLOT(dlndFinished(QNetworkReply*)));
206 qDebug() << "URL: " << m_URL;
207 manager->get(QNetworkRequest(QUrl(m_URL)));
210 void BackendKicker::dlndFinished(QNetworkReply *reply)
214 if (reply->error() != QNetworkReply::NoError) {
215 // TODO proper user friendly error handling here!
216 qDebug() << "dlnd failed: error: " << reply->error();
219 rawdata = reply->readAll();
221 emit matchListChanged();