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 ;
31
+ String body=" " ;
32
+ bool finishedHeaders = false ;
33
+ bool currentLineIsBlank = true ;
34
34
long now;
35
35
bool avail;
36
36
// Connect with youtube api over ssl
@@ -47,26 +47,26 @@ String YoutubeApi::sendGetToYoutube(String command) {
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,34 @@ String YoutubeApi::sendGetToYoutube(String command) {
79
79
}
80
80
}
81
81
82
- // int lastCharIndex = body.lastIndexOf("}");
82
+ // int lastCharIndex = body.lastIndexOf("}");
83
83
84
84
// return body.substring(0,lastCharIndex+1);
85
- return body;
85
+ return body;
86
86
}
87
87
88
88
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 ;
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 ;
112
112
}
0 commit comments