Skip to content

Commit

Permalink
Merge pull request #23 from witnessmenow/V2
Browse files Browse the repository at this point in the history
V2
  • Loading branch information
witnessmenow authored May 20, 2020
2 parents 1610fb6 + 89bec62 commit c62dff8
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 508 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@
.piolibdeps
.clang_complete
.gcc-flags.json
.pio/
.vscode/
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ Currently the only implemented method is getting the channel statistics but it c

![Imgur](http://i.imgur.com/FmXyW4E.png)

### Support what I do!

I have created a lot of different Arduino libraries that I hope people can make use of. [If you enjoy my work, please consider becoming a Github sponsor!](https://github.com/sponsors/witnessmenow/)

## Getting a Google Apps API key (Required!)

* Create an application [here](https://console.developers.google.com)
* On the API Manager section, go to "Credentials" and create a new API key
* Enable your application to communicate the YouTube Api [here](https://console.developers.google.com/apis/api/youtube)
* Make sure the following URL works for you in your browser (Change the key at the end!):
https://www.googleapis.com/youtube/v3/channels?part=statistics&id=UCu7_D0o48KbfhpEohoP7YSQ&key=PutYourNewlyGeneratedKeyHere
https://www.googleapis.com/youtube/v3/channels?part=statistics&id=UCezJOfu7OtqGzd5xrP3q6WA&key=PutYourNewlyGeneratedKeyHere

## Installing

The downloaded code can be included as a new library into the IDE selecting the menu:

Sketch / include Library / Add .Zip library
The easiest way to install this library is through the aduino library manager, just search for "Youtube"

You also have to install the ArduinoJson library written by [Benoît Blanchon](https://github.com/bblanchon). Search for it on the Arduino Library manager or get it from [here](https://github.com/bblanchon/ArduinoJson).

Expand Down
56 changes: 41 additions & 15 deletions examples/ESP32/ChannelStatistics/ChannelStatistics.ino
Original file line number Diff line number Diff line change
@@ -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 <YoutubeApi.h>
// ----------------------------
// Standard Libraries
// ----------------------------

#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.
// ----------------------------
// Additional Libraries - each one of these will need to be installed.
// ----------------------------

#include <YoutubeApi.h>
// Library for connecting to the Youtube API

// Search for "youtube" in the Arduino Library Manager
// https://github.com/witnessmenow/arduino-youtube-api

#include <ArduinoJson.h>
// 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;

Expand Down Expand Up @@ -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---------");
Expand All @@ -73,6 +99,6 @@ void loop() {
Serial.println("------------------------");

}
api_lasttime = millis();
nextRunTime = millis() + timeBetweenRequests;
}
}
54 changes: 43 additions & 11 deletions examples/ESP8266/ChannelStatistics/ChannelStatistics.ino
Original file line number Diff line number Diff line change
@@ -1,28 +1,56 @@
/*******************************************************************
* Read YouTube Channel statistics from the YouTube API *
* *
* By Brian Lough *
* https://www.youtube.com/channel/UCezJOfu7OtqGzd5xrP3q6WA *
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
* = Affilate
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 <YoutubeApi.h>
// ----------------------------
// Standard Libraries
// ----------------------------

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>

#include <ArduinoJson.h> // 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 <YoutubeApi.h>
// Library for connecting to the Youtube API

// Search for "youtube" in the Arduino Library Manager
// https://github.com/witnessmenow/arduino-youtube-api

#include <ArduinoJson.h>
// 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[] = "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;

Expand Down Expand Up @@ -50,12 +78,16 @@ void setup() {
IPAddress ip = WiFi.localIP();
Serial.println(ip);

// 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() {

if (millis() - api_lasttime > api_mtbs) {
if (millis() > nextRunTime) {
if(api.getChannelStatistics(CHANNEL_ID))
{
Serial.println("---------Stats---------");
Expand All @@ -73,6 +105,6 @@ void loop() {
Serial.println("------------------------");

}
api_lasttime = millis();
nextRunTime = millis() + timeBetweenRequests;
}
}

This file was deleted.

Loading

0 comments on commit c62dff8

Please sign in to comment.