forked from cory/tildefriends
128 lines
3.2 KiB
JavaScript
128 lines
3.2 KiB
JavaScript
|
//! {"permissions": ["network"], "require": ["libencoding", "libhttp"]}
|
||
|
|
||
|
let libhttp = require("libhttp");
|
||
|
|
||
|
async function getEvents() {
|
||
|
let now = new Date();
|
||
|
let cacheVersion = "1";
|
||
|
let useCache = await database.get("cacheVersion") == cacheVersion;
|
||
|
let events = [];
|
||
|
let url = "https://api.meetup.com/The-Most-Informal-Running-Club-Ever-TMIRCE-Upstate/events?&sign=true&photo-host=public&status=past";
|
||
|
let done = false;
|
||
|
while (!done) {
|
||
|
let response;
|
||
|
if (useCache) {
|
||
|
response = await database.get(url);
|
||
|
if (response) {
|
||
|
response = JSON.parse(response);
|
||
|
}
|
||
|
}
|
||
|
if (!response) {
|
||
|
response = await libhttp.get(url);
|
||
|
await database.set(url, JSON.stringify(response));
|
||
|
}
|
||
|
let nextLink;
|
||
|
let theseEvents = JSON.parse(response.body);
|
||
|
for (let j in theseEvents) {
|
||
|
if (new Date(theseEvents[j].time) < now) {
|
||
|
events.push(theseEvents[j]);
|
||
|
} else {
|
||
|
done = true;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
for (let j in response.headerArray) {
|
||
|
let link = response.headerArray[j];
|
||
|
if (link[0] == "Link" && link[1].split("; ")[1] == 'rel="next"') {
|
||
|
link = link[1].split("; ")[0];
|
||
|
link = link.substring(1, link.length - 1);
|
||
|
nextLink = link;
|
||
|
}
|
||
|
}
|
||
|
if (nextLink) {
|
||
|
url = nextLink;
|
||
|
} else {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
await database.set("cacheVersion", cacheVersion);
|
||
|
return events;
|
||
|
}
|
||
|
|
||
|
async function trackWorkouts(events) {
|
||
|
for (let i in events) {
|
||
|
let event = events[i];
|
||
|
if (event && event.venue && event.venue.name
|
||
|
&& new Date(event.time).getDay() == 1) {
|
||
|
let match = /follow (?:my|our) (.*?) workout/.exec(event.description);
|
||
|
let workout;
|
||
|
if (match) {
|
||
|
workout = match[1];
|
||
|
} else if (/snowshoe/.exec(event.description)
|
||
|
|| /No run/.exec(event.description)) {
|
||
|
// nothing
|
||
|
} else {
|
||
|
terminal.print(event.description);
|
||
|
}
|
||
|
if (workout) {
|
||
|
terminal.print(workout);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async function locationByDay(day, events) {
|
||
|
for (let i in events) {
|
||
|
let event = events[i];
|
||
|
if (event && event.venue && event.venue.name
|
||
|
&& new Date(event.time).getDay() == day) {
|
||
|
terminal.print(event.venue.name);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async function redisplay(action, actions, events) {
|
||
|
terminal.clear();
|
||
|
if (actions[action]) {
|
||
|
terminal.setHash(action);
|
||
|
terminal.print({style: "font-size: xx-large", value: action});
|
||
|
}
|
||
|
for (let i in actions) {
|
||
|
terminal.print({command: i});
|
||
|
}
|
||
|
if (actions[action]) {
|
||
|
await actions[action](events);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async function main() {
|
||
|
terminal.print("loading events...");
|
||
|
let events = await getEvents();
|
||
|
terminal.print("loaded ", events.length.toString(), " events");
|
||
|
|
||
|
let actions = {
|
||
|
"Sunday Locations": locationByDay.bind(null, 0),
|
||
|
"Monday Track Workouts": trackWorkouts,
|
||
|
"Tuesday Locations": locationByDay.bind(null, 2),
|
||
|
"Wednesday Locations": locationByDay.bind(null, 3),
|
||
|
"Thursday Locations": locationByDay.bind(null, 4),
|
||
|
"Friday Locations": locationByDay.bind(null, 5),
|
||
|
"Saturday Locations": locationByDay.bind(null, 6),
|
||
|
}
|
||
|
|
||
|
core.register("hashChange", function(event) {
|
||
|
if (event.hash && event.hash[0] == '#') {
|
||
|
terminal.clear();
|
||
|
let hash = event.hash.substring(1);
|
||
|
redisplay(hash, actions, events);
|
||
|
}
|
||
|
});
|
||
|
|
||
|
let action = null;
|
||
|
while (true) {
|
||
|
redisplay(action, actions, events);
|
||
|
action = await terminal.readLine();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
main().catch(terminal.print);
|