]> git.sur5r.net Git - minitube/blob - src/channelsuggest.cpp
Imported Upstream version 2.0
[minitube] / src / channelsuggest.cpp
1 #include "channelsuggest.h"
2 #include "networkaccess.h"
3
4 namespace The {
5     NetworkAccess* http();
6 }
7
8 ChannelSuggest::ChannelSuggest(QObject *parent) : Suggester(parent) {
9
10 }
11
12 void ChannelSuggest::suggest(QString query) {
13     QUrl url("http://www.youtube.com/results");
14     url.addQueryItem("search_type", "search_users");
15     url.addQueryItem("search_query", query);
16     QObject *reply = The::http()->get(url);
17     connect(reply, SIGNAL(data(QByteArray)), SLOT(handleNetworkData(QByteArray)));
18 }
19
20 void ChannelSuggest::handleNetworkData(QByteArray data) {
21     QStringList choices;
22     QString html = QString::fromUtf8(data);
23     QRegExp re("/user/([a-zA-Z0-9]+)");
24
25     int pos = 0;
26     while ((pos = re.indexIn(html, pos)) != -1) {
27         // qDebug() << re.cap(0) << re.cap(1);
28         QString choice = re.cap(1);
29         if (!choices.contains(choice, Qt::CaseInsensitive)) {
30             choices << choice;
31             if (choices.size() == 10) break;
32         }
33         pos += re.matchedLength();
34     }
35
36     emit ready(choices);
37 }