Skip to content

Instantly share code, notes, and snippets.

@yath
Created August 28, 2011 07:31
Show Gist options
  • Save yath/1176359 to your computer and use it in GitHub Desktop.
Save yath/1176359 to your computer and use it in GitHub Desktop.
Watch for changes in Kabel Deutschland's analog TV channel assignment
#!/bin/sh
MYDIR="$(dirname "$(readlink -f "$0")")"
OUTFILE="$HOME/kd-stations.txt"
if [ -e "$OUTFILE.new" ]; then
echo "$OUTFILE.new already exists" >&2
exit 1
fi
"$MYDIR/kdinfo" > "$OUTFILE.new"
rc=$?
if [ "$rc" -ne 0 ]; then
echo "kdinfo exited with return code $rc" >&2
cat "$OUTFILE.new"
rm "$OUTFILE.new"
fi
if [ ! -e "$OUTFILE" ]; then
cat "$OUTFILE.new"
else
# keep the old file if there is no difference to preserve mtime
diff -u "$OUTFILE" "$OUTFILE.new" && rm "$OUTFILE.new" && exit 0
fi
mv "$OUTFILE.new" "$OUTFILE"
#!/usr/bin/perl
# Fetch Kabel Deutschland's analog TV station list for a given address
use 5.12.0;
use utf8;
use warnings;
use open qw<:encoding(UTF-8) :std>;
use Encode;
BEGIN {
@ARGV = map { Encode::decode("UTF-8", $_) } @ARGV
if grep /\P{ASCII}/, @ARGV;
}
use WWW::Mechanize;
use HTML::TreeBuilder::XPath;
use Unicode::Collate;
my $mech = WWW::Mechanize->new();
$mech->agent_alias("Linux Mozilla");
$mech->get("https://www.kabeldeutschland.de/info-service/sender-suche.html");
$mech->submit_form(
form_name => "formSender",
fields => {
serviceAdressZipCode => 12345,
serviceAdressCity => "Foo",
serviceAdressStreet => "Barstr.",
serviceAdressStreetNo => 23
});
my $tree = HTML::TreeBuilder::XPath->new;
$tree->parse($mech->content) or die "Unable to parse content";
$tree->eof;
my %stations;
foreach my $station ($tree->findnodes(
'//div[@id="analogtv"][div/img[@src=~/analoge_tvsender/]]//div[@class=~/^row/]')) {
my @ret = map { $_->as_trimmed_text } $station->findnodes(".//div");
die "Parse error: \@ret == [@ret]" unless @ret == 2;
my ($name, $channel) = @ret;
push(@{$stations{$channel}}, $name);
}
my $coll = Unicode::Collate->new;
for my $channel (sort keys %stations) {
local $, = "\t";
say $channel, join(", ", $coll->sort(@{$stations{$channel}}));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment