Paperd.Ink Library 0.0.5
Library for interacting with Paperd.Ink devices.
Loading...
Searching...
No Matches
youtube.cpp
Go to the documentation of this file.
1#include <WiFiClientSecure.h>
2#include <HTTPClient.h>
3
4#include "youtube.h"
5#include "youtube_parser.h"
6
7static String url = "https://www.googleapis.com/youtube/v3/channels?part=statistics&id={0}&key={1}";
9
10static const char *cert = "-----BEGIN CERTIFICATE-----\n" \
11 "MIIFVzCCAz+gAwIBAgINAgPlk28xsBNJiGuiFzANBgkqhkiG9w0BAQwFADBHMQsw\n" \
12 "CQYDVQQGEwJVUzEiMCAGA1UEChMZR29vZ2xlIFRydXN0IFNlcnZpY2VzIExMQzEU\n" \
13 "MBIGA1UEAxMLR1RTIFJvb3QgUjEwHhcNMTYwNjIyMDAwMDAwWhcNMzYwNjIyMDAw\n" \
14 "MDAwWjBHMQswCQYDVQQGEwJVUzEiMCAGA1UEChMZR29vZ2xlIFRydXN0IFNlcnZp\n" \
15 "Y2VzIExMQzEUMBIGA1UEAxMLR1RTIFJvb3QgUjEwggIiMA0GCSqGSIb3DQEBAQUA\n" \
16 "A4ICDwAwggIKAoICAQC2EQKLHuOhd5s73L+UPreVp0A8of2C+X0yBoJx9vaMf/vo\n" \
17 "27xqLpeXo4xL+Sv2sfnOhB2x+cWX3u+58qPpvBKJXqeqUqv4IyfLpLGcY9vXmX7w\n" \
18 "Cl7raKb0xlpHDU0QM+NOsROjyBhsS+z8CZDfnWQpJSMHobTSPS5g4M/SCYe7zUjw\n" \
19 "TcLCeoiKu7rPWRnWr4+wB7CeMfGCwcDfLqZtbBkOtdh+JhpFAz2weaSUKK0Pfybl\n" \
20 "qAj+lug8aJRT7oM6iCsVlgmy4HqMLnXWnOunVmSPlk9orj2XwoSPwLxAwAtcvfaH\n" \
21 "szVsrBhQf4TgTM2S0yDpM7xSma8ytSmzJSq0SPly4cpk9+aCEI3oncKKiPo4Zor8\n" \
22 "Y/kB+Xj9e1x3+naH+uzfsQ55lVe0vSbv1gHR6xYKu44LtcXF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cEZOitHTtNCjv0xyBZm\n" \
38 "2tIMPNuzjsmhDYAPexZ3FL//2wmUspO8IFgV6dtxQ/PeEMMA3KgqlbbC1j+Qa3bb\n" \
39 "bP6MvPJwNQzcmRk13NfIRmPVNnGuV/u3gm3c\n" \
40 "-----END CERTIFICATE-----\n";
41
42int8_t PaperdinkUIYoutubeClass::fetch_data(const char *channel_id, const char *api_id)
43{
44 int httpCode, ret = 0;
45 String payload;
46 WiFiClientSecure *client = new WiFiClientSecure;
47 client->setCACert(cert);
48
49 String get_url = url;
50 get_url.replace("{0}", channel_id);
51 get_url.replace("{1}", api_id);
52
53 {
54 HTTPClient https;
55 https.begin(*client, get_url.c_str());
56 https.addHeader("Content-Type", "application/json", 0, 0);
57
58 httpCode = https.GET();
59
60 if (httpCode == HTTP_CODE_OK) {
61 // HTTP header has been send and Server response header has been handled
62 DEBUG.printf("[HTTP] GET SUCCESS\r\n");
63 //String payload = https.getString();
64 //Serial.println(payload);
66 parser.setListener(&youtube_listener);
67 https.writeToStream(&parser);
68 } else {
69 DEBUG.printf("[HTTP] Failed, error: %s\r\n", https.errorToString(httpCode).c_str());
70 ret = -1;
71 }
72
73 https.end();
74 }
75
76 delete client;
77 DEBUG.printf("[HTTP] COMPLETED \r\n");
78 return ret;
79}
80
81void PaperdinkUIYoutubeClass::display_subscribers_med(GxEPD2_GFX& display, int16_t x, int16_t y, int16_t w, int16_t h)
82{
83 int16_t xt, yt;
84 uint16_t wt, ht, prev_height = y, prev_width = x;
85 char subscribers_str[MAX_YOUTUBE_SUBSCRIBER_COUNT];
86
87 display.setFont(font);
88 display.setTextColor(primary_color);
89 display.setTextSize(1);
90
91 // Display subscribers count
92 snprintf(subscribers_str, MAX_YOUTUBE_SUBSCRIBER_COUNT, "%lld", subscribers);
93 display.getTextBounds(subscribers_str, 0, 0, &xt, &yt, &wt, &ht);
94 if (w == 0 || h == 0) {
95 /* No centering */
96 // 5px and 4px margin
97 display.setCursor(prev_width + 5, prev_height + ht + 4);
98 } else {
99 display.setCursor(prev_width + 5 + ((w - wt) / 2) + (user_med_width / 2), prev_height + 4 + ((h - ht) / 2));
100 prev_width += 5 + ((w + wt) / 2) + (user_med_width / 2);
101 prev_height += 4 + (ht);
102 }
103 display.print(subscribers_str);
104
105 // 3 px margin
106 display.drawBitmap(prev_width - wt - user_med_width - 5, prev_height - user_med_height + 3, user_med, user_med_width, user_med_height, primary_color);
107}
108
109void PaperdinkUIYoutubeClass::display_views_med(GxEPD2_GFX& display, int16_t x, int16_t y, int16_t w, int16_t h)
110{
111 int16_t xt, yt;
112 uint16_t wt, ht, prev_height = y, prev_width = x;
113 const int16_t block_width = 100;
114 char views_str[MAX_YOUTUBE_VIEWS_COUNT];
115
116 display.setFont(font);
117 display.setTextColor(primary_color);
118 display.setTextSize(1);
119
120 // Display ticker
121 snprintf(views_str, MAX_YOUTUBE_VIEWS_COUNT, "%lld", views);
122 display.getTextBounds(views_str, 0, 0, &xt, &yt, &wt, &ht);
123 if (w == 0 || h == 0) {
124 /* No centering */
125 // 5px and 4px margin
126 display.setCursor(prev_width + 5, prev_height + ht + 4);
127 } else {
128 display.setCursor(prev_width + 5 + ((w - wt) / 2) + (eye_med_width / 2), prev_height + 4 + ((h - ht) / 2));
129 prev_width += 5 + ((w + wt) / 2) + (eye_med_width / 2);
130 prev_height += 4 + (ht);
131 }
132 display.print(views_str);
133
134 // 3 px margin
135 display.drawBitmap(prev_width - wt - eye_med_width - 5, prev_height - eye_med_height + 3, eye_med, eye_med_width, eye_med_height, primary_color);
136}
137
const GFXfont * font
Definition ui_base.h:9
void display_views_med(GxEPD2_GFX &display, int16_t x, int16_t y, int16_t w=0, int16_t h=0)
Definition youtube.cpp:109
int8_t fetch_data(const char *channel_id, const char *api_id)
Definition youtube.cpp:42
void display_subscribers_med(GxEPD2_GFX &display, int16_t x, int16_t y, int16_t w=0, int16_t h=0)
Definition youtube.cpp:81
#define eye_med_height
Definition eye_med.h:2
#define eye_med_width
Definition eye_med.h:1
const unsigned char eye_med[128]
Definition eye_med.h:4
#define user_med_height
Definition user_med.h:2
const unsigned char user_med[128]
Definition user_med.h:4
#define user_med_width
Definition user_med.h:1
PaperdinkUIYoutubeClass Paperdink_Youtube
Definition youtube.cpp:138
YoutubeJsonListener youtube_listener
Definition youtube.cpp:8
#define MAX_YOUTUBE_VIEWS_COUNT
Definition youtube.h:9
#define MAX_YOUTUBE_SUBSCRIBER_COUNT
Definition youtube.h:8
#define DEBUG
Definition youtube.h:6