Clear old scripts

This commit is contained in:
Timothy Allen 2023-11-24 17:16:46 +02:00
parent ea56d2e85e
commit 41fd89f3f2
3 changed files with 0 additions and 384 deletions

View File

@ -1,141 +0,0 @@
#/bin/bash
#
# sudo pip install --upgrade youtube-dl && sudo pip3 install --upgrade youtube-dl
#
# If you get youtube-dl permissions errors like
# "ImportError: cannot import name 'main'", run
# sudo find /usr/local -type d -exec chmod a+rx {} + && sudo find /usr/local -type f -exec chmod a+r {} + && sudo find /usr/local/bin -type f -exec chmod a+rx {} +
#
TMP_DIR=/tmp/dailyshow
DOWNLOAD_DIR=/media/nas/video/series
#YOUTUBE_DL="/usr/bin/youtube-dl"
YOUTUBE_DL="python3 /usr/local/bin/youtube-dl"
#KEEP_DAYS=40
KEEP_DAYS=100
ARGS=-qwc
if [ ! -d "${DOWNLOAD_DIR}" ]; then
echo "Error: directory not found: ${DOWNLOAD_DIR}"
# exit
fi
OPTIND=1 # Reset is necessary if getopts was used previously
while getopts ":u:d" opt; do
case "${opt}" in
d)
ARGS=-vwc
set -x
;;
u)
url="${OPTARG}"
;;
h)
echo "Usage: ${0} <URL> <-d>"
exit 1
;;
esac
done
shift "$((OPTIND-1))" # Shift off the options and optional --.
# Formats:
# python3 /usr/local/bin/youtube-dl -F http://www.cc.com/shows/the-daily-show-with-trevor-noah/full-episodes/
# [info] Available formats for 097b3593-7592-4fe6-95fe-18d5846fead0:
# format code extension resolution note
# 278 mp4 384x216
# 498 mp4 512x288
# 1028 mp4 640x360
# 1528 mp4 768x432
# 2128 mp4 960x540
# 3128 mp4 1280x720
# 5128 mp4 1920x1080 (best)
#FORMAT="vhttp-750"
#FORMAT="rtmp-750"
#FORMAT="rtmp-3500/rtmp2200"
function download_show {
url="$1"
show="$2"
mkdir -p "${TMP_DIR}/${show}"
# Get a chosen video format for each site
if [[ "${url}" =~ cc.com ]]; then
#FORMAT="1028/907/498/500"
FORMAT="best[width<=?640]"
elif [[ "${url}" =~ cbs.com ]]; then
FORMAT="rtmp-496-0"
elif [[ "${url}" =~ nbc.com ]]; then
FORMAT="best[width<=?640]"
fi
if [ -f "${DOWNLOAD_DIR}/${show}.archive" ]; then
cp "${DOWNLOAD_DIR}/${show}.archive" "${TMP_DIR}/${show}.archive"
fi
# Try and download twice (resume if partially downloaded)
for i in 1 2; do
${YOUTUBE_DL} ${ARGS} -f ${FORMAT} \
-o "${TMP_DIR}/${show}/%(upload_date)s/%(title)s-%(id)s.%(ext)s" \
--download-archive "${TMP_DIR}/${show}.archive" \
${url}
# Wait 10 minutes before trying again
#sleep 600
if [ -d "${TMP_DIR}/${show}/" ] && [ -n "$( ls -A "${TMP_DIR}/${show}/" )" ]; then
mkdir -p "${DOWNLOAD_DIR}/${show}"
cp -nr "${TMP_DIR}/${show}"/* "${DOWNLOAD_DIR}/${show}"/
cp "${TMP_DIR}/${show}.archive" "${DOWNLOAD_DIR}/${show}.archive"
fi
done
# Move files from ${DOWNLOAD_DIR}/The Daily Show/NA/ to ${DOWNLOAD_DIR}/The Daily Show/$date
if [ -d "${DOWNLOAD_DIR}/${show}"/NA ]; then
while IFS= read -r -d '' file; do
dir=$( stat -c %y "$file" | sed -e 's/\([0-9]*\)-\([0-9]*\)-\([0-9]*\).*/\1\2\3/' );
mkdir -p "${DOWNLOAD_DIR}/${show}/${dir}/"
# Keep a copy in /NA/ to avoid re-downloading
cp -n "$file" "${DOWNLOAD_DIR}/${show}/${dir}/"
done < <(find "${DOWNLOAD_DIR}/${show}/NA" -type f -print0)
# Remove old copies in /NA/, since we don't need to
# worry about re-downloading them any more
find "${DOWNLOAD_DIR}/${show}/NA/" -mindepth 1 -mtime +7 -delete
fi
# Remove old shows
if [ -d "${DOWNLOAD_DIR}/${show}" ]; then
find "${DOWNLOAD_DIR}/${show}/" -mindepth 1 -mtime +${KEEP_DAYS} -delete
fi
}
if [[ -n ${url} ]]; then
if [[ "${url}" =~ cc.com ]]; then
show="The Daily Show"
elif [[ "${url}" =~ cbs.com ]]; then
show="The Late Show"
elif [[ "${url}" =~ nbc.com ]]; then
show="Meet the Press"
fi
download_show "${url}" "${show}"
else
#for show in "The Daily Show" "The Late Show" "Meet the Press"; do
for show in "The Daily Show"; do
url=
if [[ "${show}" =~ Daily ]]; then
url=http://www.cc.com/shows/the-daily-show-with-trevor-noah/full-episodes/
elif [[ "${show}" =~ Late ]]; then
# Get URL from http://www.cbs.com/shows/the-late-show-with-stephen-colbert/video/
# Requires Unlocator/Unotelly DNS
#url=$( wget -qO- http://www.cbs.com/shows/the-late-show-with-stephen-colbert/video/ | grep -E "/video/[^/]+/" | grep '"url":' | perl -pe 's/.*?"url":"(.*?)".*/$1/ims' | head -1 )
url=$( wget -qO- http://www.cbs.com/shows/the-late-show-with-stephen-colbert/video/ | grep -Po '"url":.*?[^\\]",' | grep http | perl -pe 's/.*?"url":"(.*?)".*/$1\n/ims' | head -1 )
elif [[ "${show}" =~ Meet ]]; then
# Get URL from https://www.nbc.com/meet-the-press/episodes
# Note that youtube-dl has a bug on https connections: https://github.com/rg3/youtube-dl/issues/13651
url=$( wget -qO- https://www.nbc.com/meet-the-press/episodes | grep -Po '"permalink":.*?[^\\]",' | perl -pe 's/.*?"permalink":"(.*?)".*/$1\n/ims; s/https/http/g' | head -1 )
fi
download_show "${url}" "${show}"
done
fi

View File

@ -1,149 +0,0 @@
#!/usr/bin/perl
# Requires HTML::TableExtract and MIME::Lite
# On Ubuntu/Debian: sudo aptitude install libhtml-tableextract-perl libmime-lite-perl
#
# Downloads, caches, parses and prints the current load shedding schedule in Cape Town
# Written by Timothy Allen <tallen@allafrica.com>
use strict;
use warnings;
use Getopt::Long;
use LWP::Simple;
use MIME::Lite;
use Time::Piece;
use Time::Seconds;
use List::MoreUtils;
use HTML::TableExtract;
my $url = "http://www.capetown.gov.za/en/electricity/Pages/LoadShedding.aspx";
my $schedule_file = "$ENV{HOME}/.loadshedding_schedule";
my @recipients = ();
my @zones;
my $verbose = 0;
GetOptions(
"t|to=s{1,}" => \@recipients,
"v|verbose" => \$verbose,
"z|zones=i{1,}" => \@zones,
) or die "Usage: $0 [-t <email address> ... ] -z <zone number> ...\n";
if (scalar @zones < 1) {
die "Usage: $0 -z <zone numbers>\n";
}
my ($last_schedule, $current_schedule, $last_str, $current_str, $last_time, $current_time, $diff);
$current_time = localtime;
$current_str = $current_time->strftime('%Y-%m-%dT%H:%M:%S%z');
if (-f $schedule_file) {
open STATUS, "<", $schedule_file or die $!;
my @lines = <STATUS>;
close STATUS or die $!;
chomp($last_str = $lines[0]) if ($lines[0] =~ s#^\s*Time:\s*##i);
chomp($last_schedule = join "\n", @lines);
if ($last_str && $last_str =~ /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+\d{4}/) {
$last_time = Time::Piece->strptime($last_str, '%Y-%m-%dT%H:%M:%S%z') or warn "Time error";
$diff = $current_time - $last_time;
} else {
undef $last_str;
}
}
# Only refetch the page every hour
my $cached = 0;
if (!$last_str || int($diff->hours) >= 1) {
my $content = LWP::Simple::get $url or die "Couldn't get $url";
if ($content =~ m#<div[^>]*id="WebPartWPQ2"[^>]*>\s*(<table[^>]*>.*?</table>)#ims) {
$current_schedule = $1;
} else {
die "Unable to parse schedule; load shedding page must have changed.\n";
}
open STATUS, ">", $schedule_file or die $!;
print STATUS "Time: $current_str\n";
print STATUS "$current_schedule\n";
close STATUS or die $!;
} else {
$current_schedule = $last_schedule;
$cached = 1;
}
my ($html, $table);
$current_schedule =~ s#<.?span[^>]*>##imsg;
$current_schedule =~ s#\s*(<[^>]*>)\s*#$1#imsg;
$html = HTML::TableExtract->new(
br_translate => 0,
);
$html->parse($current_schedule);
if (scalar $html->tables() < 1) {
unlink $schedule_file if (-f $schedule_file);
die "No table found";
}
$table = $html->first_table_found();
# Create a hash (ref) of hash (ref)s with the load shedding info
my $shedding = {};
my ($title, $header_row, @rows) = $table->rows;
my ($empty, @headers) = @$header_row;
foreach my $row (@rows) {
my $zone_times = {};
my ($stage, @times) = @$row;
foreach my $i (0 .. $#headers) {
my $key = $headers[$i];
$zone_times->{$key} = $times[$i];
}
$shedding->{$stage} = $zone_times;
}
my $day = shift @$title;
$day =~ s/.*?(\d+\s+(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec).*?\s+\d{4}?).*?/$1/ or undef $day;
my $subject = "Load shedding times";
$subject .= " for $day" if defined $day;
my $output = "If there is load shedding today";
$output .= " ($day)" if defined $day;
$output .= ", it will be on the following schedule. We will alert you if and when the City of Cape Town website announces the actual commencement of load shedding.\n\n";
foreach my $zone (List::MoreUtils::uniq @zones) {
$output .= "Load shedding hours for zone $zone";
$output .= " on $day" if defined $day;
$output .= "\n";
foreach my $stage (sort keys %$shedding) {
# Get applicable sets of hours for this zone in each stage
my @hours;
foreach my $key (sort keys %{$shedding->{$stage}}) {
push @hours, $key if grep /\b$zone\b/, $shedding->{$stage}->{$key};
}
$output .= sprintf "%-10s ", "$stage:";
if (scalar @hours > 0) {
my $str = join ", ", @hours;
$str =~ s/(.*),/$1 and/;
$output .= "$str\n";
} else {
$output .= "No load shedding in zone $zone";
$output .= " for $day" if defined $day;
$output .= "\n";
}
}
$output .= "\n";
}
if ($cached) {
$output .= "Time since last schedule download: " . $diff->pretty . "\n" if $verbose;
}
if (scalar @recipients < 1) {
print $output;
} else {
foreach my $to (@recipients) {
my $message = MIME::Lite->new(
From => 'Loadshedding Alerts <load@node.org.za>',
To => $to,
Subject => $subject,
Data => $output,
);
# send the message
$message->send();
}
}
# vim: set indentexpr= expandtab sw=2 softtabstop=2 tw=10000 :

View File

@ -1,94 +0,0 @@
#!/usr/bin/perl
#
# Downloads, parses and prints the current load shedding status.
# It is suggested that you run this script every 5-20 minutes.
#
# Written by Timothy Allen <tallen@allafrica.com>
use strict;
use warnings;
use Getopt::Long;
use LWP::Simple;
use MIME::Lite;
use Time::Piece;
use Time::Seconds;
my $url = "http://www.capetown.gov.za/en/electricity/Pages/LoadShedding.aspx";
my $status_file = "$ENV{HOME}/.loadshedding_status";
my @recipients = ();
my $verbose = 0;
GetOptions(
"t|to=s{1,}" => \@recipients,
"v|verbose" => \$verbose,
) or die "Usage: $0 [-t <email address> ... ] \n";
my ($last_status, $current_status, $last_str, $current_str, $last_time, $current_time, $diff);
$current_time = localtime;
$current_str = $current_time->strftime('%Y-%m-%dT%H:%M:%S%z');
if (-f $status_file) {
open STATUS, "<", $status_file or die $!;
my @lines = <STATUS>;
close STATUS or die $!;
chomp($last_status = $lines[0]) if ($lines[0] =~ s#^\s*Status:\s*##i);
chomp($last_str = $lines[1]) if ($lines[1] =~ s#^\s*Time:\s*##i);
if ($last_str) {
$last_time = Time::Piece->strptime($last_str, '%Y-%m-%dT%H:%M:%S%z');
$diff = $current_time - $last_time;
}
}
my ($content, $output);
unless ($content = get $url) {
warn "Couldn't get $url";
$content = get "http://www.capetown.gov.za/loadshedding/Loadshedding.html" or die "Couldn't get alternate url";
}
if ($content =~ m#<div[^>]*id="WebPartWPQ3"[^>]*>.*?<table[^>]*>(.*?)</table>#ims) {
$current_status = $1;
$current_status =~ s#\s*(<br[^>]*>|\n)\s*# #imsg;
$current_status =~ s#<.*?>##imsg;
} elsif ($content =~ m#<div[^>]*class="MainHeadText"[^>]*>.{0,1500}?<div style="display:block" class="alertbox">(.*?)</div>#ims) {
$current_status = $1;
$current_status =~ s#\s*(<br[^>]*>|\n)\s*# #imsg;
$current_status =~ s#<.*?>##imsg;
} else {
die "Unable to parse status; load shedding page must have changed.\n";
}
if (($current_status !~ /no items/i) &&
(!defined $last_status || $current_status ne $last_status)) {
open STATUS, ">", $status_file or die $!;
print STATUS "Status: $current_status\n";
print STATUS "Time: $current_str\n";
close STATUS or die $!;
if (defined $last_status) {
$output .= "The City of Cape Town's website indicates that the load shedding status has changed from: $last_status to: $current_status\n";
} else {
$output .= "Load shedding status for the City of Cape Town: $current_status\n";
}
$output .= "Time since last change: " . $diff->pretty . "\n" if defined $diff and $verbose;
} else {
$output .= "Load shedding status: $current_status\n" if $verbose;
}
if (defined $output) {
if (scalar @recipients < 1) {
print $output;
} else {
foreach my $to (@recipients) {
my $message = MIME::Lite->new(
From => 'Loadshedding Alerts <load@node.org.za>',
To => $to,
Subject => 'Load shedding status change',
Data => $output,
);
# send the message
$message->send();
}
}
}
# vim: set indentexpr= expandtab sw=2 softtabstop=2 tw=10000 :