index.mjs 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import axios from 'axios';
  2. import ical2json from 'ical2json';
  3. import fs from 'fs-extra';
  4. (async function () {
  5. console.log(`
  6. *******************
  7. Ahoj.
  8. .
  9. . ____/
  10. < *__|
  11. /\\ /\\
  12. *******************`);
  13. // get current events
  14. const currentEvents = await getEvents();
  15. // get past events
  16. const pastEvents = await getEvents(true);
  17. const events = [...currentEvents, ...pastEvents];
  18. console.log(`
  19. Found ${events.length} events.
  20. ${currentEvents.length} events will be in the future.
  21. ${pastEvents.length} events already happened.
  22. See them all in events.json.
  23. *******************
  24. `);
  25. await fs.writeJson('events.json', events, { spaces: 2 });
  26. }());
  27. async function getEventPage(i, past=false) {
  28. const url = `https://www.sustainable-communities.net/events/list/?tribe_paged=${i}&tribe_event_display=${past ? 'past' : 'list'}&ical=1${past && '&tribe_display=past' || ''}`
  29. const { data } = await axios.get(url);
  30. return (ical2json.convert(data).VCALENDAR[0].VEVENT || []);
  31. }
  32. async function getEvents(past=false) {
  33. let events = await getEventPage(1, past);
  34. let newEvents = events;
  35. let i = 2;
  36. while (newEvents.length > 0) {
  37. newEvents = await getEventPage(i, past);
  38. events = events.concat(newEvents);
  39. ++i;
  40. }
  41. return events;
  42. }