Skip to content

Commit 1254903

Browse files
authored
Merge pull request #88 from Qrome/2.8
2.8
2 parents 23955b9 + a12e4a7 commit 1254903

7 files changed

+292
-259
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ It is recommended to use Arduino IDE. You will need to configure Arduino IDE to
6969
* USB CH340G drivers: https://wiki.wemos.cc/downloads
7070
* Enter http://arduino.esp8266.com/stable/package_esp8266com_index.json into Additional Board Manager URLs field. You can add multiple URLs, separating them with commas. This will add support for the Wemos D1 Mini to Arduino IDE.
7171
* Open Boards Manager from Tools > Board menu and install esp8266 platform (and don't forget to select your ESP8266 board from Tools > Board menu after installation).
72-
* Select Board: "WeMos D1 R2 & mini"
72+
* Select Board: "WeMos D1 R2 & mini" or "LOLIN(WEMOS) D1 R2 & mini"
7373
* Set 1M SPIFFS -- this project uses SPIFFS for saving and reading configuration settings.
7474

7575
## Loading Supporting Library Files in Arduino
Binary file not shown.
Binary file not shown.

marquee/NewsApiClient.cpp

+194-186
Original file line numberDiff line numberDiff line change
@@ -1,187 +1,195 @@
1-
/** The MIT License (MIT)
2-
3-
Copyright (c) 2018 David Payne
4-
5-
Permission is hereby granted, free of charge, to any person obtaining a copy
6-
of this software and associated documentation files (the "Software"), to deal
7-
in the Software without restriction, including without limitation the rights
8-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
copies of the Software, and to permit persons to whom the Software is
10-
furnished to do so, subject to the following conditions:
11-
12-
The above copyright notice and this permission notice shall be included in all
13-
copies or substantial portions of the Software.
14-
15-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
22-
*/
23-
24-
25-
#include "NewsApiClient.h"
26-
27-
28-
29-
#define arr_len( x ) ( sizeof( x ) / sizeof( *x ) )
30-
31-
NewsApiClient::NewsApiClient(String ApiKey, String NewsSource) {
32-
updateNewsClient(ApiKey, NewsSource);
33-
}
34-
35-
void NewsApiClient::updateNewsClient(String ApiKey, String NewsSource) {
36-
mySource = NewsSource;
37-
myApiKey = ApiKey;
38-
}
39-
40-
void NewsApiClient::updateNews() {
41-
JsonStreamingParser parser;
42-
parser.setListener(this);
43-
HTTPClient http;
44-
45-
String apiGetData = "http://" + String(servername) + "/v2/top-headlines?sources=" + mySource + "&apiKey=" + myApiKey;
46-
47-
Serial.println("Getting News Data");
48-
Serial.println(apiGetData);
49-
http.begin(apiGetData);
50-
int httpCode = http.GET();
51-
52-
if (httpCode > 0) { // checks for connection
53-
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
54-
if(httpCode == HTTP_CODE_OK) {
55-
// get lenght of document (is -1 when Server sends no Content-Length header)
56-
int len = http.getSize();
57-
// create buffer for read
58-
char buff[128] = { 0 };
59-
// get tcp stream
60-
WiFiClient * stream = http.getStreamPtr();
61-
// read all data from server
62-
Serial.println("Start parsing...");
63-
while(http.connected() && (len > 0 || len == -1)) {
64-
// get available data size
65-
size_t size = stream->available();
66-
if(size) {
67-
// read up to 128 byte
68-
int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
69-
for(int i=0;i<c;i++) {
70-
parser.parse(buff[i]);
71-
}
72-
73-
if(len > 0)
74-
len -= c;
75-
}
76-
delay(1);
77-
}
78-
}
79-
http.end();
80-
} else {
81-
Serial.println("connection for news data failed: " + String(apiGetData)); //error message if no client connect
82-
Serial.println();
83-
return;
84-
}
85-
}
86-
87-
String NewsApiClient::getTitle(int index) {
88-
return news[index].title;
89-
}
90-
91-
String NewsApiClient::getDescription(int index) {
92-
return news[index].description;
93-
}
94-
95-
String NewsApiClient::getUrl(int index) {
96-
return news[index].url;
97-
}
98-
99-
void NewsApiClient::updateNewsSource(String source) {
100-
mySource = source;
101-
}
102-
103-
void NewsApiClient::whitespace(char c) {
104-
105-
}
106-
107-
void NewsApiClient::startDocument() {
108-
counterTitle = 0;
109-
}
110-
111-
void NewsApiClient::key(String key) {
112-
currentKey = key;
113-
}
114-
115-
void NewsApiClient::value(String value) {
116-
if (counterTitle == 10) {
117-
// we are full so return
118-
return;
119-
}
120-
if (currentKey == "title") {
121-
news[counterTitle].title = cleanText(value);
122-
}
123-
if (currentKey == "description") {
124-
news[counterTitle].description = cleanText(value);
125-
}
126-
if (currentKey == "url") {
127-
news[counterTitle].url = value;
128-
counterTitle++;
129-
}
130-
Serial.println(currentKey + "=" + value);
131-
}
132-
133-
void NewsApiClient::endArray() {
134-
}
135-
136-
void NewsApiClient::endObject() {
137-
}
138-
void NewsApiClient::startArray() {
139-
}
140-
141-
void NewsApiClient::startObject() {
142-
}
143-
144-
void NewsApiClient::endDocument() {
145-
}
146-
147-
String NewsApiClient::cleanText(String text) {
148-
text.replace("", "'");
149-
text.replace("", "\"");
150-
text.replace("", "\"");
151-
text.replace("`", "'");
152-
text.replace("", "'");
153-
text.replace("\\\"", "'");
154-
text.replace("", "-");
155-
text.replace("é", "e");
156-
text.replace("è", "e");
157-
text.replace("ë", "e");
158-
text.replace("ê", "e");
159-
text.replace("à", "a");
160-
text.replace("â", "a");
161-
text.replace("ù", "u");
162-
text.replace("ç", "c");
163-
text.replace("î", "i");
164-
text.replace("ï", "i");
165-
text.replace("ô", "o");
166-
text.replace("", "...");
167-
text.replace("", "-");
168-
text.replace("Â", "A");
169-
text.replace("À", "A");
170-
text.replace("æ", "ae");
171-
text.replace("Æ", "AE");
172-
text.replace("É", "E");
173-
text.replace("È", "E");
174-
text.replace("Ë", "E");
175-
text.replace("Ô", "O");
176-
text.replace("Ö", "Oe");
177-
text.replace("ö", "oe");
178-
text.replace("œ", "oe");
179-
text.replace("Œ", "OE");
180-
text.replace("Ù", "U");
181-
text.replace("Û", "U");
182-
text.replace("Ü", "Ue");
183-
text.replace("ü", "ue");
184-
text.replace("Ä", "Ae");
185-
text.replace("ä", "ae");
186-
return text;
1+
/** The MIT License (MIT)
2+
3+
Copyright (c) 2018 David Payne
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+
*/
23+
24+
25+
#include "NewsApiClient.h"
26+
27+
28+
29+
#define arr_len( x ) ( sizeof( x ) / sizeof( *x ) )
30+
31+
NewsApiClient::NewsApiClient(String ApiKey, String NewsSource) {
32+
updateNewsClient(ApiKey, NewsSource);
33+
}
34+
35+
void NewsApiClient::updateNewsClient(String ApiKey, String NewsSource) {
36+
mySource = NewsSource;
37+
myApiKey = ApiKey;
38+
}
39+
40+
void NewsApiClient::updateNews() {
41+
JsonStreamingParser parser;
42+
parser.setListener(this);
43+
HTTPClient http;
44+
45+
String apiGetData = "http://" + String(servername) + "/v2/top-headlines?sources=" + mySource + "&apiKey=" + myApiKey;
46+
47+
Serial.println("Getting News Data");
48+
Serial.println(apiGetData);
49+
http.begin(apiGetData);
50+
int httpCode = http.GET();
51+
52+
if (httpCode > 0) { // checks for connection
53+
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
54+
if(httpCode == HTTP_CODE_OK) {
55+
// get lenght of document (is -1 when Server sends no Content-Length header)
56+
int len = http.getSize();
57+
// create buffer for read
58+
char buff[128] = { 0 };
59+
// get tcp stream
60+
WiFiClient * stream = http.getStreamPtr();
61+
// read all data from server
62+
Serial.println("Start parsing...");
63+
while(http.connected() && (len > 0 || len == -1)) {
64+
// get available data size
65+
size_t size = stream->available();
66+
if(size) {
67+
// read up to 128 byte
68+
int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
69+
for(int i=0;i<c;i++) {
70+
parser.parse(buff[i]);
71+
}
72+
73+
if(len > 0)
74+
len -= c;
75+
}
76+
delay(1);
77+
}
78+
}
79+
http.end();
80+
} else {
81+
Serial.println("connection for news data failed: " + String(apiGetData)); //error message if no client connect
82+
Serial.println();
83+
return;
84+
}
85+
}
86+
87+
String NewsApiClient::getTitle(int index) {
88+
return news[index].title;
89+
}
90+
91+
String NewsApiClient::getDescription(int index) {
92+
return news[index].description;
93+
}
94+
95+
String NewsApiClient::getUrl(int index) {
96+
return news[index].url;
97+
}
98+
99+
String NewsApiClient::getUrlToImage(int index) {
100+
return news[index].urlToImage;
101+
}
102+
103+
void NewsApiClient::updateNewsSource(String source) {
104+
mySource = source;
105+
}
106+
107+
void NewsApiClient::whitespace(char c) {
108+
109+
}
110+
111+
void NewsApiClient::startDocument() {
112+
counterTitle = 0;
113+
}
114+
115+
void NewsApiClient::key(String key) {
116+
currentKey = key;
117+
}
118+
119+
void NewsApiClient::value(String value) {
120+
if (counterTitle == 10) {
121+
// we are full so return
122+
return;
123+
}
124+
if (currentKey == "title") {
125+
news[counterTitle].title = cleanText(value);
126+
}
127+
if (currentKey == "description") {
128+
news[counterTitle].description = cleanText(value);
129+
}
130+
if (currentKey == "url") {
131+
news[counterTitle].url = value;
132+
}
133+
if (currentKey == "urlToImage") {
134+
news[counterTitle].urlToImage = value;
135+
counterTitle++;
136+
}
137+
138+
Serial.println(currentKey + "=" + value);
139+
}
140+
141+
void NewsApiClient::endArray() {
142+
}
143+
144+
void NewsApiClient::endObject() {
145+
}
146+
void NewsApiClient::startArray() {
147+
}
148+
149+
void NewsApiClient::startObject() {
150+
}
151+
152+
void NewsApiClient::endDocument() {
153+
}
154+
155+
String NewsApiClient::cleanText(String text) {
156+
text.replace("", "'");
157+
text.replace("", "\"");
158+
text.replace("", "\"");
159+
text.replace("`", "'");
160+
text.replace("", "'");
161+
text.replace("\\\"", "'");
162+
text.replace("", "-");
163+
text.replace("é", "e");
164+
text.replace("è", "e");
165+
text.replace("ë", "e");
166+
text.replace("ê", "e");
167+
text.replace("à", "a");
168+
text.replace("â", "a");
169+
text.replace("ù", "u");
170+
text.replace("ç", "c");
171+
text.replace("î", "i");
172+
text.replace("ï", "i");
173+
text.replace("ô", "o");
174+
text.replace("", "...");
175+
text.replace("", "-");
176+
text.replace("Â", "A");
177+
text.replace("À", "A");
178+
text.replace("æ", "ae");
179+
text.replace("Æ", "AE");
180+
text.replace("É", "E");
181+
text.replace("È", "E");
182+
text.replace("Ë", "E");
183+
text.replace("Ô", "O");
184+
text.replace("Ö", "Oe");
185+
text.replace("ö", "oe");
186+
text.replace("œ", "oe");
187+
text.replace("Œ", "OE");
188+
text.replace("Ù", "U");
189+
text.replace("Û", "U");
190+
text.replace("Ü", "Ue");
191+
text.replace("ü", "ue");
192+
text.replace("Ä", "Ae");
193+
text.replace("ä", "ae");
194+
return text;
187195
}

0 commit comments

Comments
 (0)