This commit is contained in:
132
Plugin.pm
Normal file
132
Plugin.pm
Normal file
@@ -0,0 +1,132 @@
|
||||
package Plugins::YouTubeMusic::Plugin;
|
||||
|
||||
use strict;
|
||||
|
||||
use base qw(Slim::Plugin::OPMLBased);
|
||||
|
||||
use File::Spec::Functions qw(catdir);
|
||||
use FindBin qw($Bin);
|
||||
use lib catdir($Bin, 'Plugins', 'YouTubeMusic', 'lib');
|
||||
use YTMusicAPI qw(YTMusic);
|
||||
|
||||
use Slim::Utils::Strings qw(string);
|
||||
use Slim::Utils::Prefs;
|
||||
use Slim::Utils::Log;
|
||||
use Slim::Utils::Cache;
|
||||
|
||||
use Plugins::YouTubeMusic::OAuth2;
|
||||
|
||||
my $log = Slim::Utils::Log->addLogCategory(
|
||||
{
|
||||
'category' => 'plugin.youtubemusic',
|
||||
'defaultLevel' => 'WARN',
|
||||
'description' => 'PLUGIN_YOUTUBEMUSIC',
|
||||
}
|
||||
);
|
||||
|
||||
my $prefs = preferences('plugin.youtubemusic');
|
||||
my $cache = Slim::Utils::Cache->new();
|
||||
|
||||
$prefs->init(
|
||||
{
|
||||
testPref => 'test'
|
||||
}
|
||||
);
|
||||
|
||||
sub initPlugin {
|
||||
my $class = shift;
|
||||
|
||||
$class->SUPER::initPlugin(
|
||||
feed => \&mainMenu,
|
||||
tag => 'youtubemusic',
|
||||
menu => 'radios',
|
||||
is_app => 1,
|
||||
weight => 1,
|
||||
);
|
||||
|
||||
if (main::WEBUI) {
|
||||
require Plugins::YouTubeMusic::Settings;
|
||||
Plugins::YouTubeMusic::Settings->new();
|
||||
}
|
||||
}
|
||||
|
||||
sub shutdownPlugin {
|
||||
|
||||
}
|
||||
|
||||
sub getDisplayName { 'PLUGIN_YOUTUBEMUSIC' }
|
||||
|
||||
sub mainMenu {
|
||||
my ( $client, $callback, $args ) = @_;
|
||||
|
||||
my $yt = YTMusicAPI::YTMusic->new();
|
||||
my $song = $yt->get_song("CF1rt_9pdgU");
|
||||
my $title = $song->{"videoDetails"}->{"title"};
|
||||
|
||||
$callback->(
|
||||
[
|
||||
{
|
||||
name => cstring( $client, 'PLUGIN_YOUTUBEMUSIC_MYPLAYLISTS' ),
|
||||
type => 'url',
|
||||
url => \&myPlaylistHandler,
|
||||
passthrough => [ { count => 2 } ]
|
||||
},
|
||||
{
|
||||
name => $title,
|
||||
type => 'text'
|
||||
}
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
sub myPlaylistHandler {
|
||||
my ( $client, $cb, $args, $params ) = @_;
|
||||
|
||||
if ( !$cache->get('yt:access_token') ) {
|
||||
Plugins::YouTubeMusic::OAuth2::getToken( \&myPlaylistHandler, @_ );
|
||||
return;
|
||||
}
|
||||
|
||||
my $account = {
|
||||
_cache_ttl => 60,
|
||||
_noKey => 1,
|
||||
mine => 'true',
|
||||
access_token => $cache->get('yt:access_token'),
|
||||
};
|
||||
|
||||
Plugins::YouTubeMusic::API->searchDirect(
|
||||
'playlists',
|
||||
sub {
|
||||
$cb->( _renderList( $_[0], 'title', $account ) );
|
||||
},
|
||||
{
|
||||
%$account,
|
||||
_index => $args->{index},
|
||||
_quantity => $args->{quantity},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
sub _renderList {
|
||||
my ( $list, $sort, $passthrough, $tags ) = @_;
|
||||
my $sortedList = $list->{items};
|
||||
my @items;
|
||||
|
||||
for my $entry (@$sortedList) {
|
||||
my $snippet = $entry->{snippet} || next;
|
||||
my $title = $snippet->{title} || next;
|
||||
|
||||
my $item = {
|
||||
name => $title,
|
||||
type => 'playlist',
|
||||
};
|
||||
|
||||
push @items, $item;
|
||||
}
|
||||
|
||||
$list->{items} = \@items;
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
1;
|
||||
Reference in New Issue
Block a user