Swift-Daily-Tips icon indicating copy to clipboard operation
Swift-Daily-Tips copied to clipboard

Automating the process

Open alicanbatur opened this issue 5 years ago • 1 comments

Sharing a tip after it has been added should be automated.

alicanbatur avatar Nov 26 '18 13:11 alicanbatur

I've done a little research and came up with this pseudo(javascript)code to use with GitHub Webhooks. It only includes Twitter status updates because AFAIK Instagram does not have a public API to allow posting images. I can build a fully functional script to run on Amazon Lambda or similar if it is indeed what you guys need.

// const repo = magical GitHub API object

async function handleGithubPushEvent(pushEventData) {

    // See [1] below
    const masterWantsUsToShare = pushEventData.commits.some(commit =>
        commit.message.contains('!share')
    );

    if (!masterWantsUsToShare) {
        return;
    }

    // See [2] below
    const tree = await repo.getTree('/');

    const latestCodeDirectory = tree.sortByNameDesc()
        .find(directory => directory.isCodeDirectory);

    // See [3] below
    const meta = await.repo.getFile(latestCodeDirectory.path + '/meta.json');
    const image = await.repo.getFile(latestCodeDirectory.path + '/screenshot.png');

    // See [4] below
    const mediaID = await twitterAPI.uploadMedia(image);

    // See [5] below
    await twitterAPI.postTweet({
        tweet: meta.tweet,
        media: mediaID,
    });
}

Further read: [1] GitHub webhooks — push event: https://developer.github.com/v3/activity/events/types/#pushevent [2] GitHub API — tree traversal: https://developer.github.com/v3/git/trees/ [3] GitHub API — file handling: https://developer.github.com/v3/git/blobs/ [4] Twitter API — media upload: https://developer.twitter.com/en/docs/media/upload-media/api-reference/post-media-upload.html [5] Twitter API — status update: https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-update

Note on Instagram: There are private API implementations around but I don't know what risks they might bring up. See for yourself and take your chances if you'd like 😛 https://github.com/huttarichard/instagram-private-api

hkan avatar Nov 29 '18 00:11 hkan