Skip to content

Commit

Permalink
Added support for ESP32
Browse files Browse the repository at this point in the history
  • Loading branch information
witnessmenow committed Mar 12, 2018
1 parent e295021 commit 1610fb6
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 9 deletions.
78 changes: 78 additions & 0 deletions examples/ESP32/ChannelStatistics/ChannelStatistics.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*******************************************************************
* Read YouTube Channel statistics from the YouTube API using an
* ESP32
*
* By Brian Lough
* https://www.youtube.com/channel/UCezJOfu7OtqGzd5xrP3q6WA
*******************************************************************/

#include <YoutubeApi.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>

#include <ArduinoJson.h> // This Sketch doesn't technically need this, but the library does so it must be installed.

//------- Replace the following! ------
char ssid[] = "ssid"; // your network SSID (name)
char password[] = "password"; // your network key
#define API_KEY "ENTER_YOUR_API_KEY" // your google apps API Token
#define CHANNEL_ID "UCezJOfu7OtqGzd5xrP3q6WA" // makes up the url of channel


WiFiClientSecure client;
YoutubeApi api(API_KEY, client);

unsigned long api_mtbs = 60000; //mean time between api requests
unsigned long api_lasttime; //last time api request has been done

long subs = 0;

void setup() {

Serial.begin(115200);

// Attempt to connect to Wifi network:
Serial.print("Connecting Wifi: ");
Serial.println(ssid);

/* Explicitly set the ESP32 to be a WiFi-client, otherwise, it by default,
would try to act as both a client and an access-point and could cause
network-issues with your other WiFi-devices on your WiFi-network. */
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
IPAddress ip = WiFi.localIP();
Serial.println(ip);


}

void loop() {

if (millis() - api_lasttime > api_mtbs) {
if(api.getChannelStatistics(CHANNEL_ID))
{
Serial.println("---------Stats---------");
Serial.print("Subscriber Count: ");
Serial.println(api.channelStats.subscriberCount);
Serial.print("View Count: ");
Serial.println(api.channelStats.viewCount);
Serial.print("Comment Count: ");
Serial.println(api.channelStats.commentCount);
Serial.print("Video Count: ");
Serial.println(api.channelStats.videoCount);
// Probably not needed :)
//Serial.print("hiddenSubscriberCount: ");
//Serial.println(api.channelStats.hiddenSubscriberCount);
Serial.println("------------------------");

}
api_lasttime = millis();
}
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=YoutubeApi
version=1.0.0
version=1.1.0
author=Brian Lough
maintainer=Brian Lough <[email protected]>
sentence=A wrapper for the YouTube API for Arduino (supports ESP8266 & WiFi101 boards)
Expand Down
28 changes: 20 additions & 8 deletions src/YoutubeApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@ String YoutubeApi::sendGetToYoutube(String command) {
bool avail;
// Connect with youtube api over ssl
if (client->connect(YTAPI_HOST, YTAPI_SSL_PORT)) {
// Serial.println(".... connected to server");
if(_debug) { Serial.println(".... connected to server"); }
String a="";
char c;
int ch_count=0;
client->println("GET "+command+"&key="+_apiKey);
client->println("GET " + command + "&key=" + _apiKey + " HTTP/1.1");
client->print("HOST: ");
client->println(YTAPI_HOST);
client->println();
now=millis();
avail=false;
while (millis() - now < YTAPI_TIMEOUT) {
Expand Down Expand Up @@ -72,20 +75,22 @@ String YoutubeApi::sendGetToYoutube(String command) {
}
}
if (avail) {
//Serial.println("Body:");
//Serial.println(body);
//Serial.println("END");
if(_debug) {
Serial.println("Body:");
Serial.println(body);
Serial.println("END");
}
break;
}
}
}

closeClient();
return body;
}

bool YoutubeApi::getChannelStatistics(String channelId){
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
//Serial.println(command);
String command="/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
if(_debug) { Serial.println(F("Closing client")); }
String response = sendGetToYoutube(command); //recieve reply from youtube
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(response);
Expand All @@ -109,3 +114,10 @@ bool YoutubeApi::getChannelStatistics(String channelId){

return false;
}

void YoutubeApi::closeClient() {
if(client->connected()) {
if(_debug) { Serial.println(F("Closing client")); }
client->stop();
}
}
2 changes: 2 additions & 0 deletions src/YoutubeApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ class YoutubeApi
String sendGetToYoutube(String command);
bool getChannelStatistics(String channelId);
channelStatistics channelStats;
bool _debug = false;

private:
String _apiKey;
Client *client;
const int maxMessageLength = 1000;
bool checkForOkResponse(String response);
void closeClient();
};

#endif

0 comments on commit 1610fb6

Please sign in to comment.