added intermedia code that selects the corrent icons for the Kicker
[buliscores] / src / backendkicker.cpp
1 #include <QtNetwork/QNetworkAccessManager>
2 #include <QtNetwork/QNetworkRequest>
3 #include <QUrl>
4 #include <QRegExp>
5 #include <QDebug>
6 #include <QStringList>
7 #include <QDateTime>
8
9 #include "backendkicker.h"
10 #include "livescores.h"
11
12 BackendKicker::BackendKicker(QObject *parent) :
13     QObject(parent)
14 {
15     this->update();
16 }
17
18 Match* BackendKicker::getMatch(QString hometeam, QString awayteam)
19 {
20     QListIterator<Match*> iter(m_matchlist);
21     Match*        match;
22
23     while (iter.hasNext()) {
24         match = iter.next();
25         if (match->awayteam() == awayteam &&
26             match->hometeam() == hometeam) {
27             return match;
28         }
29     }
30
31     match = new Match(hometeam, awayteam, this);
32     m_matchlist.append(match);
33
34     emit matchListChanged();
35
36     return match;
37 }
38
39 QList<Match*> BackendKicker::matchList()
40 {
41     return m_matchlist;
42 }
43
44 static QDateTime parseDate(QString datehtml)
45 {
46     static QDateTime    lastParsedDate;
47     QStringList         tokens;
48     QDate               date;
49
50     int month, day, hour, minute;
51
52     //qDebug() << "parseDate in: " << datehtml;
53
54     tokens = datehtml.split(QRegExp("[>.&;:<\"]"), QString::SkipEmptyParts);
55     date = QDate::currentDate();
56
57     qDebug() << tokens;
58     if (tokens.count() < 6) {
59         return lastParsedDate;
60     }
61
62     month  = (tokens.at(2)).toInt();
63     day    = (tokens.at(1)).toInt();
64     hour   = (tokens.at(4)).toInt();
65     minute = (tokens.at(5)).toInt();
66
67     lastParsedDate =  QDateTime(QDate(date.year(), month, day),
68                                 QTime(hour, minute));
69
70     return lastParsedDate;
71 }
72
73 static QString parseTeam(QString teamhtml)
74 {
75     QString team;
76
77     //qDebug() << "parseTeam in: " << teamhtml;
78
79     teamhtml.truncate(teamhtml.indexOf("</a>"));
80     team = teamhtml.mid(teamhtml.lastIndexOf(">") + 1);
81
82     qDebug() << "parseTeam out: " << team;
83     return team;
84 }
85
86 static int parseScore(QString scorehtml, bool home, bool firsthalf)
87 {
88     int         score = -1;
89     int         index = 0;
90     QStringList tokens;
91
92     qDebug() << "parseScore in: " << scorehtml;
93
94     tokens = scorehtml.split(QRegExp("[>&();:<]"), QString::SkipEmptyParts);
95     qDebug() << tokens;
96
97     index = 1;
98     if (!home)      { index++;    }
99     if (firsthalf)  { index += 3; }
100
101     score = tokens.at(index).toInt();
102
103     qDebug() << "parseScore out: " << score;
104
105     return score;
106 }
107
108 void BackendKicker::parsePage (QString htmlstr)
109 {
110     QStringList     rawmatches;
111     QString         hometeam, awayteam, tmp;
112     QRegExp         rx;
113     QDateTime       date;
114     Match*          match;
115
116     int             pos     = 0;
117     int             count   = 0;
118     int             homescore, awayscore;
119     int             homescorefh, awayscorefh;
120
121
122     //qDebug() << "parsePage in: " << htmlstr;
123
124     rx.setPattern("<td class=\"first\">(.*)<td class=\"aligncenter last\">");
125     rx.setMinimal(true);
126     while ((pos = rx.indexIn(htmlstr, pos)) != -1) {
127          ++count;
128          rawmatches.append(htmlstr.mid(pos, rx.matchedLength()));
129          qDebug() << "MATCH " << count << ":" << htmlstr.mid(pos, rx.matchedLength()) << "\n\n";
130          pos += rx.matchedLength();
131      }
132
133     rx.setPattern("<td.*>(.*)</td>");
134
135     QStringList::iterator i;
136     for (i = rawmatches.begin(); i != rawmatches.end(); ++i) {
137         pos = 0;
138         count = 0;
139         while ((pos = rx.indexIn(*i, pos)) != -1) {
140              ++count;
141              tmp = (*i).mid(pos, rx.matchedLength());
142              pos += rx.matchedLength();
143              switch (count) {
144              case 2: // date
145                  date = parseDate(tmp);
146                  break;
147              case 3: // hometeam
148                  hometeam = parseTeam(tmp);
149                  break;
150              case 5: // awayteam
151                  awayteam = parseTeam(tmp);
152                  break;
153              case 6: // scores
154                  homescore   = parseScore(tmp, true, false);
155                  homescorefh = parseScore(tmp, true, true);
156                  awayscore   = parseScore(tmp, false, false);
157                  awayscorefh = parseScore(tmp, false, true);
158                  break;
159              default:
160                 ;;
161              }
162         }
163         match = getMatch(hometeam, awayteam);
164         match->setScore(homescore, awayscore);
165     }
166
167
168 }
169
170 void BackendKicker::update()
171 {
172     QString URL = "http://www.kicker.de/news/fussball/bundesliga/spieltag/1-bundesliga/2010-11/spieltag.html";
173
174     QNetworkAccessManager *manager = new QNetworkAccessManager(this);
175     connect(manager, SIGNAL(finished(QNetworkReply*)),
176             this, SLOT(dlndFinished(QNetworkReply*)));
177
178     qDebug() << "URL: " << URL;
179     manager->get(QNetworkRequest(QUrl(URL)));
180 }
181
182 void BackendKicker::dlndFinished(QNetworkReply *reply)
183 {
184     QString         rawdata;
185
186     if (reply->error() != QNetworkReply::NoError) {
187         // TODO proper user friendly error handling here!
188         qDebug() << "dlnd failed: error: " << reply->error();
189     }
190
191     rawdata = reply->readAll();
192     parsePage(rawdata);
193 }