|
1 | 1 | /*
|
2 |
| - Copyright (c) 2017 Brian Lough. All right reserved. |
| 2 | + Copyright (c) 2020 Brian Lough. All right reserved. |
3 | 3 |
|
4 | 4 | YoutubeApi - An Arduino wrapper for the YouTube API
|
5 | 5 |
|
|
22 | 22 | #include "YoutubeApi.h"
|
23 | 23 |
|
24 | 24 | YoutubeApi::YoutubeApi(String apiKey, Client &client) {
|
| 25 | + int strLen = apiKey.length() + 1; |
| 26 | + char tempStr[strLen]; |
| 27 | + apiKey.toCharArray(tempStr, strLen); |
| 28 | + |
| 29 | + YoutubeApi(tempStr, client); |
| 30 | +} |
| 31 | + |
| 32 | +YoutubeApi::YoutubeApi(char *apiKey, Client &client) { |
25 | 33 | _apiKey = apiKey;
|
26 | 34 | this->client = &client;
|
27 | 35 | }
|
28 | 36 |
|
29 |
| -String YoutubeApi::sendGetToYoutube(String command) { |
30 |
| - String headers=""; |
31 |
| - String body=""; |
32 |
| - bool finishedHeaders = false; |
33 |
| - bool currentLineIsBlank = true; |
34 |
| - unsigned long now; |
35 |
| - bool avail; |
36 |
| - // Connect with youtube api over ssl |
37 |
| - if (client->connect(YTAPI_HOST, YTAPI_SSL_PORT)) { |
38 |
| - if(_debug) { Serial.println(".... connected to server"); } |
39 |
| - String a=""; |
40 |
| - char c; |
41 |
| - int ch_count=0; |
42 |
| - client->println("GET " + command + "&key=" + _apiKey + " HTTP/1.1"); |
43 |
| - client->print("HOST: "); |
44 |
| - client->println(YTAPI_HOST); |
45 |
| - client->println(); |
46 |
| - now=millis(); |
47 |
| - avail=false; |
48 |
| - while (millis() - now < YTAPI_TIMEOUT) { |
49 |
| - while (client->available()) { |
50 |
| - |
51 |
| - // Allow body to be parsed before finishing |
52 |
| - avail = finishedHeaders; |
53 |
| - char c = client->read(); |
54 |
| - //Serial.write(c); |
55 |
| - |
56 |
| - if(!finishedHeaders){ |
57 |
| - if (currentLineIsBlank && c == '\n') { |
58 |
| - finishedHeaders = true; |
59 |
| - } |
60 |
| - else{ |
61 |
| - headers = headers + c; |
62 |
| - |
63 |
| - } |
64 |
| - } else { |
65 |
| - if (ch_count < maxMessageLength) { |
66 |
| - body=body+c; |
67 |
| - ch_count++; |
68 |
| - } |
69 |
| - } |
70 |
| - |
71 |
| - if (c == '\n') { |
72 |
| - currentLineIsBlank = true; |
73 |
| - }else if (c != '\r') { |
74 |
| - currentLineIsBlank = false; |
75 |
| - } |
76 |
| - } |
77 |
| - if (avail) { |
78 |
| - if(_debug) { |
79 |
| - Serial.println("Body:"); |
80 |
| - Serial.println(body); |
81 |
| - Serial.println("END"); |
82 |
| - } |
83 |
| - break; |
84 |
| - } |
85 |
| - } |
86 |
| - } |
87 |
| - closeClient(); |
88 |
| - return body; |
| 37 | +int YoutubeApi::sendGetToYoutube(char *command) { |
| 38 | + client->flush(); |
| 39 | + client->setTimeout(YTAPI_TIMEOUT); |
| 40 | + if (!client->connect(YTAPI_HOST, YTAPI_SSL_PORT)) |
| 41 | + { |
| 42 | + Serial.println(F("Connection failed")); |
| 43 | + return false; |
| 44 | + } |
| 45 | + // give the esp a breather |
| 46 | + yield(); |
| 47 | + |
| 48 | + // Send HTTP request |
| 49 | + client->print(F("GET ")); |
| 50 | + client->print(command); |
| 51 | + client->println(F(" HTTP/1.1")); |
| 52 | + |
| 53 | + //Headers |
| 54 | + client->print(F("Host: ")); |
| 55 | + client->println(YTAPI_HOST); |
| 56 | + |
| 57 | + client->println(F("Cache-Control: no-cache")); |
| 58 | + |
| 59 | + if (client->println() == 0) |
| 60 | + { |
| 61 | + Serial.println(F("Failed to send request")); |
| 62 | + return -1; |
| 63 | + } |
| 64 | + |
| 65 | + int statusCode = getHttpStatusCode(); |
| 66 | + |
| 67 | + // Let the caller of this method parse the JSon from the client |
| 68 | + skipHeaders(); |
| 69 | + return statusCode; |
89 | 70 | }
|
90 | 71 |
|
91 | 72 | bool YoutubeApi::getChannelStatistics(String channelId){
|
92 |
| - 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 |
93 |
| - if(_debug) { Serial.println(F("Closing client")); } |
94 |
| - String response = sendGetToYoutube(command); //recieve reply from youtube |
95 |
| - DynamicJsonBuffer jsonBuffer; |
96 |
| - JsonObject& root = jsonBuffer.parseObject(response); |
97 |
| - if(root.success()) { |
98 |
| - if (root.containsKey("items")) { |
99 |
| - long subscriberCount = root["items"][0]["statistics"]["subscriberCount"]; |
100 |
| - long viewCount = root["items"][0]["statistics"]["viewCount"]; |
101 |
| - long commentCount = root["items"][0]["statistics"]["commentCount"]; |
102 |
| - long hiddenSubscriberCount = root["items"][0]["statistics"]["hiddenSubscriberCount"]; |
103 |
| - long videoCount = root["items"][0]["statistics"]["videoCount"]; |
104 |
| - |
105 |
| - channelStats.viewCount = viewCount; |
106 |
| - channelStats.subscriberCount = subscriberCount; |
107 |
| - channelStats.commentCount = commentCount; |
108 |
| - channelStats.hiddenSubscriberCount = hiddenSubscriberCount; |
109 |
| - channelStats.videoCount = videoCount; |
110 |
| - |
111 |
| - return true; |
112 |
| - } |
113 |
| - } |
114 | 73 |
|
115 |
| - return false; |
| 74 | + int strLen = channelId.length() + 1; |
| 75 | + char tempStr[strLen]; |
| 76 | + channelId.toCharArray(tempStr, strLen); |
| 77 | + |
| 78 | + return getChannelStatistics(tempStr); |
| 79 | +} |
| 80 | + |
| 81 | +bool YoutubeApi::getChannelStatistics(char *channelId){ |
| 82 | + char command[150] = YTAPI_CHANNEL_ENDPOINT; |
| 83 | + strcat(command, "?part=statistics&id=%s"); |
| 84 | + sprintf(command, command, channelId); |
| 85 | + |
| 86 | + if (_debug) |
| 87 | + { |
| 88 | + Serial.println(command); |
| 89 | + } |
| 90 | + |
| 91 | + bool hasError = true; |
| 92 | + |
| 93 | + // Get from https://arduinojson.org/v6/assistant/ |
| 94 | + const size_t bufferSize = JSON_ARRAY_SIZE(1) |
| 95 | + + JSON_OBJECT_SIZE(2) |
| 96 | + + 2*JSON_OBJECT_SIZE(4) |
| 97 | + + JSON_OBJECT_SIZE(5); |
| 98 | + |
| 99 | + if (sendGetToYoutube(command) == 200) |
| 100 | + { |
| 101 | + // Allocate DynamicJsonDocument |
| 102 | + DynamicJsonDocument doc(bufferSize); |
| 103 | + |
| 104 | + // Parse JSON object |
| 105 | + DeserializationError error = deserializeJson(doc, *client); |
| 106 | + if (!error) |
| 107 | + { |
| 108 | + hasError = false; |
| 109 | + |
| 110 | + JsonObject itemStatistics = doc["items"][0]["statistics"]; |
| 111 | + |
| 112 | + channelStats.viewCount = itemStatistics["viewCount"].as<long>(); |
| 113 | + channelStats.subscriberCount = itemStatistics["subscriberCount"].as<long>(); |
| 114 | + channelStats.commentCount = itemStatistics["commentCount"].as<long>(); |
| 115 | + channelStats.hiddenSubscriberCount = itemStatistics["hiddenSubscriberCount"].as<bool>(); |
| 116 | + channelStats.videoCount = itemStatistics["videoCount"].as<long>(); |
| 117 | + } |
| 118 | + else |
| 119 | + { |
| 120 | + Serial.print(F("deserializeJson() failed with code ")); |
| 121 | + Serial.println(error.c_str()); |
| 122 | + } |
| 123 | + } |
| 124 | + closeClient(); |
| 125 | + |
| 126 | + return hasError; |
| 127 | +} |
| 128 | + |
| 129 | +void YoutubeApi::skipHeaders() |
| 130 | +{ |
| 131 | + // Skip HTTP headers |
| 132 | + char endOfHeaders[] = "\r\n\r\n"; |
| 133 | + if (!client->find(endOfHeaders)) |
| 134 | + { |
| 135 | + Serial.println(F("Invalid response")); |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + // Was getting stray characters between the headers and the body |
| 140 | + // This should toss them away |
| 141 | + while (client->available() && client->peek() != '{') |
| 142 | + { |
| 143 | + char c = 0; |
| 144 | + client->readBytes(&c, 1); |
| 145 | + if (_debug) |
| 146 | + { |
| 147 | + Serial.print("Tossing an unexpected character: "); |
| 148 | + Serial.println(c); |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +int YoutubeApi::getHttpStatusCode() |
| 154 | +{ |
| 155 | + // Check HTTP status |
| 156 | + if(client->find("HTTP/1.1")){ |
| 157 | + int statusCode = client->parseInt(); |
| 158 | + return statusCode; |
| 159 | + } |
| 160 | + |
| 161 | + return -1; |
116 | 162 | }
|
117 | 163 |
|
118 | 164 | void YoutubeApi::closeClient() {
|
|
0 commit comments