-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitchapi.cpp
183 lines (104 loc) · 4.36 KB
/
twitchapi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "twitchapi.h"
TwitchApi::TwitchApi(QObject *parent, QString oauthtoken) :
QObject(parent)
{
QObject::connect(&m_nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(parseNetworkResponse(QNetworkReply*)));
this->oAuthAccessToken = oauthtoken;
}
void TwitchApi::setOAuthAccessToken(QString oauthtoken) {
this->oAuthAccessToken = oauthtoken;
}
void TwitchApi::setStatusAndGameTitle(QString user, QHash<QString, QString> urlParams )
{
genericHelper::log("twitch-api setStatusAndGameTitle - https://api.twitch.tv/kraken/channels/.");
this->putRequest("https://api.twitch.tv/kraken/channels/"+user, urlParams);
}
void TwitchApi::getUser()
{
genericHelper::log("twitch-api getUser - https://api.twitch.tv/kraken/user.");
this->getRequestAuthenticated("https://api.twitch.tv/kraken/user");
}
void TwitchApi::getFollows(QString user)
{
genericHelper::log("twitch-api getFollows - https://api.twitch.tv/kraken/users/"+user+"/follows/channels");
this->getRequest("https://api.twitch.tv/kraken/users/"+user+"/follows/channels");
}
void TwitchApi::getStream(QString user)
{
genericHelper::log("twitch-api getStream - https://api.twitch.tv/kraken/streams/"+user);
this->getRequest("https://api.twitch.tv/kraken/streams/"+user);
}
void TwitchApi::getGame()
{
genericHelper::log("twitch-api getGame - https://api.twitch.tv/kraken/games/top?limit=100");
this->getRequest("https://api.twitch.tv/kraken/games/top?limit=1001");
}
void TwitchApi::getKrakenChannel()
{
genericHelper::log("twitch-api getKrakenChannel - https://api.twitch.tv/kraken/channel");
this->getRequestAuthenticated("https://api.twitch.tv/kraken/channel");
}
void TwitchApi::getChannel(QString user)
{
genericHelper::log("twitch-api getChannel - https://api.twitch.tv/kraken/channels/"+user);
this->getRequest("https://api.twitch.tv/kraken/channels/"+user);
}
void TwitchApi::searchGames(QString searchStr)
{
genericHelper::log("twitch-api searchGames - https://api.twitch.tv/kraken/search/games?q="+QUrl::toPercentEncoding(searchStr)+"&type=suggest");
this->getRequest("https://api.twitch.tv/kraken/search/games?q="+QUrl::toPercentEncoding(searchStr)+"&type=suggest");
}
void TwitchApi::putRequest( const QString &urlString, QHash<QString, QString> urlParams)
{
QUrl url ( urlString );
QUrlQuery query(url);
for (QHash<QString, QString>::iterator iter = urlParams.begin(); iter != urlParams.end(); ++iter) {
query.addQueryItem(iter.key(),iter.value());
}
url.setQuery(query);
QNetworkRequest req ( url );
req.setRawHeader("Accept", "application/vnd.twitchtv.v3+json");
req.setRawHeader("Authorization", "OAuth "+this->oAuthAccessToken.toLatin1());
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded" );
m_nam.put( req, url.toEncoded() );
}
void TwitchApi::getRequestAuthenticated( const QString &urlString )
{
QUrl url ( urlString );
QNetworkRequest req ( url );
req.setRawHeader("Accept", "application/vnd.twitchtv.v3+json");
req.setRawHeader("Authorization", "OAuth "+this->oAuthAccessToken.toLatin1());
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded" );
m_nam.get( req );
}
void TwitchApi::getRequest( const QString &urlString )
{
QUrl url ( urlString );
QNetworkRequest req ( url );
req.setRawHeader("Accept", "application/vnd.twitchtv.v3+json");
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded" );
m_nam.get( req );
}
void TwitchApi::parseNetworkResponse( QNetworkReply *finished )
{
if ( finished->error() != QNetworkReply::NoError )
{
// A communication error has occurred
//qDebug()<< finished->errorString();
emit networkError( finished->errorString() );
return;
}
// QNetworkReply is a QIODevice. So we read from it just like it was a file
QByteArray data = finished->readAll();
// qDebug() << data;
json_buffer = QJsonDocument::fromJson(data);
// qDebug()<< json_buffer;
emit twitchReady( json_buffer );
// new parser
QJsonObject jsonObject = json_buffer.object();
if (!jsonObject["top"].isNull()){
emit twitchGameReady ( jsonObject );
} else if (!jsonObject["stream_key"].isNull()) {
emit twitchStreamKeyReady ( jsonObject );
}
}