-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNews.cpp
More file actions
152 lines (124 loc) · 4.02 KB
/
News.cpp
File metadata and controls
152 lines (124 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "News.h"
#include "Kernel.h"
#include "Secrets.h"
#include "GFXConstants.h"
#include <HTTPClient.h>
#include "ArduinoJson.h"
const String guardian = "https://content.guardianapis.com/search?show-blocks=body&tag=us-news/us-news&page-size=1&type=article";
const String hf_bart = "https://router.huggingface.co/hf-inference/models/facebook/bart-large-cnn";
String getNewsArticle(short idx=1) {
HTTPClient http;
String url = guardian + GUARDIAN_API;
url += "&page=" + String(idx);
http.begin(url.c_str());
int http_code = http.GET();
if(http_code > 0) {
// Serial.println(http.getString());
Serial.print("Guardian error: "); // <<< ADDED
Serial.println(http_code); // <<< ADDED
JsonDocument doc;
DeserializationError error = deserializeJson(doc, http.getString());
if(error) { http.end(); return ""; }
// Serial.println(doc["response"]["results"][0]["webTitle"]);
String title = doc["response"]["results"][0]["webTitle"];
String content = doc["response"]["results"][0]["blocks"]["body"][0]["bodyTextSummary"];
http.end();
return (title + ": " + content).substring(0, 1.5 * 1024);
} else {
Serial.println("Failed to read URL");
http.end();
return "";
}
}
String bartSummarize(String& news) {
HTTPClient http;
http.begin(hf_bart);
String bearerAuth = "Bearer ";
bearerAuth += HF_TOKEN;
http.addHeader("Authorization", bearerAuth);
// 2. Content-Type Header
http.addHeader("Content-Type", "application/json");
news.replace("\\", "\\\\"); // Keep escaping for good practice
news.replace("\"", "\\\"");
news.replace("\'", "\\\'");
news.replace("\n", "\\n");
news.replace("\r", "\\r");
news.replace("\t", "\\t");
news.replace("\b", "\\b");
news.replace("\f", "\\f");
String payload = "{ \"parameters\": {\"truncation\": \"longest_first\", \"max_length\": 100}, \"inputs\": \"";
payload += news;
payload += "\"}";
int http_response = http.POST(payload);
// Serial.println(payload);
if(http_response > 0) {
// Serial.println(http_response);
JsonDocument doc;
DeserializationError error = deserializeJson(doc, http.getStream());
if(error) {
http.end();
return "";
} else {
http.end();
return doc[0]["summary_text"];
}
} else {
http.end();
return "";
}
}
News::News(Kernel* kernel) : App(kernel) {
this->kernel = kernel;
// this->kernel->display.setTextColor(TFT_WHITE, TFT_BLACK, true);
// Create display sprite
this->kernel->display.unloadFont();
this->news_sprite = new TFT_eSprite(&(this->kernel->display));
this->news_sprite->setColorDepth(1);
this->news_sprite->createSprite(viewbox_wh, viewbox_wh);
this->news_sprite->setTextSize(1);
this->news_sprite->setTextFont(1); //
this->news_sprite->setTextColor(TFT_WHITE, TFT_BLACK, true);
this->news_sprite->setTextWrap(true);
}
void News::run_code(double x, double y, bool special) {
if(x != 0 && !this->last_x) {
if(x > 0) {
this->news_idx += 1;
updateNews();
} else if(this->news_idx > 1) {
this->news_idx -= 1;
updateNews();
}
}
if(this->news_summary == "") {
this->updateNews();
}
this->news_sprite->pushSprite(left_vb, top_vb);
if(x == 0) {
this->last_x = false;
}
}
String News::get_name() {
return String("News");
}
void News::updateNews() {
this->kernel->clearViewBox();
this->news_sprite->fillSprite(TFT_BLACK);
this->news_sprite->setCursor(0, 0);
this->news_sprite->print("Loading...");
this->news_sprite->pushSprite(left_vb, top_vb);
Serial.printf("Heap: %u\n", ESP.getFreeHeap());
String content = getNewsArticle(this->news_idx);
Serial.printf("Heap: %u\n", ESP.getFreeHeap());
Serial.println(content);
this->news_summary = bartSummarize(content);
Serial.printf("Heap: %u\n", ESP.getFreeHeap());
Serial.println(this->news_summary);
this->kernel->clearViewBox();
this->news_sprite->fillSprite(TFT_BLACK);
this->news_sprite->setCursor(0, 0);
this->news_sprite->print(news_summary);
}
News::~News() {
delete this->news_sprite;
}