Store milestone data in DB

This commit is contained in:
Jordan Eldredge 2022-02-03 13:50:48 -05:00
parent 367b85448f
commit 5169b94708
4 changed files with 8 additions and 82 deletions

View file

@ -3,6 +3,9 @@ import { knex } from "../db";
export default class KeyValue {
static async get(key: string): Promise<any> {
const result = await knex("key_value").where({key}).first("value");
if(result == null) {
return null;
}
return JSON.parse(result.value);
}
@ -25,4 +28,4 @@ export default class KeyValue {
await knex("key_value").insert({key, value: json});
}
}
}

View file

@ -1 +0,0 @@
10000

View file

@ -1,64 +0,0 @@
{
"50": [
"1482472702807969792",
"1482499205675421699",
"1482563296083394561",
"1482955883315298305",
"1483137146881937412",
"1483378696480116738",
"1483559944079118337",
"1484586634645360640",
"1484496125222535171",
"1484707514847399937",
"1484737641526427651",
"1484979307009114112",
"1485009488952246274",
"1485100025688428544",
"1485371876864004099",
"1485432275433000961",
"1485734211948453889",
"1485945680837283842",
"1485824869111451650",
"1486036193598869507",
"1486519454057971712",
"1486821359636254720",
"1486881762747711489",
"1487123345124429824",
"1486247585249566720",
"1487274426966032386",
"1487334818459242497",
"1487606529884758019",
"1486579857366548480",
"1487968924461195270",
"1488029392081018884",
"1488241425535758337"
],
"100": [
"1482499205675421699",
"1482563296083394561",
"1484586634645360640",
"1485371876864004099",
"1485734211948453889",
"1486519454057971712",
"1487274426966032386",
"1487606529884758019",
"1487968924461195270",
"1488029392081018884"
],
"150": [
"1485371876864004099",
"1487606529884758019",
"1487968924461195270"
],
"200": [
"1487606529884758019",
"1487968924461195270"
],
"400": [],
"500": [],
"600": [],
"700": [],
"800": [],
"900": [],
"1000": []
}

View file

@ -51,7 +51,6 @@ function tweetUrl(tweet: { id_str: string }) {
return `https://twitter.com/winampskins/status/${tweet.id_str}`;
}
const JSON_FILE_NAME = "./popularTweets.json";
const KEY = "tweet_milestones";
// Sort key/value entries by key largest to smallest
@ -64,13 +63,7 @@ export async function popularTweets(handler: DiscordEventHandler) {
const tweets = await getTweets(twitterClient);
const currentJSON = fs.readFileSync(JSON_FILE_NAME, "utf8");
const current: { [bracket: string]: string[] } = JSON.parse(currentJSON);
const kvCurrent = await KeyValue.get(KEY);
if(JSON.stringify(kvCurrent) === JSON.stringify(current)) {
console.warn("KV value does not match!")
}
const current = await KeyValue.get(KEY);
for (const tweet of tweets) {
let notified = false;
@ -93,13 +86,12 @@ export async function popularTweets(handler: DiscordEventHandler) {
}
await KeyValue.set(KEY, current);
fs.writeFileSync(JSON_FILE_NAME, JSON.stringify(current, null, 2));
}
}
}
}
const FOLLOW_COUNT_FILE_NAME = "./followerCount.json";
const FOLLOW_COUNT_KEY = "winamp_skins_twitter_follow_count";
const FOLLOW_COUNT_BRACKET_SIZE = 1000;
export async function followerCount(handler: DiscordEventHandler) {
@ -111,8 +103,7 @@ export async function followerCount(handler: DiscordEventHandler) {
const user = response.data;
const followerCount = user.followers_count;
const currentJSON = fs.readFileSync(FOLLOW_COUNT_FILE_NAME, "utf8");
const current: { count: number } = JSON.parse(currentJSON);
const current: { count: number } = await KeyValue.get(FOLLOW_COUNT_KEY);
const currentNumber = current.count;
const nextMilestone = currentNumber + FOLLOW_COUNT_BRACKET_SIZE;
@ -123,10 +114,7 @@ export async function followerCount(handler: DiscordEventHandler) {
bracket: nextMilestone,
count: followerCount,
});
fs.writeFileSync(
FOLLOW_COUNT_FILE_NAME,
JSON.stringify({ count: nextMilestone }, null, 2)
);
await KeyValue.set(FOLLOW_COUNT_KEY, { count: nextMilestone });
} else {
console.log(`Not notifying: ${followerCount} < ${nextMilestone}`);
}