Created a simple script to send out a Discord notification for a channel's latest video

This commit is contained in:
Gregory Ballantine 2022-03-06 22:13:23 -05:00
parent 3cea90739b
commit 1ab911e7df
7 changed files with 2900 additions and 1 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
# ignore vendored dependencies
vendor/
# ignore config file
config/config.yaml

View File

@ -1,4 +1,4 @@
Copyright (c) <year> <owner>
Copyright (c) 2022 Bit Goblin
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

18
composer.json Normal file
View File

@ -0,0 +1,18 @@
{
"name": "bitgoblin/pigeon",
"description": "Bit Goblin Discord notifier for YouTube uploads",
"type": "project",
"license": "BSD-2-Clause",
"authors": [
{
"name": "Gregory Ballantine",
"email": "gballantine@bitgoblin.tech"
}
],
"require": {
"symfony/yaml": "^6.0",
"hassankhan/config": "^3.0",
"madcoda/php-youtube-api": "^1.2",
"team-reflex/discord-php": "^7.0"
}
}

2816
composer.lock generated Normal file

File diff suppressed because it is too large Load Diff

0
config/.gitkeep Normal file
View File

View File

@ -0,0 +1,8 @@
youtube:
api_key: "supersecretyoutubeapiky"
channel_id: "mychannelid"
discord:
api_key: "supersecretdiscordapikey"
announcement_channel: "general"
message_template: "Hey @everyone {name} has just released a new video over on YouTube! Go check it out! {link}"

51
pigeon.php Normal file
View File

@ -0,0 +1,51 @@
<?php
include __DIR__.'/vendor/autoload.php';
use Discord\Discord;
use Discord\Parts\Channel\Message;
use Discord\WebSockets\Intents;
use Madcoda\Youtube\Youtube;
use Noodlehaus\Config;
// load config file
$conf = new Config('config/config.yaml');
// create YouTube API object
$youtube = new Youtube(array('key' => $conf->get('youtube.api_key')));
// get channel
$yt_res = $youtube->getChannelById($conf->get('youtube.channel_id'));
// get channel info
$channel_name = $yt_res->snippet->title;
$channel_uploads_id = $yt_res->contentDetails->relatedPlaylists->uploads;
// get latest video from uploads playlist
$uploads = $youtube->getPlaylistItemsByPlaylistId($channel_uploads_id);
$latest_video_id = $uploads[0]->contentDetails->videoId;
$latest_url = "https://youtu.be/$latest_video_id";
// instantiate Discord API object
$discord = new Discord([
'token' => $conf->get('discord.api_key'),
'intents' => Intents::getDefaultIntents() | Intents::GUILDS,
]);
// run tasks when Discord is ready
$discord->on('ready', function (Discord $discord) use ($conf, $latest_url) {
echo "Bot is ready!", PHP_EOL;
$message_text = str_replace('{link}', $latest_url, $conf->get('discord.message_template'));
$discord->guilds->first()->channels->find(function ($channel) use ($conf) {
return $channel->name === $conf->get('discord.announcement_channel');
})->sendMessage($message_text)->done(function ($message) use ($discord) {
echo 'Message sent!', PHP_EOL;
$discord->close();
});
});
// run the Discord event loop
$discord->run();