ytyt-legacy/bin/add_youtube_subscription.pl

71 lines
2.5 KiB
Perl
Executable File

#!/usr/bin/perl
use warnings;
use strict;
use lib '../lib';
use Data::Dump qw% dump %;
use YTYT;
my %params;
# Setup YouTube object
my $youtube = YTYT::new( host => 'www.youtube.com' );
my $content;
unless (defined $ARGV[0]) {
die "Requires at least one argument with a User ID, Channel ID or URL string\n" .
"with either of these values included. RegEx argument optional" . (scalar @ARGV) . "\n";
}
if (defined $ARGV[1]) {
$params{regex} = $ARGV[1];
}
if ($ARGV[0] =~ m%^UC[a-zA-Z0-9\-\_]{22}$%) {
$params{channelId} = $ARGV[0];
#print "Found raw channelId " . $params{channelId} . "\n";
} elsif ($ARGV[0] =~ m%^(?:(?:https://)?(?:www\.|m\.)?youtube\.com/)?\@([^\/]+)%) {
$params{channelHandle} = $1;
#die("Handles are not yet supported. Found \@$params{channelHandle}\n");
} elsif ($ARGV[0] =~ m%^(https://)?(www\.|m\.)?youtube\.com/channel/UC[a-zA-Z0-9\-\_]{22}%) {
$params{channelId} = $ARGV[0];
$params{channelId} =~ s%(https://)?(www\.|m\.)?youtube\.com/channel/(UC[^/]{22})(/.*)?%$3%;
#print "Found URL encoded channelId " . $params{channelId} . "\n";
} elsif ($ARGV[0] =~ m%^(https://)?(www\.|m\.)?youtube\.com/c/%) {
$params{channelName} = $ARGV[0];
$params{channelName} =~ s%(https://)?(www\.|m\.)?youtube\.com/c/([^/]*)/?.*%$4%;
#print "Found URL encoded channelName " . $params{channelName} . "\n";
} elsif ($ARGV[0] =~ m%^(https://)?(www\.|m\.)?youtube\.com/user/%) {
$params{userName} = $ARGV[0];
$params{userName} =~ s%(https://)?(www\.|m\.)?youtube\.com/user/([^/]*)/?.*%$4%;
#print "Found URL encoded channelName " . $params{userName} . "\n";
} else {
$params{channelName} = $ARGV[0];
#print "Found raw channelName " . $params{channelName} . "\n";
}
$content = $youtube->get_videos_page( %params );
unless (defined $params{channelName}) {
$params{channelName} = $content->{header}->{c4TabbedHeaderRenderer}->{title};
}
unless (defined $params{channelId}) {
$params{channelId} = $content->{header}->{c4TabbedHeaderRenderer}->{channelId};
}
$params{channelThumbnail} = $content->{header}->{c4TabbedHeaderRenderer}->{avatar}->{thumbnails}[0]->{url};
$youtube->db_connect();
my @check = $youtube->{dbh}->selectrow_array("SELECT channelId FROM channels WHERE channelId = '$params{channelId}';");
if (scalar @check) {
$youtube->db_disconnect();
die "Channel already exists in database.\n";
}
$youtube->db_insert( table => 'channels', %params ) || die "Failed to insert: $!\n";
$youtube->db_disconnect();
print "Channel added successfully\n";