-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnews.js
147 lines (140 loc) · 8.16 KB
/
news.js
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
load("sbbsdefs.js");
load("rss-atom.js");
load("http.js");
load("frame.js");
require("html2asc.js", 'html2asc');
const SHOW_AT_MOST = 10;
const FROG_FIND_URL = "http://www.frogfind.com/read.php?a=";
const MENU_ITEM_FMT = "\x01c\x01h%2d\x01k\x01h)\x01n %s\x010\x01n";
const PROMPT_FMT = "\x01n\r\nSelect: \x01h1\x01n-\x01h%d\x01n or \x01hQ\x01n to quit\x01h> ";
const DIVIDER = "\x01n\x01b______________________________________________________________________________\x01n";
const BW_TEXT = true;
const MONTH = ["Jan", "Feb", "Mar", "Apr", "May",
"Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
];
var articleCount = 0;
var articleSelect = 0;
var s = 0;
var l = 0;
var startY;
var rss;
var selection = "";
var jsonSources = "";
var msgFile = "";
var fSources = new File(js.exec_dir + "news_src.json");
if (fSources.open("r")) {
jsonSources = JSON.parse(fSources.read());
fSources.close();
while (bbs.online && selection !== "Q") {
console.clear();
console.printfile(js.exec_dir + "news.msg", P_NOABORT);
s = 0;
l = 0;
while (s < Object.keys(jsonSources).length) {
if (s + 1 < Object.keys(jsonSources).length) {
console.gotoxy(2, 10 + l);
printf(MENU_ITEM_FMT, s + 1, jsonSources[s].name);
s = s + 1;
console.gotoxy(41, 10 + l);
printf(MENU_ITEM_FMT, s + 1, jsonSources[s].name);
s = s + 1;
} else {
console.gotoxy(2, 10 + l);
printf(MENU_ITEM_FMT, s + 1, jsonSources[s].name);
s = s + 1;
}
l = l + 1;
}
printf("\r\n" + PROMPT_FMT, Object.keys(jsonSources).length);
selection = console.getkeys("Q", Object.keys(jsonSources).length, K_UPPER);
if (selection === "" || selection === undefined) {
selection = "Q";
}
if (!isNaN(selection) && selection !== "") {
articleSelect = "";
selection = parseInt(selection) - 1;
printf("\x01L\x01nGetting feed for \x01h%s\x01n...", jsonSources[selection].name);
log(LOG_INFO, "Getting feed for " + jsonSources[selection].name);
try {
rss = new Feed(jsonSources[selection].url);
} catch (f_err) {
print("\x01n\r\n\r\nCouldn't get any data from " + jsonSources[selection].name + "\x01n, \x01hsorry!.\x01n");
log(LOG_WARNING, f_err + " ... " + jsonSources[selection].name + " (url: " + jsonSources[selection].url + ")");
continue;
}
for (var c = 0; c < rss.channels.length && c <= SHOW_AT_MOST; c++) { // It's normal to have at most one channel in a feed.
while (bbs.online && articleSelect !== "Q") {
articleSelect = "";
msgFile = backslash(backslash(js.exec_dir) + "newsicons") + jsonSources[selection].icon + ".msg";
console.clear();
if (file_exists(msgFile)) {
console.printfile(msgFile, P_NOABORT);
startY = console.getxy().y;
} else {
log(LOG_WARNING, "Missing msg file: " + msgFile);
startY = 5;
}
console.gotoxy(30, 1);
printf("\x01y\x01h%*s", 50, rss.channels[c].title.slice(0, 50).trim());
console.gotoxy(30, 2);
printf("\x01w\x01h%*s", 50, "updated " + getShortDateTime(new Date(rss.channels[c].updated === "" ? rss.channels[c].items[0].date : rss.channels[c].updated)));
var frame = new Frame(31, 3, 49, 3, BG_BLACK);
frame.open();
frame.putmsg("\x01k\x01h" + lfexpand(word_wrap(utf8_decode(rss.channels[c].description.trim()), 49, 49, true, true)).trim());
frame.cycle();
console.gotoxy(1, startY);
print("\x01n\r\n" + format("\x01c%2s\x01k\x01h)\x01n \x01w%-62s\x01n \x01b%-11s", "#", "title", "added"));
print(DIVIDER);
articleCount = 0;
for (var i = 0; i < rss.channels[c].items.length && i < SHOW_AT_MOST; i++) {
print(format("\x01c\x01h%2d\x01k\x01h)\x01n \x01w\x01h%-62.62s\x01n \x01b\x01h%-11.11s", i + 1, utf8_decode(rss.channels[c].items[i].title), getShortDate(new Date(rss.channels[c].items[i].date)) ));
articleCount = articleCount + 1;
}
printf(PROMPT_FMT, articleCount);
articleSelect = console.getkeys("Q", articleCount, K_UPPER);
if (articleSelect === "") {
articleSelect = "Q";
}
if (!isNaN(articleSelect) && articleSelect !== "") {
console.clear();
print("\x01n\x01bTitle: \x01w\x01h" + word_wrap(utf8_decode(rss.channels[c].items[articleSelect - 1].title), 70, 70, true, true).replace(/\n/g, "\r\n ").trim());
log(LOG_INFO, rss.channels[c].items[articleSelect - 1].title);
if (rss.channels[c].items[articleSelect - 1].author != undefined && rss.channels[c].items[articleSelect - 1].author != "") {
printf("\x01n\x01bAuthor: \x01w\x01h%s\r\n", html2asc(rss.channels[c].items[articleSelect - 1].author, BW_TEXT).trim());
}
print("\x01n\x01bDate: \x01w\x01h" + new Date(rss.channels[c].items[articleSelect - 1].date).toLocaleString());
print(DIVIDER);
print(lfexpand(word_wrap(utf8_decode(html2asc(rss.channels[c].items[articleSelect - 1].body, BW_TEXT)), 79, 79, true, true)));
if (rss.channels[c].items[articleSelect - 1].body === undefined || rss.channels[c].items[articleSelect - 1].body === "") {
print(lfexpand(word_wrap(utf8_decode(html2asc(rss.channels[c].items[articleSelect - 1].content, BW_TEXT)).replace(/\[/g, "\x01k\x01h[").replace(/\]/g, "]\x01n"), 79, 79, true, true)));
}
if (jsonSources[selection].frog_prompt) {
if (!console.noyes("Read full article text")) {
print("\x01n\x01gGetting text via \x01hFrogFind\x01n\x01g for:\r\n\x01n\x01c" + rss.channels[c].items[articleSelect - 1].link + "\x01n...\r\n");
log(LOG_INFO, "Getting text via FrogFind for:" + rss.channels[c].items[articleSelect - 1].link + "...\r\n");
try {
var resp = (new HTTPRequest()).Get(FROG_FIND_URL + rss.channels[c].items[articleSelect - 1].link);
print("\x01q\x01l" + utf8_decode(html2asc(resp, BW_TEXT)).substr(50).replace(/\[/g, "\x01k\x01h[").replace(/\]/g, "]\x01n") );
print("\x01n\x01b\x01h/===END===/");
} catch (err) {
print("\x01nFailed... \x01hFrogFind might be temporarily offline.\r\n\x01w\x01hPlease try again later.\x01n");
log(LOG_WARNING, "No response from: " + FROG_FIND_URL + rss.channels[c].items[articleSelect - 1].link);
}
}
} else {
print("\x01n\x01b\x01h/===END===/");
console.pause();
}
}
frame.close();
}
} // end for
}
} // end while
}
function getShortDate(d) {
return format( "%s %02d %04d", MONTH[d.getMonth()], d.getDate(), d.getFullYear() );
}
function getShortDateTime(d) {
return format( "%s %02d %04d %02d:%02d", MONTH[d.getMonth()], d.getDate(), d.getFullYear(), d.getHours(), d.getMinutes() );
}