1
1
/*
2
- Copyright (c) 2017 Brian Lough. All right reserved.
2
+ Copyright (c) 2017 Brian Lough. All right reserved.
3
3
4
- YoutubeApi - An Arduino wrapper for the YouTube API
4
+ YoutubeApi - An Arduino wrapper for the YouTube API
5
5
6
- This library is free software; you can redistribute it and/or
7
- modify it under the terms of the GNU Lesser General Public
8
- License as published by the Free Software Foundation; either
9
- version 2.1 of the License, or (at your option) any later version.
6
+ This library is free software; you can redistribute it and/or
7
+ modify it under the terms of the GNU Lesser General Public
8
+ License as published by the Free Software Foundation; either
9
+ version 2.1 of the License, or (at your option) any later version.
10
10
11
- This library is distributed in the hope that it will be useful,
12
- but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
- Lesser General Public License for more details.
11
+ This library is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ Lesser General Public License for more details.
15
15
16
- You should have received a copy of the GNU Lesser General Public
17
- License along with this library; if not, write to the Free Software
18
- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
- */
16
+ You should have received a copy of the GNU Lesser General Public
17
+ License along with this library; if not, write to the Free Software
18
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
20
21
21
22
22
#include " YoutubeApi.h"
23
23
24
24
YoutubeApi::YoutubeApi (String apiKey, Client &client) {
25
- _apiKey = apiKey;
26
- this ->client = &client;
25
+ _apiKey = apiKey;
26
+ this ->client = &client;
27
27
}
28
28
29
29
String YoutubeApi::sendGetToYoutube (String command) {
30
30
String headers=" " ;
31
- String body=" " ;
32
- bool finishedHeaders = false ;
33
- bool currentLineIsBlank = true ;
34
- long now;
31
+ String body=" " ;
32
+ bool finishedHeaders = false ;
33
+ bool currentLineIsBlank = true ;
34
+ unsigned long now;
35
35
bool avail;
36
36
// Connect with youtube api over ssl
37
- if (client->connect (HOST, SSL_PORT )) {
37
+ if (client->connect (YTAPI_HOST, YTAPI_SSL_PORT )) {
38
38
// Serial.println(".... connected to server");
39
39
String a=" " ;
40
40
char c;
41
41
int ch_count=0 ;
42
42
client->println (" GET " +command+" &key=" +_apiKey);
43
43
now=millis ();
44
44
avail=false ;
45
- while (millis ()- now< 1500 ) {
45
+ while (millis () - now < YTAPI_TIMEOUT ) {
46
46
while (client->available ()) {
47
47
char c = client->read ();
48
48
// Serial.write(c);
49
49
50
- if (!finishedHeaders){
51
- if (currentLineIsBlank && c == ' \n ' ) {
52
- finishedHeaders = true ;
53
- }
54
- else {
55
- headers = headers + c;
56
-
57
- }
58
- } else {
59
- if (ch_count < maxMessageLength) {
60
- body=body+c;
61
- ch_count++;
62
- }
63
- }
64
-
65
- if (c == ' \n ' ) {
66
- currentLineIsBlank = true ;
67
- }else if (c != ' \r ' ) {
68
- currentLineIsBlank = false ;
69
- }
50
+ if (!finishedHeaders){
51
+ if (currentLineIsBlank && c == ' \n ' ) {
52
+ finishedHeaders = true ;
53
+ }
54
+ else {
55
+ headers = headers + c;
56
+
57
+ }
58
+ } else {
59
+ if (ch_count < maxMessageLength) {
60
+ body=body+c;
61
+ ch_count++;
62
+ }
63
+ }
64
+
65
+ if (c == ' \n ' ) {
66
+ currentLineIsBlank = true ;
67
+ }else if (c != ' \r ' ) {
68
+ currentLineIsBlank = false ;
69
+ }
70
70
71
71
avail=true ;
72
72
}
@@ -79,34 +79,31 @@ String YoutubeApi::sendGetToYoutube(String command) {
79
79
}
80
80
}
81
81
82
- // int lastCharIndex = body.lastIndexOf("}");
83
-
84
- // return body.substring(0,lastCharIndex+1);
85
- return body;
82
+ return body;
86
83
}
87
84
88
85
bool YoutubeApi::getChannelStatistics (String channelId){
89
- String command=" https://www.googleapis.com /youtube/v3/channels?part=statistics&id=" +channelId; // If you can't find it(for example if you have a custom url) look here: https://www.youtube.com/account_advanced
90
- String response = sendGetToYoutube (command); // recieve reply from youtube
91
- DynamicJsonBuffer jsonBuffer;
92
- JsonObject& root = jsonBuffer.parseObject (response);
93
- if (root.success ()) {
94
- if (root.containsKey (" items" )) {
95
- long subscriberCount = root[" items" ][0 ][" statistics" ][" subscriberCount" ];
96
- long viewCount = root[" items" ][0 ][" statistics" ][" viewCount" ];
97
- long commentCount = root[" items" ][0 ][" statistics" ][" commentCount" ];
98
- long hiddenSubscriberCount = root[" items" ][0 ][" statistics" ][" hiddenSubscriberCount" ];
99
- long videoCount = root[" items" ][0 ][" statistics" ][" videoCount" ];
100
-
101
- channelStats.viewCount = viewCount;
102
- channelStats.subscriberCount = subscriberCount;
103
- channelStats.commentCount = commentCount;
104
- channelStats.hiddenSubscriberCount = hiddenSubscriberCount;
105
- channelStats.videoCount = videoCount;
106
-
107
- return true ;
108
- }
109
- }
110
-
111
- return false ;
86
+ String command=" https://" YTAPI_HOST " /youtube/v3/channels?part=statistics&id=" +channelId; // If you can't find it(for example if you have a custom url) look here: https://www.youtube.com/account_advanced
87
+ String response = sendGetToYoutube (command); // recieve reply from youtube
88
+ DynamicJsonBuffer jsonBuffer;
89
+ JsonObject& root = jsonBuffer.parseObject (response);
90
+ if (root.success ()) {
91
+ if (root.containsKey (" items" )) {
92
+ long subscriberCount = root[" items" ][0 ][" statistics" ][" subscriberCount" ];
93
+ long viewCount = root[" items" ][0 ][" statistics" ][" viewCount" ];
94
+ long commentCount = root[" items" ][0 ][" statistics" ][" commentCount" ];
95
+ long hiddenSubscriberCount = root[" items" ][0 ][" statistics" ][" hiddenSubscriberCount" ];
96
+ long videoCount = root[" items" ][0 ][" statistics" ][" videoCount" ];
97
+
98
+ channelStats.viewCount = viewCount;
99
+ channelStats.subscriberCount = subscriberCount;
100
+ channelStats.commentCount = commentCount;
101
+ channelStats.hiddenSubscriberCount = hiddenSubscriberCount;
102
+ channelStats.videoCount = videoCount;
103
+
104
+ return true ;
105
+ }
106
+ }
107
+
108
+ return false ;
112
109
}
0 commit comments