Skip to content

Commit

Permalink
Merge pull request #4 from PhirePhly/kwf_fixes
Browse files Browse the repository at this point in the history
Assorted code cleanups in the library source
  • Loading branch information
witnessmenow authored Mar 6, 2017
2 parents ee9eb09 + 5d50830 commit 13c1533
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 74 deletions.
135 changes: 66 additions & 69 deletions src/YoutubeApi.cpp
Original file line number Diff line number Diff line change
@@ -1,72 +1,72 @@
/*
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;
long now;
String body="";
bool finishedHeaders = false;
bool currentLineIsBlank = true;
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;
int ch_count=0;
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);

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;
}
Expand All @@ -79,34 +79,31 @@ String YoutubeApi::sendGetToYoutube(String command) {
}
}

//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://" 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);
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;
}
8 changes: 3 additions & 5 deletions src/YoutubeApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include <ArduinoJson.h>
#include <Client.h>

#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{
Expand Down

0 comments on commit 13c1533

Please sign in to comment.