From 5a4ecf632c043873e0f65004e0de9b6580507dd2 Mon Sep 17 00:00:00 2001 From: sergiotarxz Date: Thu, 3 Mar 2022 17:23:24 +0100 Subject: [PATCH] Adding initial tdt watcher. --- Build.PL | 13 +++++ bin/tdtcli.pl | 137 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/TDT/Cli.pm | 8 +++ 3 files changed, 158 insertions(+) create mode 100644 Build.PL create mode 100644 bin/tdtcli.pl create mode 100644 lib/TDT/Cli.pm diff --git a/Build.PL b/Build.PL new file mode 100644 index 0000000..82b0cd5 --- /dev/null +++ b/Build.PL @@ -0,0 +1,13 @@ +use Module::Build; + +my $home = $ENV{HOME}; + +my $build = Module::Build->new( + module_name => 'TDT::Cli', + license => 'AGPLv3', + requires => { + 'Mojolicious' => 0, + 'JSON' => 0, + }, +); +$build->create_build_script; diff --git a/bin/tdtcli.pl b/bin/tdtcli.pl new file mode 100644 index 0000000..cb327b9 --- /dev/null +++ b/bin/tdtcli.pl @@ -0,0 +1,137 @@ +#!/usr/bin/env perl + +use v5.30.0; + +use strict; +use warnings; + +use Data::Dumper; +use Term::ReadLine; +use FileHandle; + +use Mojo::UserAgent; +use JSON; + +binmode STDOUT, ':utf8'; + +my $ua = Mojo::UserAgent->new; +my $tdt_channels = decode_json( + $ua->get('https://www.tdtchannels.com/lists/tv.json')->result->body ); +my $epg = $tdt_channels->{epg}{json}; +my $countries = $tdt_channels->{countries}; + +my $selected_country; +my $selected_ambit; + +my $term = Term::ReadLine->new('tdtcli'); + +while ( defined( my $line = $term->readline('~> ') ) ) { + proccess_line($line); + $term->addhistory($line); + + if ( defined $selected_country ) { + say 'Selected country: ' . $countries->[$selected_country]{name}; + } + if ( defined $selected_ambit ) { + say 'Selected ambit: ' + . $countries->[$selected_country]{ambits}[$selected_ambit]{name}; + } +} + +sub proccess_line { + my $line = shift; + return unless defined $line; + my @line = split /\s+/, $line; + return unless defined $line[0]; + my $command = shift @line; + show_countries() if $command eq 'countries'; + select_country( [@line] ) if $command eq 'select_country'; + show_ambits() if $command eq 'ambits'; + select_ambit( [@line] ) if $command eq 'select_ambit'; + show_channels() if $command eq 'channels'; + select_channel( [@line] ) if $command eq 'select_channel'; +} + +sub select_channel { + my $arguments = shift; + say('You must pass a channel number.'), return + unless exists $arguments->[0]; + my $channel = $arguments->[0]; + return unless $channel =~ /^\d+$/; + say('You must first select a ambit with /select_ambit'), + return undef $selected_ambit + unless defined $selected_ambit; + my $ambits = $countries->[$selected_country]{ambits}; + my $channels = $ambits->[$selected_ambit]{channels}; + my $current_channel_options = $channels->[$channel]{options}; + say('This channel doesn\'t provides an option to see online.'), return + unless scalar @$current_channel_options; + + if (fork) { + return; + } + close(STDOUT); + close(STDERR); + close(STDIN); + system 'mpv', $current_channel_options->[0]{url}; +} + +sub show_channels { + say('You must first select a ambit with /select_ambit'), return + unless defined $selected_ambit; + my $names = + [ map { $_->{name} } + $countries->[$selected_country]{ambits}[$selected_ambit]{channels} + ->@* ]; + my $i = 0; + for my $name (@$names) { + say "$i : $name"; + $i++; + } +} + +sub select_ambit { + my $arguments = shift; + say('You must first select a country with /select_country'), return + unless defined $selected_country; + say('No ambit passed see /ambits'), return + unless defined $arguments->[0]; + my $ambit = $arguments->[0]; + return unless $ambit =~ /^\d+$/; + say('Such ambit doesn\'t exists see /ambits'), return + unless exists $countries->[$selected_country]{ambits}[$ambit]; + $selected_ambit = $ambit; +} + +sub select_country { + my $arguments = shift; + say('No country passed see /countries'), return + unless defined $arguments->[0]; + my $country = $arguments->[0]; + return unless $country =~ /^\d+$/; + say('Such country doesn\'t exists see /countries'), return + if !exists $countries->[$country]; + $selected_country = $country; + undef $selected_ambit; +} + +sub show_countries { + my $names = [ map { $_->{name} } @$countries ]; + my $i = 0; + for my $name (@$names) { + say "$i : $name"; + $i++; + } +} + +sub show_ambits { + say('You must set first a country'), return + if !defined $selected_country; + my $names = + [ map { $_->{name} } $countries->[$selected_country]{ambits}->@* ]; + my $i = 0; + for my $name (@$names) { + say "$i : $name"; + $i++; + } +} diff --git a/lib/TDT/Cli.pm b/lib/TDT/Cli.pm new file mode 100644 index 0000000..69f5bae --- /dev/null +++ b/lib/TDT/Cli.pm @@ -0,0 +1,8 @@ +package TDT::Cli; + +use v5.30.0; + +use strict; +use warnings; + +our $VERSION = '0.0.1';