From 29bb3e54c7d4fcfcdd0547c2d359ad6d396f9574 Mon Sep 17 00:00:00 2001 From: Kenneth Finnegan Date: Sat, 4 Mar 2017 15:54:16 -0800 Subject: [PATCH 1/4] Whitespace fix for library cpp file --- src/YoutubeApi.cpp | 128 ++++++++++++++++++++++----------------------- 1 file changed, 64 insertions(+), 64 deletions(-) diff --git a/src/YoutubeApi.cpp b/src/YoutubeApi.cpp index b9328af..1345307 100644 --- a/src/YoutubeApi.cpp +++ b/src/YoutubeApi.cpp @@ -1,36 +1,36 @@ /* -Copyright (c) 2017 Brian Lough. All right reserved. + Copyright (c) 2017 Brian Lough. All right reserved. -YoutubeApi - An Arduino wrapper for the YouTube API + YoutubeApi - An Arduino wrapper for the YouTube API -This library is free software; you can redistribute it and/or -modify it under the terms of the GNU Lesser General Public -License as published by the Free Software Foundation; either -version 2.1 of the License, or (at your option) any later version. + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. -This library is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -Lesser General Public License for more details. + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. -You should have received a copy of the GNU Lesser General Public -License along with this library; if not, write to the Free Software -Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ #include "YoutubeApi.h" YoutubeApi::YoutubeApi(String apiKey, Client &client) { - _apiKey = apiKey; - this->client = &client; + _apiKey = apiKey; + this->client = &client; } String YoutubeApi::sendGetToYoutube(String command) { String headers=""; - String body=""; - bool finishedHeaders = false; - bool currentLineIsBlank = true; + String body=""; + bool finishedHeaders = false; + bool currentLineIsBlank = true; long now; bool avail; // Connect with youtube api over ssl @@ -47,26 +47,26 @@ String YoutubeApi::sendGetToYoutube(String command) { char c = client->read(); //Serial.write(c); - if(!finishedHeaders){ - if (currentLineIsBlank && c == '\n') { - finishedHeaders = true; - } - else{ - headers = headers + c; - - } - } else { - if (ch_count < maxMessageLength) { - body=body+c; - ch_count++; - } - } - - if (c == '\n') { - currentLineIsBlank = true; - }else if (c != '\r') { - currentLineIsBlank = false; - } + if(!finishedHeaders){ + if (currentLineIsBlank && c == '\n') { + finishedHeaders = true; + } + else{ + headers = headers + c; + + } + } else { + if (ch_count < maxMessageLength) { + body=body+c; + ch_count++; + } + } + + if (c == '\n') { + currentLineIsBlank = true; + }else if (c != '\r') { + currentLineIsBlank = false; + } avail=true; } @@ -79,34 +79,34 @@ String YoutubeApi::sendGetToYoutube(String command) { } } - //int lastCharIndex = body.lastIndexOf("}"); + //int lastCharIndex = body.lastIndexOf("}"); //return body.substring(0,lastCharIndex+1); - return body; + return body; } bool YoutubeApi::getChannelStatistics(String channelId){ - 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 - String response = sendGetToYoutube(command); //recieve reply from youtube - DynamicJsonBuffer jsonBuffer; - JsonObject& root = jsonBuffer.parseObject(response); - if(root.success()) { - if (root.containsKey("items")) { - long subscriberCount = root["items"][0]["statistics"]["subscriberCount"]; - long viewCount = root["items"][0]["statistics"]["viewCount"]; - long commentCount = root["items"][0]["statistics"]["commentCount"]; - long hiddenSubscriberCount = root["items"][0]["statistics"]["hiddenSubscriberCount"]; - long videoCount = root["items"][0]["statistics"]["videoCount"]; - - channelStats.viewCount = viewCount; - channelStats.subscriberCount = subscriberCount; - channelStats.commentCount = commentCount; - channelStats.hiddenSubscriberCount = hiddenSubscriberCount; - channelStats.videoCount = videoCount; - - return true; - } - } - - return false; + 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 + String response = sendGetToYoutube(command); //recieve reply from youtube + DynamicJsonBuffer jsonBuffer; + JsonObject& root = jsonBuffer.parseObject(response); + if(root.success()) { + if (root.containsKey("items")) { + long subscriberCount = root["items"][0]["statistics"]["subscriberCount"]; + long viewCount = root["items"][0]["statistics"]["viewCount"]; + long commentCount = root["items"][0]["statistics"]["commentCount"]; + long hiddenSubscriberCount = root["items"][0]["statistics"]["hiddenSubscriberCount"]; + long videoCount = root["items"][0]["statistics"]["videoCount"]; + + channelStats.viewCount = viewCount; + channelStats.subscriberCount = subscriberCount; + channelStats.commentCount = commentCount; + channelStats.hiddenSubscriberCount = hiddenSubscriberCount; + channelStats.videoCount = videoCount; + + return true; + } + } + + return false; } From 6fdd76d9483229f9384757b9202f2173882be918 Mon Sep 17 00:00:00 2001 From: Kenneth Finnegan Date: Sat, 4 Mar 2017 15:56:37 -0800 Subject: [PATCH 2/4] Removed commented out code --- src/YoutubeApi.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/YoutubeApi.cpp b/src/YoutubeApi.cpp index 1345307..7e2c8ff 100644 --- a/src/YoutubeApi.cpp +++ b/src/YoutubeApi.cpp @@ -79,9 +79,6 @@ String YoutubeApi::sendGetToYoutube(String command) { } } - //int lastCharIndex = body.lastIndexOf("}"); - - //return body.substring(0,lastCharIndex+1); return body; } From abcc3d2ef42c943bc2bf8eb53b0ab29178ee0714 Mon Sep 17 00:00:00 2001 From: Kenneth Finnegan Date: Sat, 4 Mar 2017 15:59:47 -0800 Subject: [PATCH 3/4] Correct data type for variable now --- src/YoutubeApi.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/YoutubeApi.cpp b/src/YoutubeApi.cpp index 7e2c8ff..f7700d8 100644 --- a/src/YoutubeApi.cpp +++ b/src/YoutubeApi.cpp @@ -31,7 +31,7 @@ String YoutubeApi::sendGetToYoutube(String command) { String body=""; bool finishedHeaders = false; bool currentLineIsBlank = true; - long now; + unsigned long now; bool avail; // Connect with youtube api over ssl if (client->connect(HOST, SSL_PORT)) { From 5d50830174c24e0ca4c71903213aa41b9706a126 Mon Sep 17 00:00:00 2001 From: Kenneth Finnegan Date: Sat, 4 Mar 2017 16:07:39 -0800 Subject: [PATCH 4/4] Added unique prefix to preprocessor macros and updated their usage Using generic preprocessor macros like HOST opens the risk of another library or the user trying to define the same macro. To correct this, I prefixed all macros for the YouTubeAPI with YTAPI_ so they're unique. Also removed unused macros, replaced the hardcoded hostname with the existing macro, and pulled out the hardcoded timeout. --- src/YoutubeApi.cpp | 6 +++--- src/YoutubeApi.h | 8 +++----- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/YoutubeApi.cpp b/src/YoutubeApi.cpp index f7700d8..c291157 100644 --- a/src/YoutubeApi.cpp +++ b/src/YoutubeApi.cpp @@ -34,7 +34,7 @@ String YoutubeApi::sendGetToYoutube(String command) { unsigned long now; bool avail; // Connect with youtube api over ssl - if (client->connect(HOST, SSL_PORT)) { + if (client->connect(YTAPI_HOST, YTAPI_SSL_PORT)) { // Serial.println(".... connected to server"); String a=""; char c; @@ -42,7 +42,7 @@ String YoutubeApi::sendGetToYoutube(String command) { client->println("GET "+command+"&key="+_apiKey); now=millis(); avail=false; - while (millis()-now<1500) { + while (millis() - now < YTAPI_TIMEOUT) { while (client->available()) { char c = client->read(); //Serial.write(c); @@ -83,7 +83,7 @@ String YoutubeApi::sendGetToYoutube(String command) { } bool YoutubeApi::getChannelStatistics(String channelId){ - 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 + 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 String response = sendGetToYoutube(command); //recieve reply from youtube DynamicJsonBuffer jsonBuffer; JsonObject& root = jsonBuffer.parseObject(response); diff --git a/src/YoutubeApi.h b/src/YoutubeApi.h index dfea1cf..207e5e2 100644 --- a/src/YoutubeApi.h +++ b/src/YoutubeApi.h @@ -26,11 +26,9 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include #include -#define HOST "www.googleapis.com" -#define SSL_PORT 443 -#define HANDLE_MESSAGES 1 -#define MAX_BUFFER_SIZE 1250 - +#define YTAPI_HOST "www.googleapis.com" +#define YTAPI_SSL_PORT 443 +#define YTAPI_TIMEOUT 1500 struct channelStatistics{