Compare commits
No commits in common. "master" and "v0.2.0" have entirely different histories.
4
.gitignore
vendored
4
.gitignore
vendored
@ -6,7 +6,3 @@ config/config.yaml
|
||||
|
||||
# PHP_Codesniffer cache
|
||||
.phpcs-cache
|
||||
|
||||
# database file
|
||||
data/pigeon.db
|
||||
|
||||
|
@ -13,9 +13,7 @@
|
||||
"symfony/yaml": "^6.0",
|
||||
"hassankhan/config": "^3.0",
|
||||
"madcoda/php-youtube-api": "^1.2",
|
||||
"team-reflex/discord-php": "^7.0",
|
||||
"robmorgan/phinx": "^0.12.10",
|
||||
"illuminate/database": "^9.3"
|
||||
"team-reflex/discord-php": "^7.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"squizlabs/php_codesniffer": "^3.6",
|
||||
@ -24,10 +22,5 @@
|
||||
"scripts": {
|
||||
"phpcs": "phpcs --standard=./phpcs.xml",
|
||||
"phpmd": "phpmd src/ text phpmd.xml"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Pigeon\\": "src"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
1985
composer.lock
generated
1985
composer.lock
generated
File diff suppressed because it is too large
Load Diff
@ -1,7 +1,3 @@
|
||||
pigeon:
|
||||
check_interval: 10
|
||||
db_path: "data/pigeon.db"
|
||||
|
||||
youtube:
|
||||
api_key: "supersecretyoutubeapiky"
|
||||
channel_id: "mychannelid"
|
||||
|
@ -1,29 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use Phinx\Migration\AbstractMigration;
|
||||
|
||||
final class AddVideosTable extends AbstractMigration
|
||||
{
|
||||
/**
|
||||
* Change Method.
|
||||
*
|
||||
* Write your reversible migrations using this method.
|
||||
*
|
||||
* More information on writing migrations is available here:
|
||||
* https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
|
||||
*
|
||||
* Remember to call "create()" or "update()" and NOT "save()" when working
|
||||
* with the Table class.
|
||||
*/
|
||||
public function change(): void
|
||||
{
|
||||
// create the table
|
||||
$table = $this->table('videos');
|
||||
$table->addColumn('video_id', 'string')
|
||||
->addColumn('published_at', 'datetime')
|
||||
->addTimestamps()
|
||||
->addIndex(['video_id'], ['unique' => true])
|
||||
->create();
|
||||
}
|
||||
}
|
28
phinx.php
28
phinx.php
@ -1,28 +0,0 @@
|
||||
<?php
|
||||
|
||||
return
|
||||
[
|
||||
'paths' => [
|
||||
'migrations' => '%%PHINX_CONFIG_DIR%%/db/migrations',
|
||||
'seeds' => '%%PHINX_CONFIG_DIR%%/db/seeds'
|
||||
],
|
||||
'environments' => [
|
||||
'default_migration_table' => 'phinxlog',
|
||||
'default_environment' => 'development',
|
||||
'production' => [
|
||||
'adapter' => 'sqlite',
|
||||
'name' => './data/pigeon',
|
||||
'suffix' => 'db',
|
||||
],
|
||||
'development' => [
|
||||
'adapter' => 'sqlite',
|
||||
'name' => './data/pigeon',
|
||||
'suffix' => 'db',
|
||||
],
|
||||
'testing' => [
|
||||
'adapter' => 'sqlite',
|
||||
'memory' => true,
|
||||
]
|
||||
],
|
||||
'version_order' => 'creation'
|
||||
];
|
42
phpcs.xml
42
phpcs.xml
@ -1,33 +1,27 @@
|
||||
<?xml version="1.0"?>
|
||||
|
||||
<ruleset name="PHP_CodeSniffer">
|
||||
<ruleset name="PHP_CodeSniffer">
|
||||
|
||||
<description>PHPCS configuration file.</description>
|
||||
<description>PHPCS configuration file.</description>
|
||||
|
||||
<arg name="basepath" value="." />
|
||||
<arg name="extensions" value="php" />
|
||||
<arg name="colors" />
|
||||
<arg name="cache" value=".phpcs-cache" />
|
||||
<arg value="p" />
|
||||
<arg value="s" />
|
||||
<arg name="basepath" value="." />
|
||||
<arg name="extensions" value="php" />
|
||||
<arg name="colors" />
|
||||
<arg name="cache" value=".phpcs-cache" />
|
||||
<arg value="p" />
|
||||
<arg value="s" />
|
||||
|
||||
<!-- Check PHP files in the src/ directory -->
|
||||
<file>src/</file>
|
||||
<!-- Check PHP files in the src/ directory -->
|
||||
<file>src/</file>
|
||||
|
||||
<!-- Our base rule: set to PSR12-->
|
||||
<rule ref="PSR12">
|
||||
<exclude name="PSR2.Classes.ClassDeclaration.OpenBraceNewLine" />
|
||||
<exclude name="Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine" />
|
||||
</rule>
|
||||
<!-- Our base rule: set to PSR12-->
|
||||
<rule ref="PSR12"/>
|
||||
|
||||
<!-- Some custom rules -->
|
||||
<rule ref="Generic.Functions.OpeningFunctionBraceKernighanRitchie" />
|
||||
<rule ref="Generic.Classes.OpeningBraceSameLine" />
|
||||
<!-- Set indent size to 2 -->
|
||||
<rule ref="Generic.WhiteSpace.ScopeIndent">
|
||||
<properties>
|
||||
<property name="indent" value="2" />
|
||||
</properties>
|
||||
</rule>
|
||||
<!-- Set indent size to 2 -->
|
||||
<rule ref="Generic.WhiteSpace.ScopeIndent">
|
||||
<properties>
|
||||
<property name="indent" value="2" />
|
||||
</properties>
|
||||
</rule>
|
||||
|
||||
</ruleset>
|
||||
|
@ -1,10 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Pigeon\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Video extends Model {
|
||||
// define SQL attributes that can be modified
|
||||
protected $fillable = ['video_id', 'published_at'];
|
||||
}
|
@ -5,24 +5,12 @@ include __DIR__ . '/../vendor/autoload.php';
|
||||
use Discord\Discord;
|
||||
use Discord\Parts\Channel\Message;
|
||||
use Discord\WebSockets\Intents;
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
use Madcoda\Youtube\Youtube;
|
||||
use Noodlehaus\Config;
|
||||
|
||||
use Pigeon\Models\Video;
|
||||
|
||||
// load config file
|
||||
$config = new Config('config/config.yaml');
|
||||
|
||||
// create Eloquent connection object
|
||||
$capsule = new Capsule();
|
||||
$capsule->addConnection([
|
||||
'driver' => 'sqlite',
|
||||
'database' => $config->get('pigeon.db_path'),
|
||||
]);
|
||||
$capsule->setAsGlobal();
|
||||
$capsule->bootEloquent();
|
||||
|
||||
// create YouTube API object
|
||||
$youtube = new Youtube(array('key' => $config->get('youtube.api_key')));
|
||||
// get channel
|
||||
@ -31,21 +19,6 @@ $yt_res = $youtube->getChannelById($config->get('youtube.channel_id'));
|
||||
$config->set('youtube.channel_name', $yt_res->snippet->title);
|
||||
$config->set('youtube.uploads_id', $yt_res->contentDetails->relatedPlaylists->uploads);
|
||||
|
||||
// get the channel's uploads so we can seed the DB
|
||||
$uploads = $youtube->getPlaylistItemsByPlaylistId($config->get('youtube.uploads_id'));
|
||||
|
||||
// create Video model objects with the above uploads
|
||||
foreach ($uploads as $u) {
|
||||
$video = new Video();
|
||||
$video->video_id = $u->contentDetails->videoId;
|
||||
$video->published_at = date($u->contentDetails->videoPublishedAt);
|
||||
|
||||
// create the table entry if it doesn't already exist
|
||||
Video::where('video_id', $video->video_id)->firstOr(function () use ($video) {
|
||||
$video->save();
|
||||
});
|
||||
}
|
||||
|
||||
// create our event loop
|
||||
$loop = React\EventLoop\Factory::create();
|
||||
|
||||
@ -59,31 +32,21 @@ $discord = new Discord([
|
||||
// periodic youtube uploads check
|
||||
$loop->addPeriodicTimer($config->get('pigeon.check_interval') * 60, function () use ($config, $discord, $youtube) {
|
||||
// get latest video from uploads playlist
|
||||
$uploads = $youtube->getPlaylistItemsByPlaylistId($config->get('youtube.uploads_id'), 1);
|
||||
$uploadDetails = $uploads[0]->contentDetails;
|
||||
$uploads = $youtube->getPlaylistItemsByPlaylistId($config->get('youtube.uploads_id'));
|
||||
$latest_video_id = $uploads[0]->contentDetails->videoId;
|
||||
$latest_url = "https://youtu.be/$latest_video_id";
|
||||
|
||||
// search for the latest video in the DB
|
||||
Video::where('video_id', $uploadDetails->videoId)->firstOr(function () use ($discord, $config, $uploadDetails) {
|
||||
$latest_url = "https://youtu.be/$uploadDetails->videoId";
|
||||
$message_text = str_replace('{link}', $latest_url, $config->get('discord.message_template'));
|
||||
|
||||
$message_text = str_replace('{link}', $latest_url, $config->get('discord.message_template'));
|
||||
|
||||
$discord->guilds->first()->channels->find(function ($channel) use ($config) {
|
||||
return $channel->name === $config->get('discord.announcement_channel');
|
||||
})->sendMessage($message_text)->done(function ($message) use ($discord) {
|
||||
echo 'Message sent!', PHP_EOL;
|
||||
});
|
||||
|
||||
// add the video to the DB
|
||||
$video = new Video();
|
||||
$video->video_id = $uploadDetails->videoId;
|
||||
$video->published_at = date($uploadDetails->videoPublishedAt);
|
||||
$video->save();
|
||||
$discord->guilds->first()->channels->find(function ($channel) use ($config) {
|
||||
return $channel->name === $config->get('discord.announcement_channel');
|
||||
})->sendMessage($message_text)->done(function ($message) use ($discord) {
|
||||
echo 'Message sent!', PHP_EOL;
|
||||
});
|
||||
});
|
||||
|
||||
// run tasks when Discord is ready
|
||||
$discord->on('ready', function (Discord $discord) {
|
||||
$discord->on('ready', function (Discord $discord) use ($conf, $latest_url) {
|
||||
echo "Bot is ready!", PHP_EOL;
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user