Skip to content

Instantly share code, notes, and snippets.

@gmillerd
Created July 26, 2018 08:46
Show Gist options
  • Select an option

  • Save gmillerd/59e4e7d5a569f86f1d3b86be0acf6f73 to your computer and use it in GitHub Desktop.

Select an option

Save gmillerd/59e4e7d5a569f86f1d3b86be0acf6f73 to your computer and use it in GitHub Desktop.
a simple perl script to download all the video files, as stored natively, in a remote playlist named SYNCME on friend's PLEX server
#!/usr/bin/perl
# todo, visit your plex server, get all friends rather than static url/tok
# todo, maybe fixup the name of the file a bit ... it comes like their layout stores it however
use strict;
use warnings;
use Data::Dumper;
use LWP::UserAgent;
use XML::Simple; # apt install libxml-simple-perl
my $URL='https://ip-ad-ddr-ess.longhashherechageme.plex.direct:port'; # friends url ... this is the base url in every request url in chrome
my $TOK='abcdefghijklmnop-changee'; # your token on friend's system ... this is the "X-Plex-Token: abcdefghijklmnop" in every request if you look in chome
my $ua = LWP::UserAgent->new;
my $syncid = getsyncid();
for my $videoid (getplaylistitems( $syncid )) {
snagfile($videoid);
}
sub snagfile {
my $videoid = shift;
my $res = $ua->get(sprintf("%s/library/metadata/%s?X-Plex-Token=%s",
$URL,
$videoid,
$TOK));
if ($res->is_success) {
my $xml = $res->decoded_content;
my $twig = XMLin($xml);
my $file = $twig->{Video}->{Media}->{Part}->{file};
my $id = $twig->{Video}->{Media}->{Part}->{key};
my $savename = $file;
$savename =~ s!/!_!g;
unless (-f $savename) {
$ua->mirror(sprintf("%s%s?download=1&X-Plex-Token=%s",
$URL,
$id,
$TOK),
$savename);
}
} else {
die $res->status_line;
}
}
sub getplaylistitems {
my $syncid = shift;
my @items = ();
my $res = $ua->get(sprintf("%s/playlists/%s/items?X-Plex-Token=%s",
$URL,
$syncid,
$TOK));
if ($res->is_success) {
my $xml = $res->decoded_content;
my $twig = XMLin($xml);
for my $key (keys $twig->{'Video'}) {
my $h = $twig->{'Video'}->{$key};
#print Dumper($h);
push(@items, $h->{'ratingKey'});
}
return @items;
} else {
die $res->status_line;
}
}
sub getsyncid {
my $res = $ua->get(sprintf("%s/playlists?X-Plex-Token=%s",
$URL,
$TOK));
if ($res->is_success) {
my $xml = $res->decoded_content;
my $twig = XMLin($xml);
for my $playlist (keys $twig->{'Playlist'}) {
my $h = $twig->{'Playlist'}->{$playlist};
if($h->{'title'} eq 'SYNCME') {
# print Dumper($h);
return $h->{'ratingKey'}
}
}
} else {
die $res->status_line;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment