]> git.sur5r.net Git - minitube/blob - src/channelsuggest.cpp
Imported Upstream version 1.4.1
[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() {
9
10 }
11
12 void ChannelSuggest::suggest(QString query) {
13
14     /* // TODO how to localize results?
15     QString locale = QLocale::system().name().replace("_", "-");
16     // case for system locales such as "C"
17     if (locale.length() < 2) {
18         locale = "en-US";
19     }*/
20
21     QUrl url("http://www.youtube.com/results?search_type=search_users");
22     url.addQueryItem("search_query", query);
23     // url.addQueryItem("hl", "it-IT");
24
25     QObject *reply = The::http()->get(url);
26     connect(reply, SIGNAL(data(QByteArray)), SLOT(handleNetworkData(QByteArray)));
27 }
28
29 void ChannelSuggest::handleNetworkData(QByteArray data) {
30     QStringList choices;
31     QString html = QString::fromUtf8(data);
32     QRegExp re("/user/([a-zA-Z0-9]+)");
33
34     int pos = 0;
35     while ((pos = re.indexIn(html, pos)) != -1) {
36         // qDebug() << re.cap(0) << re.cap(1);
37         QString choice = re.cap(1);
38         if (!choices.contains(choice)) {
39             choices << choice;
40             if (choices.size() == 10) break;
41         }
42         pos += re.matchedLength();
43     }
44
45     emit ready(choices);
46 }