Fix twitter bot

This commit is contained in:
Jordan Eldredge 2020-09-22 00:21:19 -04:00
parent 1c07cf8e29
commit 04dca01af9
6 changed files with 45 additions and 16 deletions

View file

@ -3,7 +3,7 @@ import path from "path";
import logger from "../logger";
import md5Hash from "md5";
import { searchIndex } from "../algolia";
import { truncate } from "../utils";
import { truncate, MD5_REGEX } from "../utils";
import { DBSkinRecord, SkinRecord, DBIARecord, TweetStatus } from "../types";
import fetch from "node-fetch";
import * as S3 from "../s3";
@ -86,7 +86,6 @@ export async function addSkin({
}
const IA_URL = /^(https:\/\/)?archive.org\/details\/([^\/]+)\/?/;
const MD5 = /([a-fA-F0-9]{32})/;
const CRUFT_FILENAME = /winampskins\.info\.(html)|(txt)$/;
@ -112,7 +111,7 @@ export async function setContentHash(md5: string): Promise<string | null> {
export async function getMd5ByAnything(
anything: string
): Promise<string | null> {
const md5Match = anything.match(MD5);
const md5Match = anything.match(MD5_REGEX);
if (md5Match != null) {
const md5 = md5Match[1];
const found = await knex("skins").where({ md5, skin_type: 1 }).first();
@ -499,6 +498,7 @@ export async function getScreenshotBuffer(md5: string): Promise<Buffer> {
export async function setTweetInfo(
md5: string,
likes: number,
retweets: number,
tweetId: string
): Promise<void> {
await skins_CONVERTED.findOneAndUpdate(

View file

@ -24,6 +24,7 @@
"sqlite3": "^4.2.0",
"temp": "^0.9.0",
"ts-node": "^8.10.2",
"twitter-api-client": "^0.2.1",
"winston": "^3.2.1",
"yargs": "^13.2.4"
},

View file

@ -27,7 +27,7 @@ function spawnPromise(command: string, args: string[]): Promise<string> {
if (code === 0) {
resolve(stdout);
} else {
reject({ stdout, stderr });
reject(stderr);
}
});
});
@ -48,7 +48,7 @@ export async function tweet(discordClient: Client, anything: string | null) {
);
return;
}
tweetableSkin = await Skins.getSkinByMd5(_md5);
tweetableSkin = await Skins.getSkinByMd5_DEPRECATED(_md5);
if (tweetableSkin == null) {
// @ts-ignore
await tweetBotChannel.send(
@ -83,15 +83,23 @@ export async function tweet(discordClient: Client, anything: string | null) {
if (filename == null) {
throw new Error(`Could not find filename for skin with hash ${md5}`);
}
const output = await spawnPromise(
path.resolve(PROJECT_ROOT, "../tweetBot/tweet.py"),
[
"tweet",
md5,
filename,
// "--dry",
]
);
let output;
try {
output = await spawnPromise(
path.resolve(PROJECT_ROOT, "../tweetBot/tweet.py"),
[
"tweet",
md5,
filename, // "--dry",
]
);
} catch (e) {
// @ts-ignore
await tweetBotChannel.send(
`Oops. The Python part of the twitter bot crashed: ${e.message}`
);
return;
}
await Skins.markAsTweeted(md5, output.trim());
// @ts-ignore
await tweetBotChannel.send(output.trim());

View file

@ -10,3 +10,5 @@ export function truncate(str: string, len: number): string {
const end = str.slice(-half);
return `${start} ########### ${end}`;
}
export const MD5_REGEX = /([a-fA-F0-9]{32})/;

View file

@ -68,7 +68,7 @@ def tweet_skin(md5, skin_name, dry):
def get_screenshot_url(md5):
return "https://s3.amazonaws.com/webamp-uploaded-skins/screenshots/%s.png" % md5
return "https://cdn.webampskins.org/screenshots/%s.png" % md5
def tweet_image(skin_name, md5, screenshot_path, dry):

View file

@ -2941,7 +2941,7 @@ buffer@^4.3.0:
ieee754 "^1.1.4"
isarray "^1.0.0"
buffer@^5.2.1, buffer@^5.5.0:
buffer@^5.2.1, buffer@^5.5.0, buffer@^5.6.0:
version "5.6.0"
resolved "https://registry.yarnpkg.com/buffer/-/buffer-5.6.0.tgz#a31749dc7d81d84db08abf937b6b8c4033f62786"
dependencies:
@ -8711,6 +8711,10 @@ oauth-sign@~0.9.0:
version "0.9.0"
resolved "https://registry.yarnpkg.com/oauth-sign/-/oauth-sign-0.9.0.tgz#47a7b016baa68b5fa0ecf3dee08a85c679ac6455"
oauth@^0.9.15:
version "0.9.15"
resolved "https://registry.yarnpkg.com/oauth/-/oauth-0.9.15.tgz#bd1fefaf686c96b75475aed5196412ff60cfb9c1"
object-assign@4.x, object-assign@^4, object-assign@^4.0.1, object-assign@^4.1.0, object-assign@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
@ -8738,6 +8742,12 @@ object-keys@^1.0.11, object-keys@^1.0.12, object-keys@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/object-keys/-/object-keys-1.1.1.tgz#1c47f272df277f3b1daf061677d9c82e2322c60e"
object-sizeof@^1.6.1:
version "1.6.1"
resolved "https://registry.yarnpkg.com/object-sizeof/-/object-sizeof-1.6.1.tgz#35971f3fd2102bd8b51c67b0a53ed773ff77ab56"
dependencies:
buffer "^5.6.0"
object-visit@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/object-visit/-/object-visit-1.0.1.tgz#f79c4493af0c5377b59fe39d395e41042dd045bb"
@ -11772,6 +11782,14 @@ tweetnacl@^1.0.0:
version "1.0.3"
resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-1.0.3.tgz#ac0af71680458d8a6378d0d0d050ab1407d35596"
twitter-api-client@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/twitter-api-client/-/twitter-api-client-0.2.1.tgz#f766c90211827e6a62e92c6a9390eb31f6732eea"
dependencies:
"@sinonjs/fake-timers" "^6.0.1"
oauth "^0.9.15"
object-sizeof "^1.6.1"
type-check@~0.3.2:
version "0.3.2"
resolved "https://registry.yarnpkg.com/type-check/-/type-check-0.3.2.tgz#5884cab512cf1d355e3fb784f30804b2b520db72"