-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4fac5b6
commit 7a6677c
Showing
12 changed files
with
193 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Twitter API parameters | ||
TWITTER_CONSUMER_KEY= | ||
TWITTER_CONSUMER_SECRET= | ||
TWITTER_ACCESS_TOKEN_KEY= | ||
TWITTER_ACCESS_TOKEN_SECRET= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ dist-ssr | |
coverage | ||
*.local | ||
|
||
.env | ||
docker-compose.override.yml | ||
|
||
/cypress/videos/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import dotenv from 'dotenv'; | ||
import { sendTweets } from './twitter/index.mjs'; | ||
|
||
dotenv.config(); | ||
|
||
const actions = { | ||
twitter: sendTweets, | ||
} | ||
|
||
const command = process.argv[2]; | ||
const action = actions[command]; | ||
if (action) { | ||
action(); | ||
} else { | ||
console.error(`Unrecognized command: ${command}`); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export default class Media | ||
{ | ||
file = null; | ||
type = 'image/png'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export default class Tweet | ||
{ | ||
status = null; | ||
media = []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { TwitterApi } from "twitter-api-v2"; | ||
import Tweet from "./Tweet.mjs"; | ||
|
||
export default class TwitterClient | ||
{ | ||
/** @var {TwitterApi} */ | ||
#api; | ||
|
||
constructor() { | ||
this.#api = new TwitterApi({ | ||
appKey: process.env.TWITTER_CONSUMER_KEY, | ||
appSecret: process.env.TWITTER_CONSUMER_SECRET, | ||
accessToken: process.env.TWITTER_ACCESS_TOKEN_KEY, | ||
accessSecret: process.env.TWITTER_ACCESS_TOKEN_SECRET, | ||
}); | ||
} | ||
|
||
/** | ||
* @param {Tweet} tweet | ||
*/ | ||
async send(tweet) { | ||
// Upload images | ||
let mediaIds = await Promise.all( | ||
tweet.media.map(m => this.#api.v1.uploadMedia(m.file, { mimeType: m.type})) | ||
); | ||
|
||
// Send tweet | ||
await this.#api.v1.tweet(tweet.status, { media_ids: mediaIds }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import TweetGenerator from "./generators/TweetGenerator.mjs"; | ||
import TwitterClient from "./TwitterClient.mjs"; | ||
|
||
export default class TwitterManager | ||
{ | ||
/** @var {TweetGenerator[]} */ | ||
generators; | ||
|
||
/** @var {TwitterClient} */ | ||
client; | ||
|
||
constructor(generators = []) { | ||
this.generators = generators; | ||
this.client = new TwitterClient; | ||
} | ||
|
||
async sendTweets() { | ||
for (let generator of this.generators) { | ||
if (!(await generator.shouldTweet())) { | ||
continue; | ||
} | ||
|
||
let tweet = await generator.getTweet(); | ||
|
||
await this.client.send(tweet); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import TweetGenerator from "./TweetGenerator.mjs"; | ||
import { captureScreenshot } from "../../screenshots/captureScreenshot.mjs"; | ||
import Media from "../Media.mjs"; | ||
|
||
const releaseDate = new Date('2022-09-09'); | ||
|
||
export default class CountdownTweet extends TweetGenerator | ||
{ | ||
shouldTweet() { | ||
const now = new Date; | ||
const cutoff = new Date('2022-09-10'); | ||
|
||
return now < cutoff; | ||
} | ||
|
||
_getStatus() { | ||
const now = new Date; | ||
const ms = releaseDate - now; | ||
let days = Math.max(0, Math.ceil(ms / 1000 / 60 / 60 / 24)); | ||
|
||
switch (days) { | ||
case 0: return 'Splatoon 3 is NOW RELEASED! https://splatoon3.ink #Splatoon3'; | ||
case 1: return 'Splatoon 3 releases in 1 DAY! https://splatoon3.ink #Splatoon3'; | ||
default: return `Splatoon 3 releases in ${days} days! https://splatoon3.ink #Splatoon3`; | ||
} | ||
} | ||
|
||
async _getMedia() { | ||
let media = new Media; | ||
media.file = await captureScreenshot('countdown'); | ||
|
||
return media; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import Tweet from '../Tweet.mjs'; | ||
|
||
export default class TweetGenerator | ||
{ | ||
async shouldTweet() { | ||
return true; | ||
} | ||
|
||
async getTweet() { | ||
const tweet = new Tweet; | ||
tweet.status = await this._getStatus(); | ||
|
||
let media = await this._getMedia(); | ||
if (media && !Array.isArray(media)) { | ||
media = [media]; | ||
} | ||
tweet.media = media; | ||
|
||
return tweet; | ||
} | ||
|
||
async _getStatus () { | ||
// | ||
} | ||
|
||
async _getMedia() { | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import CountdownTweet from "./generators/CountdownTweet.mjs"; | ||
import TwitterManager from "./TwitterManager.mjs" | ||
|
||
export function sendTweets() { | ||
const manager = new TwitterManager([ | ||
new CountdownTweet, | ||
]); | ||
|
||
return manager.sendTweets(); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters