diff --git a/examples/ESP32/ChannelStatistics/ChannelStatistics.ino b/examples/ESP32/ChannelStatistics/ChannelStatistics.ino index 11b3477..4534862 100644 --- a/examples/ESP32/ChannelStatistics/ChannelStatistics.ino +++ b/examples/ESP32/ChannelStatistics/ChannelStatistics.ino @@ -1,29 +1,55 @@ /******************************************************************* - * Read YouTube Channel statistics from the YouTube API using an - * ESP32 - * - * By Brian Lough - * https://www.youtube.com/channel/UCezJOfu7OtqGzd5xrP3q6WA + Read YouTube Channel statistics from the YouTube API on + an ESP32 and print them to the serial monitor + + Parts: + Any ESP32 board + + If you find what I do useful and would like to support me, + please consider becoming a sponsor on Github + https://github.com/sponsors/witnessmenow/ + + Written by Brian Lough + YouTube: https://www.youtube.com/brianlough + Tindie: https://www.tindie.com/stores/brianlough/ + Twitter: https://twitter.com/witnessmenow *******************************************************************/ -#include +// ---------------------------- +// Standard Libraries +// ---------------------------- + #include #include -#include // This Sketch doesn't technically need this, but the library does so it must be installed. +// ---------------------------- +// Additional Libraries - each one of these will need to be installed. +// ---------------------------- + +#include +// Library for connecting to the Youtube API + +// Search for "youtube" in the Arduino Library Manager +// https://github.com/witnessmenow/arduino-youtube-api + +#include +// Library used for parsing Json from the API responses + +// Search for "Arduino Json" in the Arduino Library manager +// https://github.com/bblanchon/ArduinoJson //------- 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 +char ssid[] = "xxx"; // your network SSID (name) +char password[] = "yyyy"; // your network key +#define API_KEY "zzzz" // 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 +unsigned long timeBetweenRequests = 60000; +unsigned long nextRunTime; long subs = 0; @@ -55,7 +81,7 @@ void setup() { void loop() { - if (millis() - api_lasttime > api_mtbs) { + if (millis() > nextRunTime) { if(api.getChannelStatistics(CHANNEL_ID)) { Serial.println("---------Stats---------"); @@ -73,6 +99,6 @@ void loop() { Serial.println("------------------------"); } - api_lasttime = millis(); + nextRunTime = millis() + timeBetweenRequests; } } diff --git a/examples/ESP8266/ChannelStatistics/ChannelStatistics.ino b/examples/ESP8266/ChannelStatistics/ChannelStatistics.ino index b6447d9..3b7d4c6 100644 --- a/examples/ESP8266/ChannelStatistics/ChannelStatistics.ino +++ b/examples/ESP8266/ChannelStatistics/ChannelStatistics.ino @@ -1,5 +1,6 @@ /******************************************************************* - Read YouTube Channel statistics from the YouTube API + Read YouTube Channel statistics from the YouTube API on + an ESP8266 and print them to the serial monitor Parts: D1 Mini ESP8266 (or any ESP8266) * - http://s.click.aliexpress.com/e/uzFUnIe @@ -79,6 +80,9 @@ void setup() { // Required if you are using ESP8266 V2.5 or above client.setInsecure(); + + // If you want to enable some extra debugging + api._debug = true; } void loop() { diff --git a/examples/ESP8266/ChannelStatisticsWithWifiManager/ChannelStatisticsWithWifiManager.ino b/examples/ESP8266/ChannelStatisticsWithWifiManager/ChannelStatisticsWithWifiManager.ino deleted file mode 100644 index 5b95752..0000000 --- a/examples/ESP8266/ChannelStatisticsWithWifiManager/ChannelStatisticsWithWifiManager.ino +++ /dev/null @@ -1,174 +0,0 @@ -/******************************************************************* - * Read YouTube Channel statistics from the YouTube API * - * This sketch uses the WiFiManager Library for configuraiton * - * * - * By Brian Lough * - * https://www.youtube.com/channel/UCezJOfu7OtqGzd5xrP3q6WA * - *******************************************************************/ - -#include -#include -#include - -// For storing configurations -#include "FS.h" -#include - -// WiFiManager Libraries -#include //Local DNS Server used for redirecting all rs to the configuration portal -#include //Local WebServer used to serve the configuration portal -#include //https://github.com/tzapu/WiFiManager WiFi Configuration Magic - -const int resetConfigPin = D8; //When high will reset the wifi manager config - -char apiKey[45] = ""; -char channelId[30] = "UCezJOfu7OtqGzd5xrP3q6WA"; - -WiFiClientSecure client; -YoutubeApi *api; - -unsigned long api_mtbs = 60000; //mean time between api requests -unsigned long api_lasttime; //last time api request has been done - -long subs = 0; - -// flag for saving data -bool shouldSaveConfig = false; - -//callback notifying us of the need to save config -void saveConfigCallback () { - Serial.println("Should save config"); - shouldSaveConfig = true; -} - -void setup() { - - Serial.begin(115200); - - if (!SPIFFS.begin()) { - Serial.println("Failed to mount FS"); - return; - } - - pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output - digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level - loadConfig(); - - WiFiManager wifiManager; - wifiManager.setSaveConfigCallback(saveConfigCallback); - - // Adding an additional config on the WIFI manager webpage for the API Key and Channel ID - WiFiManagerParameter customApiKey("apiKey", "API Key", apiKey, 50); - WiFiManagerParameter customChannelId("channelId", "Channel ID", channelId, 35); - wifiManager.addParameter(&customApiKey); - wifiManager.addParameter(&customChannelId); - - // If it fails to connect it will create a YouTube-Counter access point - wifiManager.autoConnect("YouTube-Counter", "supersecret"); - - strcpy(apiKey, customApiKey.getValue()); - strcpy(channelId, customChannelId.getValue()); - - if (shouldSaveConfig) { - saveConfig(); - } - - digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH - // Force Config mode if there is no API key - if(strcmp(apiKey, "") > 0) { - Serial.println("Init YouTube API"); - api = new YoutubeApi(apiKey, client); - } else { - Serial.println("Forcing Config Mode"); - forceConfigMode(); - } - Serial.println(""); - Serial.println("WiFi connected"); - Serial.println("IP address: "); - IPAddress ip = WiFi.localIP(); - Serial.println(ip); - -} - -bool loadConfig() { - File configFile = SPIFFS.open("/config.json", "r"); - if (!configFile) { - Serial.println("Failed to open config file"); - return false; - } - - size_t size = configFile.size(); - if (size > 1024) { - Serial.println("Config file size is too large"); - return false; - } - - // Allocate a buffer to store contents of the file. - std::unique_ptr buf(new char[size]); - - configFile.readBytes(buf.get(), size); - - StaticJsonBuffer<200> jsonBuffer; - JsonObject& json = jsonBuffer.parseObject(buf.get()); - - if (!json.success()) { - Serial.println("Failed to parse config file"); - return false; - } - - strcpy(apiKey, json["apiKey"]); - strcpy(channelId, json["channelId"]); - return true; -} - -bool saveConfig() { - StaticJsonBuffer<200> jsonBuffer; - JsonObject& json = jsonBuffer.createObject(); - json["apiKey"] = apiKey; - json["channelId"] = channelId; - - File configFile = SPIFFS.open("/config.json", "w"); - if (!configFile) { - Serial.println("Failed to open config file for writing"); - return false; - } - - json.printTo(configFile); - return true; -} - -void forceConfigMode() { - Serial.println("Reset"); - WiFi.disconnect(); - Serial.println("Dq"); - delay(500); - ESP.restart(); - delay(5000); -} - -void loop() { - - if (digitalRead(resetConfigPin) == HIGH) { - forceConfigMode(); - } - if (millis() - api_lasttime > api_mtbs) { - if(api->getChannelStatistics(channelId)) - { - 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(); - } -} diff --git a/examples/ESP8266/ChannelStatisticsWithWifiManagerAndDoubleReset/ChannelStatisticsWithWifiManagerAndDoubleReset.ino b/examples/ESP8266/ChannelStatisticsWithWifiManagerAndDoubleReset/ChannelStatisticsWithWifiManagerAndDoubleReset.ino deleted file mode 100644 index 93c9c74..0000000 --- a/examples/ESP8266/ChannelStatisticsWithWifiManagerAndDoubleReset/ChannelStatisticsWithWifiManagerAndDoubleReset.ino +++ /dev/null @@ -1,213 +0,0 @@ -/******************************************************************* - * Read YouTube Channel statistics from the YouTube API * - * This sketch uses the WiFiManager Library for configuraiton * - * Using DoubleResetDetector to launch config mode * - * * - * By Brian Lough * - * https://www.youtube.com/channel/UCezJOfu7OtqGzd5xrP3q6WA * - *******************************************************************/ - -#include -#include -#include - - -#include -// For entering Config mode by pressing reset twice -// Available on the library manager (DoubleResetDetector) -// https://github.com/datacute/DoubleResetDetector - -#include -// Required for the YouTubeApi and used for the config file -// Available on the library manager (ArduinoJson) -// https://github.com/bblanchon/ArduinoJson - -#include -// For configuring the Wifi credentials without re-programing -// Availalbe on library manager (WiFiManager) -// https://github.com/tzapu/WiFiManager - -// For storing configurations -#include "FS.h" - -// Additional libraries needed by WiFiManager -#include //Local DNS Server used for redirecting all rs to the configuration portal -#include //Local WebServer used to serve the configuration portal - -char apiKey[45] = ""; -char channelId[30] = "UCezJOfu7OtqGzd5xrP3q6WA"; - -WiFiClientSecure client; -YoutubeApi *api; - -unsigned long api_mtbs = 60000; //mean time between api requests -unsigned long api_lasttime; //last time api request has been done - -long subs = 0; - -// flag for saving data -bool shouldSaveConfig = false; - -// Number of seconds after reset during which a -// subseqent reset will be considered a double reset. -// This sketch uses drd.stop() rather than relying on the timeout -#define DRD_TIMEOUT 10 - -// RTC Memory Address for the DoubleResetDetector to use -#define DRD_ADDRESS 0 - -DoubleResetDetector drd(DRD_TIMEOUT, DRD_ADDRESS); - -//callback notifying us of the need to save config -void saveConfigCallback () { - Serial.println("Should save config"); - shouldSaveConfig = true; -} - -void configModeCallback (WiFiManager *myWiFiManager) { - Serial.println("Entered config mode"); - Serial.println(WiFi.softAPIP()); - - // You could indicate on your screen or by an LED you are in config mode here - - // We don't want the next time the boar resets to be considered a double reset - // so we remove the flag - drd.stop(); -} - -void setup() { - - Serial.begin(115200); - - if (!SPIFFS.begin()) { - Serial.println("Failed to mount FS"); - return; - } - - pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output - digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level - loadConfig(); - - WiFiManager wifiManager; - wifiManager.setAPCallback(configModeCallback); - wifiManager.setSaveConfigCallback(saveConfigCallback); - - // Adding an additional config on the WIFI manager webpage for the API Key and Channel ID - WiFiManagerParameter customApiKey("apiKey", "API Key", apiKey, 50); - WiFiManagerParameter customChannelId("channelId", "Channel ID", channelId, 35); - wifiManager.addParameter(&customApiKey); - wifiManager.addParameter(&customChannelId); - - if (drd.detectDoubleReset()) { - Serial.println("Double Reset Detected"); - wifiManager.startConfigPortal("YouTube-Counter", "supersecret"); - } else { - Serial.println("No Double Reset Detected"); - wifiManager.autoConnect("YouTube-Counter", "supersecret"); - } - - strcpy(apiKey, customApiKey.getValue()); - strcpy(channelId, customChannelId.getValue()); - - if (shouldSaveConfig) { - saveConfig(); - } - - digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH - // Force Config mode if there is no API key - if(strcmp(apiKey, "") > 0) { - Serial.println("Init YouTube API"); - api = new YoutubeApi(apiKey, client); - } else { - Serial.println("Forcing Config Mode"); - forceConfigMode(); - } - Serial.println(""); - Serial.println("WiFi connected"); - Serial.println("IP address: "); - IPAddress ip = WiFi.localIP(); - Serial.println(ip); - - drd.stop(); - -} - -bool loadConfig() { - File configFile = SPIFFS.open("/config.json", "r"); - if (!configFile) { - Serial.println("Failed to open config file"); - return false; - } - - size_t size = configFile.size(); - if (size > 1024) { - Serial.println("Config file size is too large"); - return false; - } - - // Allocate a buffer to store contents of the file. - std::unique_ptr buf(new char[size]); - - configFile.readBytes(buf.get(), size); - - StaticJsonBuffer<200> jsonBuffer; - JsonObject& json = jsonBuffer.parseObject(buf.get()); - - if (!json.success()) { - Serial.println("Failed to parse config file"); - return false; - } - - strcpy(apiKey, json["apiKey"]); - strcpy(channelId, json["channelId"]); - return true; -} - -bool saveConfig() { - StaticJsonBuffer<200> jsonBuffer; - JsonObject& json = jsonBuffer.createObject(); - json["apiKey"] = apiKey; - json["channelId"] = channelId; - - File configFile = SPIFFS.open("/config.json", "w"); - if (!configFile) { - Serial.println("Failed to open config file for writing"); - return false; - } - - json.printTo(configFile); - return true; -} - -void forceConfigMode() { - Serial.println("Reset"); - WiFi.disconnect(); - Serial.println("Dq"); - delay(500); - ESP.restart(); - delay(5000); -} - -void loop() { - - if (millis() - api_lasttime > api_mtbs) { - if(api->getChannelStatistics(channelId)) - { - 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(); - } -} diff --git a/src/YoutubeApi.cpp b/src/YoutubeApi.cpp index 58c7ee5..acbe6df 100644 --- a/src/YoutubeApi.cpp +++ b/src/YoutubeApi.cpp @@ -71,7 +71,6 @@ int YoutubeApi::sendGetToYoutube(char *command) { bool YoutubeApi::getChannelStatistics(String channelId){ - Serial.println("Deprecated, user a char* rather than String"); int strLen = channelId.length() + 1; char tempStr[strLen]; channelId.toCharArray(tempStr, strLen); diff --git a/src/YoutubeApi.h b/src/YoutubeApi.h index a8d3b47..99e4b4e 100644 --- a/src/YoutubeApi.h +++ b/src/YoutubeApi.h @@ -1,5 +1,5 @@ /* -Copyright (c) 2017 Brian Lough. All right reserved. +Copyright (c) 2020 Brian Lough. All right reserved. YoutubeApi - An Arduino wrapper for the YouTube API @@ -18,16 +18,6 @@ License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#if defined(__GNUC__) || defined(__clang__) -#define DEPRECATED __attribute__((deprecated)) -#elif defined(_MSC_VER) -#define DEPRECATED __declspec(deprecated) -#else -#pragma message("WARNING: You need to implement DEPRECATED for this compiler") -#define DEPRECATED -#endif - - #ifndef YoutubeApi_h #define YoutubeApi_h