diff --git a/packages/skin-database/cli.ts b/packages/skin-database/cli.ts index 147ba168..04033fb3 100755 --- a/packages/skin-database/cli.ts +++ b/packages/skin-database/cli.ts @@ -123,10 +123,10 @@ async function main() { break; } case "tweet": { - console.log("tweet"); + console.log("tweet & insta"); const hash = argv._[1]; - const md5 = await tweet(client, hash); - await insta(client, md5); + await tweet(client, hash); + await insta(client, hash); break; } case "insta": { diff --git a/packages/skin-database/data/.DS_Store b/packages/skin-database/data/.DS_Store new file mode 100644 index 00000000..5008ddfc Binary files /dev/null and b/packages/skin-database/data/.DS_Store differ diff --git a/packages/skin-database/data/skins.ts b/packages/skin-database/data/skins.ts index bbeeead3..2ca99729 100644 --- a/packages/skin-database/data/skins.ts +++ b/packages/skin-database/data/skins.ts @@ -116,7 +116,10 @@ export async function markAsPostedToInstagram( postId: string, url: string ): Promise { - await knex("instagram_posts").insert({ skin_md5: md5, post_id: postId, url }, []); + await knex("instagram_posts").insert( + { skin_md5: md5, post_id: postId, url }, + [] + ); } // TODO: Also path actor @@ -391,6 +394,28 @@ export async function getSkinToTweet(): Promise<{ return { md5: skin.md5, canonicalFilename: path.basename(skin.file_path) }; } +export async function getSkinToPostToInstagram(): Promise { + // TODO: This does not account for skins that have been both approved and rejected + const tweetables = await knex("skins") + .leftJoin("skin_reviews", "skin_reviews.skin_md5", "=", "skins.md5") + .leftJoin("instagram_posts", "instagram_posts.skin_md5", "=", "skins.md5") + .leftJoin("refreshes", "refreshes.skin_md5", "=", "skins.md5") + .where({ + "instagram_posts.id": null, + skin_type: 1, + "skin_reviews.review": "APPROVED", + "refreshes.error": null, + }) + .groupBy("skins.md5") + .orderByRaw("random()") + .limit(1); + const skin = tweetables[0]; + if (skin == null) { + return null; + } + return skin.md5; +} + export async function getStats(): Promise<{ approved: number; rejected: number; diff --git a/packages/skin-database/tasks/insta.ts b/packages/skin-database/tasks/insta.ts index e7b31912..d3575668 100644 --- a/packages/skin-database/tasks/insta.ts +++ b/packages/skin-database/tasks/insta.ts @@ -1,13 +1,27 @@ import * as Skins from "../data/skins"; import * as S3 from "../s3"; -import { INSTAGRAM_ACCESS_TOKEN, INSTAGRAM_ACCOUNT_ID, TWEET_BOT_CHANNEL_ID } from "../config"; +import { + INSTAGRAM_ACCESS_TOKEN, + INSTAGRAM_ACCOUNT_ID, + TWEET_BOT_CHANNEL_ID, +} from "../config"; import { Client } from "discord.js"; import sharp from "sharp"; import SkinModel from "../data/SkinModel"; import UserContext from "../data/UserContext"; import fetch from "node-fetch"; -export async function insta(discordClient: Client, md5: string): Promise { +export async function insta( + discordClient: Client, + md5: string | null +): Promise { + if (md5 == null) { + md5 = await Skins.getSkinToPostToInstagram(); + } + if (md5 == null) { + console.error("No skins to post to instagram"); + return; + } const url = await post(md5); console.log("Going to post to discord"); @@ -19,7 +33,6 @@ export async function insta(discordClient: Client, md5: string): Promise { console.log("Posted to discord"); } - async function createContent( imageUrl: string, caption: string @@ -133,11 +146,11 @@ async function post(md5: string): Promise { console.log("Publishing content..."); const id = await publish(contentId); - console.log("Getting permalink") + console.log("Getting permalink"); const permalink = await getPermalink(id); console.log("Permalink", permalink); - console.log("Marking posted in DB") + console.log("Marking posted in DB"); await Skins.markAsPostedToInstagram(md5, id, permalink); return permalink;