X-Git-Url: https://git.jankratochvil.net/?p=nethome.git;a=blobdiff_plain;f=bin%2Fengine-di.fm;h=45a9968dd829c3b9a5b6b88e5eb9068eacd5c9c3;hp=1cd49e776a701abc92b53cbad53059417f37b902;hb=11606c698522b4651509e39735fc13f96575e262;hpb=1cb27aafc16edec162e03de0dd12921dc68137d9 diff --git a/bin/engine-di.fm b/bin/engine-di.fm index 1cd49e7..45a9968 100755 --- a/bin/engine-di.fm +++ b/bin/engine-di.fm @@ -1,61 +1,176 @@ -#! /bin/bash +#! /usr/bin/perl # # $Id$ +use strict; +use warnings; +require LWP::Simple; +use Data::Dumper; +require HTTP::Headers; +require HTTP::Request; +require LWP::UserAgent; +require HTTP::Cookies; -if [ "$1" = "build" ];then - old="`find -maxdepth 1 '(' -name "di.fm-*" -o -name "sky.fm-*" ')' -type l`" - if [ -z "$old" ];then echo >&2 "No existing symlinks found.";exit 1;fi - for site in di.fm sky.fm;do - newdashed="`wget -q -O - http://$site/|tr ' ' '\n' \ - |perl -n -e ' - s{^.*"(?:http://www.'$site')?([^":]*(?&2 "Failed to download the new listing found.";exit 1;fi - TARGET="engine-di.fm" - if [ ! -x "$TARGET" ];then echo >&2 "Target engine $TARGET not found.";exit 1;fi - rm -f $old - old="" - PREFIX="$site" - for pathdashed in $newdashed;do - test -L "$PREFIX$pathdashed" && continue - ln -s "$TARGET" "$PREFIX$pathdashed" - done - done - echo OK - exit 0 -fi - -set -ex -pathslashed="`basename "$0"|tr - /`" -if false;then - stream="`lynx -source http://di.fm${pathslashed}|sed -n 's/^File1=//p'|tr -d '\r'`" - exec mplayer -ao oss:/dev/dsp -cache 128 "$stream" "$@" - exit 1 -else - streamlist="`lynx -source http://${pathslashed}|sed -n \ - -e 's/^File[0-9]*=//p' \ - -e 's#^.*"\(mms://[^"]*\)".*$#\1#p' \ - |tr -d '\r'`" - for stream in $streamlist;do - # Using "set -e" here: - # mplayer rc 0 on failure to read the stream - # mplayer rc 0 on 'q' - # mplayer rc 1 on ctrl-c - ip="` echo "$stream"|sed -n 's#^http://\([^:/]*\).*$#\1#p'`" - if [ -n "$ip" ];then - port="`echo "$stream"|sed -n 's#^http://[^:/]*:\([0-9][0-9]*\).*$#\1#p'`" - port="${port:-80}" - echo >&2 "Checking $stream -> {$ip}:{$port} ..." - if ! nc -w 2 "$ip" "$port";then - echo >&2 "TIMEOUT: $stream" - continue - fi - fi - mplayer -ao oss:/dev/dsp -cache 128 "$stream" "$@" - done -fi +my $USERNAME="lace"; +my $PASSWORD=&_priv_postget("di.fm.pwd"); + +delete $ENV{"http_proxy"}; + +sub _priv_postget($) +{ +my($base)=@_; + + my $full=$ENV{"HOME"}."/priv/postget.".$base; + local *F; + open F,$full or die "$full: $!"; + my $r=do { local $/; ; } or die "read \"$full\": $!"; + close F or die "close \"$full\": $!"; + chomp $r; + return $r; +} + +sub paidget($$) +{ + my($url,$site)=@_; + + my $ua=LWP::UserAgent->new() or die "UA: $!"; + my $jar=HTTP::Cookies->new() or die "jar: $!"; + $ua->cookie_jar($jar); + push @{$ua->requests_redirectable()},"POST"; + my $req=HTTP::Request->new( + "POST", + q{http://}.$site.q{/pro/login.php}, + HTTP::Headers->new( + "Host"=>$site, + "Content-Type"=>"application/x-www-form-urlencoded", + ), + join('&', + q{amember_login=}.$USERNAME, + q{amember_pass=}.$PASSWORD, + q{refer_back_url=http://}.$site.q{/}, + q{login_attempt_id=}.(0+time()), + q{remember_login=1}, + ), + ); + my $resp=$ua->request($req) or die "resp(0): $!"; + die "!success(0)" if !$resp->is_success(); + die "!content(0)" if !(my $content=$resp->content()); + $content=~m{You will be redirected to protected area} or die $content; + $req=HTTP::Request->new( + "GET", + $url, + ); + $resp=$ua->request($req) or die "resp(1): $!"; + die "!success(1)" if !$resp->is_success(); + die "!content(1)" if !($content=$resp->content()); + return $content; +} + +if ("build" eq ($ARGV[0]||"")) { + my @symlink; + local $_; + while (<*>) { + chomp; + next if !/^(?:\Qdi.fm\E|\Qsky.fm\E)-/; + -l or next; + push @symlink,$_; + } + @symlink or die "No existing symlinks found"; + my $TARGET="engine-di.fm"; + -x $TARGET or die "Target $TARGET not found"; + + sub sitedump($$) + { + my($site,$page)=@_; + + my @newdashed; + while ($page=~m{"((?:http://www.'$site')?([^":]*(?}g) { + my $url=$1; + $url=~s{&}{&}; + push @streamlist,$url; + } + return @streamlist; + } + + my $streamlist=LWP::Simple::get($streamlisturl) or die $streamlisturl; + my @streamlist=streamlist_get $streamlist; + if (!@streamlist) { + $streamlist=~m{Digitally Imported :: Login} or die "No streams found (free): $streamlisturl"; + $streamlist=paidget $streamlisturl,"di.fm"; # FIXME: sky.fm + @streamlist=streamlist_get $streamlist; + @streamlist or die "No streams found (paid): $streamlisturl: $streamlist"; + } + warn Dumper(\@streamlist); + for my $stream (@streamlist) { + if (my($host,$port)=($stream=~m{^http://(?:[^@]+@)?([^:/@]+)(?::(\d+))?})) { + # blacklisted: too slow + next if $host eq "81.92.172.3"; + $tried++; + $port||=80; + warn "Connecting {$host}:{$port}...\n"; + my $tcp=IO::Socket::INET->new( + Proto=>"tcp", + PeerHost=>$host, + PeerPort=>$port, + Timeout=>2, + ) or next; + close $tcp or die "Close TCP: $!"; + } + my $mplayer="mplayer"; + my @list=($mplayer); + if ($pathslashed=~m{/(\d+)k}) { + push @list,"-cache",60*$1/8,"-cache-min",45; + } + push @list,@ARGV; + push @list,$stream; + warn Dumper(\@list); + #system $mplayer @list; + do { die "[$&] <$_>" if m{[^-a-zA-Z 0-9/:.@=]}; } for @list; + system {"bash"} "bash","-c",join(" ",@list); + exit 0 if $?; + } +} while ($tried);