Skip to content

Instantly share code, notes, and snippets.

@z448
Last active December 5, 2016 19:03
Show Gist options
  • Save z448/abf2cda1e46398174759d7a8a478adb3 to your computer and use it in GitHub Desktop.
Save z448/abf2cda1e46398174759d7a8a478adb3 to your computer and use it in GitHub Desktop.
iOS client to watch MovieBox content on Linux/Mac without any additional service/receiver
#!/usr/local/bin/perl
use 5.010;
use warnings;
use strict;
use Getopt::Std;
use Filesys::Notify::Simple;
use File::Path;
use JSON::PP;
use Net::OpenSSH;
my $opt = {};
getopts('s:', $opt);
# get config
my $c = decode_json scalar <DATA>;
# create 'run command' in activator
sub setup_activator {
my $cache = $c->{ios}->{activator_cache};
my $plutil = '/usr/local/lib/perl5/site_perl/5.22.0/armv7-apple-darwin/App/moviebox-connect/plutil';
if(`activator get LACommandListeners` =~ /sh\.load\.moviebox-connect/){ return }
system("$plutil -key LACommandListeners -dict $cache") unless `activator get LACommandListeners`;
system("$plutil -key LACommandListeners -key libactivator.command.run.sh.load.moviebox-connect -dict $cache");
system("$plutil -key LACommandListeners -key libactivator.command.run.sh.load.moviebox-connect -key title -value moviebox-connect $cache");
system("$plutil -key LACommandListeners -key libactivator.command.run.sh.load.moviebox-connect -key command -value \"moviebox-connect -start\" $cache");
}
start() if defined $opt->{s};
#system("touch $c->{ios}->{url}") unless -f $c->{ios}->{url};
#system("touch $c->{ios}->{old_cache}") unless -f $c->{ios}->{old_cache};
watch();
sub start {
my $pids = ' ';
my @ps = grep{ /moviebox-connect/ } qx{ ps aux };
for(@ps){ push my @pid, split(" ", $_); $pids .= " $pid[1]" unless $pid[1] eq $$ }
if($opt->{s} eq 'etup'){ setup_activator(); exit }
if($opt->{s} eq 'top'){ system("kill -9 $pids");say "killed: $pids"; die bless [], 'Dies'; }
if($opt->{s} eq 'tatus'){
if($pids eq ' '){ say "$0: not running" } else { say "$0: $pids" }
}
if($opt->{s} eq 'tart' or $opt->{s} eq 'how'){ vlc() }
die bless [], 'Dies';
package Dies;
use overload '""' => 'dies';
sub dies { return '' }
}
# get video url
sub url {
system("cp $c->{ios}->{cache} $c->{ios}->{old_cache}") if -f $c->{ios}->{cache};
open(my $fh,'<',$c->{ios}->{old_cache});
while(my $line = <$fh>){
if($line =~ /\.mp4/){
$line =~ s/(.*?)(http:\/\/.*?)(<\/.*)/$2/;
chomp($line);
open(my $tmp,'>',"$c->{ios}->{url}");
print $tmp $line;
close $tmp;
system("rm -rf /tmp/MediaCache/*");
return;
}
}
}
# open vlc on remote host
sub vlc {
open(my $fh,'<',"$c->{ios}->{url}");
my $url = <$fh>;
close $fh;
my $user;
if(-f $c->{ios}->{user}){
local $/;
open(my $fh,'<', $c->{ios}->{user});
$user = decode_json scalar <$fh>;
close $fh;
}
if($opt->{s} eq 'how'){ print $url and return }
system("echo \'$c->{playlist}->{head}$url$c->{playlist}->{tail}\' > /tmp/moviebox.xspf");
my $open = 'export DISPLAY=:0 && vlc -f'; $open = 'open' if $user->{os} eq 'osx';
my @cmd = ( "echo \'$c->{playlist}->{head}$url$c->{playlist}->{tail}\' > ~/Desktop/moviebox.xspf && $open ~/Desktop/moviebox.xspf > /dev/null 2>&1" );
my $ssh;
$ssh = Net::OpenSSH->new("$user->{username}:$user->{password}\@$user->{hostname}");
$ssh->capture(@cmd);
die;
}
sub watch {
$| = 1;
my $watcher = Filesys::Notify::Simple->new([ '/tmp/MediaCache' ]);
while(1){ $watcher->wait( sub{ url() for @_ }) }
}
=head1 NAME
moviebox-connect
=cut
=head1 SYNOPSIS
Play MovieBox content in VLC on Linux or OSX.
=cut
=head1 USAGE
Fill details of your computer in ~/moviebox-connect.txt
Print video URL
moviebox-connect -show
See status ( shows PID or 'not running' )
moviebox-connect -status
Stop. This will stop current proceess and LaunchDaemon will start new one. To also stop background daemon unload it ( launchctl -unload -w /Library/LaunchDaemons/sh.load.moviebox-connect )
moviebox-connect -stop
Start video in VLC on remote computer.
moviebox-connect -start
Setup activator action
moviebox-connect -setup && reboot
Start background process manually. moviebox-connect is running in background, no need to start it. Use this option for troubleshooting/development.
fill remote host details in ~/moviebox-connect.txt and start without any option
moviebox-connect
=cut
# todo
# activator send libactivator.sms.compose-message
# moviebox-connect -l | pbcopy
__DATA__
{"ios":{"cache":"/private/var/tmp/MediaCache/diskcacherepository.plist","old_cache":"/tmp/.moviebox-connect.old_cache","playlist":"/private/var/tmp/moviebox.xspf","url":"/private/var/tmp/.moviebox-connect.url","user":"/private/var/mobile/moviebox-connect.txt","activator_cache":"/private/var/mobile/Library/Caches/libactivator.plist","user_template":"{\"username\":\"fill\",\"hostname\":\"fill\",\"password\":\"fill\"}"},"playlist":{"head":"<?xml version=\"1.0\" encoding=\"UTF-8\"?><playlist xmlns=\"http://xspf.org/ns/0/\" xmlns:vlc=\"http://www.videolan.org/vlc/playlist/ns/0/\" version=\"1\"><title>Playlist</title><trackList><track><location>","url":"","tail":"</location><extension application=\"http://www.videolan.org/vlc/playlist/0\"><vlc:id>0</vlc:id><vlc:option>network-caching=1000</vlc:option></extension></track></trackList><extension application=\"http://www.videolan.org/vlc/playlist/0\"><vlc:item tid=\"0\"/></extension></playlist>"}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment