-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
152 lines (139 loc) · 3.74 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import ffmpeg from "fluent-ffmpeg";
import dotenv from "dotenv";
import imagemin from "imagemin";
import imageminPngquant from "imagemin-pngquant";
import { createClient } from "@supabase/supabase-js";
import { TwitterApi as TwitterClientV2 } from "twitter-api-v2";
// import { generateTimestamps } from "./generateTimestamps";
dotenv.config();
const twitterClient = new TwitterClientV2({
appKey: process.env.TWITTER_API_KEY ?? "",
appSecret: process.env.TWITTER_API_SECRET ?? "",
accessToken: process.env.TWITTER_ACCESS_TOKEN,
accessSecret: process.env.TWITTER_ACCESS_TOKEN_SECRET,
});
const supabase = createClient(
process.env.SUPABASE_URL ?? "",
process.env.SUPABASE_TOKEN ?? ""
);
interface RandomRow {
id: number;
position: number;
episodes: {
title: string;
link: string;
};
}
const fetchRandomRow = async () => {
// Query for creating the random timestamps view:
// create view random_timestamps as select * from timestamps order by random()
const { data, error } = await supabase
.from<RandomRow>("random_timestamps")
.select(
`
id,
position,
episodes (
title,
link
)
`
)
.limit(1)
.single();
if (error || !data) {
console.log(error);
throw error;
}
console.log(
`Random timestamp row title ${data.episodes.title} - ${data.position}`
);
return data;
};
const takeScreenshotAndSaveImage = (timestamp: number, episodeURL: string) => {
return new Promise((resolve, reject) => {
new ffmpeg(episodeURL)
.takeScreenshots({
count: 1,
timemarks: [`${timestamp}`],
filename: "screenshot",
})
.on("end", function () {
console.log("Screenshot taken");
resolve(null);
})
.on("error", function (err) {
console.log("An error occurred: " + err.message);
reject(err);
});
});
};
const tweetImage = async () => {
const mediaId = await twitterClient.v1.uploadMedia('./screenshot.png');
console.log("Uploaded image to Twitter");
const tweetData = await twitterClient.v2.tweet({
text: "",
media: {
media_ids: [mediaId],
},
});
if (tweetData.errors) {
console.log(tweetData.errors);
throw JSON.stringify(tweetData.errors);
}
console.log("Tweeted image");
return tweetData;
};
const removeRowFromDatabase = async (id: number) => {
const { error } = await supabase.from("timestamps").delete().match({ id });
if (error) {
console.log(error);
throw error;
}
console.log("Removed row from database");
};
const compressImage = async () => {
await imagemin(["./screenshot.png"], {
destination: "./",
plugins: [imageminPngquant()],
});
console.log("Compressed png");
};
const prependZeroIfSingleDigit = (number: number) => {
return ("0" + number).slice(-2);
};
const replyToTweet = async ({
tweetId,
episodeTitle,
position,
}: {
tweetId: string;
episodeTitle: string;
position: number;
}) => {
const timestamp = `${prependZeroIfSingleDigit(
Math.floor(position / 60)
)}:${prependZeroIfSingleDigit(position % 60)}`;
const reply = `${episodeTitle} [${timestamp}] #arcane`;
await twitterClient.v2.reply(`@arcane_frames ${reply}`, tweetId);
console.log(`Replied to tweet - ${reply}`);
};
const screenshotAndTweet = async () => {
try {
const data = await fetchRandomRow();
await takeScreenshotAndSaveImage(data.position, data.episodes.link);
await compressImage();
const tweetData = await tweetImage();
await removeRowFromDatabase(data.id);
await replyToTweet({
tweetId: tweetData.data.id,
episodeTitle: data.episodes.title,
position: data.position,
});
} catch (error) {
console.log(error);
throw error;
}
};
screenshotAndTweet();
// generateTimestamps();