Skip to content

Commit 6cf38b3

Browse files
committed
getting channel statistics working
1 parent 070a3e4 commit 6cf38b3

File tree

8 files changed

+663
-1
lines changed

8 files changed

+663
-1
lines changed

.gitattributes

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto
3+
4+
# Custom for Visual Studio
5+
*.cs diff=csharp
6+
7+
# Standard to msysgit
8+
*.doc diff=astextplain
9+
*.DOC diff=astextplain
10+
*.docx diff=astextplain
11+
*.DOCX diff=astextplain
12+
*.dot diff=astextplain
13+
*.DOT diff=astextplain
14+
*.pdf diff=astextplain
15+
*.PDF diff=astextplain
16+
*.rtf diff=astextplain
17+
*.RTF diff=astextplain

.gitignore

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Compiled Object files
2+
*.slo
3+
*.lo
4+
*.o
5+
*.obj
6+
7+
# Precompiled Headers
8+
*.gch
9+
*.pch
10+
11+
# Compiled Dynamic libraries
12+
*.so
13+
*.dylib
14+
*.dll
15+
16+
# Fortran module files
17+
*.mod
18+
19+
# Compiled Static libraries
20+
*.lai
21+
*.la
22+
*.a
23+
*.lib
24+
25+
# Executables
26+
*.exe
27+
*.out
28+
*.app

LICENSE.md

Lines changed: 339 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
11
# arduino-youtube-api
2-
A wrapper around the youtube api for arduino
2+
A wrapper for the YouTube API for Arduino (works on ESP8266)
3+
4+
Currently the only implemented method is getting the channel statistics but it can be easily extended.
5+
6+
![Imgur](http://i.imgur.com/FmXyW4E.png)
7+
8+
## Getting a Google Apps API key (Required!)
9+
10+
* Create an application [here](https://console.developers.google.com)
11+
* On the API Manager section, go to "Credentials" and create a new API key
12+
* Enable your application to communicate the YouTube Api [here](https://console.developers.google.com/apis/api/youtube)
13+
* Make sure the following URL works for you in your browser (Change the key at the end!):
14+
https://www.googleapis.com/youtube/v3/channels?part=statistics&id=UCu7_D0o48KbfhpEohoP7YSQ&key=PutYourNewlyGeneratedKeyHere
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <YoutubeApi.h>
2+
3+
/*******************************************************************
4+
* An example of bot that echos back any messages received *
5+
* *
6+
* written by Giacarlo Bacchio (Gianbacchio on Github) *
7+
* adapted by Brian Lough *
8+
*******************************************************************/
9+
10+
11+
#include <ESP8266WiFi.h>
12+
#include <WiFiClientSecure.h>
13+
14+
//------- Replace the following! ------
15+
char ssid[] = "xxx"; // your network SSID (name)
16+
char password[] = "yyyy"; // your network key
17+
#define API_KEY "zzzz" // your google apps API Token
18+
#define CHANNEL_ID "UCu7_D0o48KbfhpEohoP7YSQ" // makes up the url of channel
19+
20+
21+
22+
23+
WiFiClientSecure client;
24+
YoutubeApi api(API_KEY, client);
25+
26+
int api_mtbs = 60000; //mean time between api requests
27+
long api_lasttime; //last time api request has been done
28+
29+
long subs = 0;
30+
31+
void setup() {
32+
33+
Serial.begin(115200);
34+
35+
// Set WiFi to station mode and disconnect from an AP if it was Previously
36+
// connected
37+
WiFi.mode(WIFI_STA);
38+
WiFi.disconnect();
39+
delay(100);
40+
41+
// Attempt to connect to Wifi network:
42+
Serial.print("Connecting Wifi: ");
43+
Serial.println(ssid);
44+
while (WiFi.begin(ssid, password) != WL_CONNECTED) {
45+
Serial.print(".");
46+
delay(500);
47+
}
48+
Serial.println("");
49+
Serial.println("WiFi connected");
50+
Serial.println("IP address: ");
51+
IPAddress ip = WiFi.localIP();
52+
Serial.println(ip);
53+
54+
55+
}
56+
57+
void loop() {
58+
59+
if (millis() > api_lasttime + api_mtbs) {
60+
if(api.getChannelStatistics(CHANNEL_ID))
61+
{
62+
Serial.println("---------Stats---------");
63+
Serial.print("Subscriber Count: ");
64+
Serial.println(api.channelStats.subscriberCount);
65+
Serial.print("View Count: ");
66+
Serial.println(api.channelStats.viewCount);
67+
Serial.print("Comment Count: ");
68+
Serial.println(api.channelStats.commentCount);
69+
Serial.print("Video Count: ");
70+
Serial.println(api.channelStats.videoCount);
71+
// Probably not needed :)
72+
//Serial.print("hiddenSubscriberCount: ");
73+
//Serial.println(api.channelStats.hiddenSubscriberCount);
74+
Serial.println("------------------------");
75+
76+
}
77+
api_lasttime = millis();
78+
}
79+
}

library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=YoutubeApi
2+
version=1.0
3+
author=Brian Lough
4+
maintainer=Brian Lough <[email protected]>
5+
sentence=A wrapper for the YouTube API for Arduino (works on ESP8266)
6+
paragraph=A wrapper for the YouTube API for Arduino (works on ESP8266)
7+
category=Communication
8+
url=https://github.com/witnessmenow/arduino-youtube-api
9+
architectures=*

src/YoutubeApi.cpp

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
2+
/*
3+
Copyright (c) 2015 Giancarlo Bacchio. All right reserved.
4+
5+
TelegramBot - Library to create your own Telegram Bot using
6+
ESP8266 on Arduino IDE.
7+
Ref. Library at https:github/esp8266/Arduino
8+
9+
This library is free software; you can redistribute it and/or
10+
modify it under the terms of the GNU Lesser General Public
11+
License as published by the Free Software Foundation; either
12+
version 2.1 of the License, or (at your option) any later version.
13+
14+
This library is distributed in the hope that it will be useful,
15+
but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17+
Lesser General Public License for more details.
18+
19+
You should have received a copy of the GNU Lesser General Public
20+
License along with this library; if not, write to the Free Software
21+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22+
*/
23+
24+
25+
#include "YoutubeApi.h"
26+
27+
YoutubeApi::YoutubeApi(String apiKey, Client &client) {
28+
_apiKey = apiKey;
29+
this->client = &client;
30+
}
31+
32+
String YoutubeApi::sendGetToYoutube(String command) {
33+
String headers="";
34+
String body="";
35+
bool finishedHeaders = false;
36+
bool currentLineIsBlank = true;
37+
long now;
38+
bool avail;
39+
// Connect with api.telegram.org
40+
if (client->connect(HOST, SSL_PORT)) {
41+
// Serial.println(".... connected to server");
42+
String a="";
43+
char c;
44+
int ch_count=0;
45+
client->println("GET "+command+"&key="+_apiKey);
46+
now=millis();
47+
avail=false;
48+
while (millis()-now<1500) {
49+
while (client->available()) {
50+
char c = client->read();
51+
//Serial.write(c);
52+
53+
if(!finishedHeaders){
54+
if (currentLineIsBlank && c == '\n') {
55+
finishedHeaders = true;
56+
}
57+
else{
58+
headers = headers + c;
59+
60+
}
61+
} else {
62+
if (ch_count < maxMessageLength) {
63+
body=body+c;
64+
ch_count++;
65+
}
66+
}
67+
68+
if (c == '\n') {
69+
currentLineIsBlank = true;
70+
}else if (c != '\r') {
71+
currentLineIsBlank = false;
72+
}
73+
74+
avail=true;
75+
}
76+
if (avail) {
77+
//Serial.println("Body:");
78+
//Serial.println(body);
79+
//Serial.println("END");
80+
break;
81+
}
82+
}
83+
}
84+
85+
//int lastCharIndex = body.lastIndexOf("}");
86+
87+
//return body.substring(0,lastCharIndex+1);
88+
return body;
89+
}
90+
91+
bool YoutubeApi::getChannelStatistics(String channelId){
92+
String command="https://www.googleapis.com/youtube/v3/channels?part=statistics&id="+channelId;
93+
String response = sendGetToYoutube(command); //recieve reply from telegram.org
94+
DynamicJsonBuffer jsonBuffer;
95+
JsonObject& root = jsonBuffer.parseObject(response);
96+
if(root.success()) {
97+
if (root.containsKey("items")) {
98+
long subscriberCount = root["items"][0]["statistics"]["subscriberCount"];
99+
long viewCount = root["items"][0]["statistics"]["viewCount"];
100+
long commentCount = root["items"][0]["statistics"]["commentCount"];
101+
long hiddenSubscriberCount = root["items"][0]["statistics"]["hiddenSubscriberCount"];
102+
long videoCount = root["items"][0]["statistics"]["videoCount"];
103+
104+
channelStats.viewCount = viewCount;
105+
channelStats.subscriberCount = subscriberCount;
106+
channelStats.commentCount = commentCount;
107+
channelStats.hiddenSubscriberCount = hiddenSubscriberCount;
108+
channelStats.videoCount = videoCount;
109+
110+
return true;
111+
}
112+
}
113+
114+
return false;
115+
}

src/YoutubeApi.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Copyright (c) 2015 Giancarlo Bacchio. All right reserved.
3+
4+
TelegramBot - Library to create your own Telegram Bot using
5+
ESP8266 on Arduino IDE.
6+
Ref. Library at https:github/esp8266/Arduino
7+
8+
This library is free software; you can redistribute it and/or
9+
modify it under the terms of the GNU Lesser General Public
10+
License as published by the Free Software Foundation; either
11+
version 2.1 of the License, or (at your option) any later version.
12+
13+
This library is distributed in the hope that it will be useful,
14+
but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
Lesser General Public License for more details.
17+
18+
You should have received a copy of the GNU Lesser General Public
19+
License along with this library; if not, write to the Free Software
20+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21+
*/
22+
23+
24+
#ifndef YoutubeApi_h
25+
#define YoutubeApi_h
26+
27+
#include <Arduino.h>
28+
#include <ArduinoJson.h>
29+
#include <Client.h>
30+
31+
#define HOST "www.googleapis.com"
32+
#define SSL_PORT 443
33+
#define HANDLE_MESSAGES 1
34+
#define MAX_BUFFER_SIZE 1250
35+
36+
37+
38+
struct channelStatistics{
39+
long viewCount;
40+
long commentCount;
41+
long subscriberCount;
42+
bool hiddenSubscriberCount;
43+
long videoCount;
44+
};
45+
46+
class YoutubeApi
47+
{
48+
public:
49+
YoutubeApi (String apiKey, Client &client);
50+
String sendGetToYoutube(String command);
51+
bool getChannelStatistics(String channelId);
52+
channelStatistics channelStats;
53+
const char* fingerprint = "3F:AE:17:DA:03:65:45:BE:CE:77:2F:8A:DC:5B:C9:08:98:7E:9E:E7"; //Telegram.org Certificate
54+
55+
private:
56+
//JsonObject * parseUpdates(String response);
57+
String _apiKey;
58+
Client *client;
59+
const int maxMessageLength = 1000;
60+
bool checkForOkResponse(String response);
61+
};
62+
63+
#endif

0 commit comments

Comments
 (0)