Skip to content

Instantly share code, notes, and snippets.

@Beej126
Last active November 18, 2021 22:31
Show Gist options
  • Select an option

  • Save Beej126/fef02524a541d0d22532bf08ac6fa90c to your computer and use it in GitHub Desktop.

Select an option

Save Beej126/fef02524a541d0d22532bf08ac6fa90c to your computer and use it in GitHub Desktop.

Revisions

  1. Beej126 revised this gist Nov 18, 2021. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions instaTeam.js
    Original file line number Diff line number Diff line change
    @@ -24,11 +24,13 @@ const events = fullSchedule.schedulesByDate.map(s => s.schedules).flat();
    //attendance === 3 means not yet attending
    //so this final expression filters on those events not yet booked,
    //and calls the web api to flip her on for attending
    events.filter(e => e.childrenAttendance[0].attendance === 3).forEachAsync(async (e) => {
    await events.filter(e => e.childrenAttendance[0].attendance === 3).forEachAsync(async (e) => {
    console.log("attend " +e.scheduleId+ " posting...");
    await instaPost("/api/team/schedule/attend", {
    "scheduleId": e.scheduleId,
    "attend": "yes", // **** here's the beef! *****************************************************
    "memberId": "xxxxxxx"
    });
    await sleep(2);
    });
    });
    console.log("DONE!");
  2. Beej126 created this gist Sep 7, 2021.
    34 changes: 34 additions & 0 deletions instaTeam.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    /////////////////////////////////////////////////////////////////////////////////////////
    //first some helper functions to tee up one last clean routine at the bottom

    //from: https://stackoverflow.com/questions/951021/what-is-the-javascript-version-of-sleep/39914235#39914235
    const sleep = s => new Promise(resolve => setTimeout(resolve, s * 1000));

    //from:https://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop/49432189#49432189
    Array.prototype.forEachAsync = async function (fn) { for (let t of this) { await fn(t) } }

    //get data
    const instaGet = url => fetch(url).then(response => response.json());

    //post data
    const instaPost = (url, dataObj) => fetch(url, { method: "POST", headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(dataObj) }).then(response => response.json()).then(result => console.log(result));

    /////////////////////////////////////////////////////////////////////////////////////////

    //get the full list of scheduled events
    const fullSchedule = await instaGet("/api/team/schedule/list?v=2&paidEvents=0");

    //flatten out their nested datastructure into an clean array of event objects
    const events = fullSchedule.schedulesByDate.map(s => s.schedules).flat();

    //attendance === 3 means not yet attending
    //so this final expression filters on those events not yet booked,
    //and calls the web api to flip her on for attending
    events.filter(e => e.childrenAttendance[0].attendance === 3).forEachAsync(async (e) => {
    await instaPost("/api/team/schedule/attend", {
    "scheduleId": e.scheduleId,
    "attend": "yes", // **** here's the beef! *****************************************************
    "memberId": "xxxxxxx"
    });
    await sleep(2);
    });