Skip to content

Commit 13c1533

Browse files
authored
Merge pull request #4 from PhirePhly/kwf_fixes
Assorted code cleanups in the library source
2 parents ee9eb09 + 5d50830 commit 13c1533

File tree

2 files changed

+69
-74
lines changed

2 files changed

+69
-74
lines changed

src/YoutubeApi.cpp

Lines changed: 66 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,72 @@
11
/*
2-
Copyright (c) 2017 Brian Lough. All right reserved.
2+
Copyright (c) 2017 Brian Lough. All right reserved.
33
4-
YoutubeApi - An Arduino wrapper for the YouTube API
4+
YoutubeApi - An Arduino wrapper for the YouTube API
55
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.
1010
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.
1515
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+
*/
2020

2121

2222
#include "YoutubeApi.h"
2323

2424
YoutubeApi::YoutubeApi(String apiKey, Client &client) {
25-
_apiKey = apiKey;
26-
this->client = &client;
25+
_apiKey = apiKey;
26+
this->client = &client;
2727
}
2828

2929
String YoutubeApi::sendGetToYoutube(String command) {
3030
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;
3535
bool avail;
3636
// Connect with youtube api over ssl
37-
if (client->connect(HOST, SSL_PORT)) {
37+
if (client->connect(YTAPI_HOST, YTAPI_SSL_PORT)) {
3838
// Serial.println(".... connected to server");
3939
String a="";
4040
char c;
4141
int ch_count=0;
4242
client->println("GET "+command+"&key="+_apiKey);
4343
now=millis();
4444
avail=false;
45-
while (millis()-now<1500) {
45+
while (millis() - now < YTAPI_TIMEOUT) {
4646
while (client->available()) {
4747
char c = client->read();
4848
//Serial.write(c);
4949

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+
}
7070

7171
avail=true;
7272
}
@@ -79,34 +79,31 @@ String YoutubeApi::sendGetToYoutube(String command) {
7979
}
8080
}
8181

82-
//int lastCharIndex = body.lastIndexOf("}");
83-
84-
//return body.substring(0,lastCharIndex+1);
85-
return body;
82+
return body;
8683
}
8784

8885
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;
112109
}

src/YoutubeApi.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,9 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2626
#include <ArduinoJson.h>
2727
#include <Client.h>
2828

29-
#define HOST "www.googleapis.com"
30-
#define SSL_PORT 443
31-
#define HANDLE_MESSAGES 1
32-
#define MAX_BUFFER_SIZE 1250
33-
29+
#define YTAPI_HOST "www.googleapis.com"
30+
#define YTAPI_SSL_PORT 443
31+
#define YTAPI_TIMEOUT 1500
3432

3533

3634
struct channelStatistics{

0 commit comments

Comments
 (0)