[mythtv-users] Apple Trailer Downloader

John Payne mythtv at payne.ch
Tue Jun 24 18:34:53 UTC 2008

I really liked the Myth Apple Trailers 'plugin' but I wanted to see 
plot, cast, rating etc in the basic display, before I started the actual 
trailer. Rather than try to write a real plugin I created an Apple 
Trailer downloader that reads the list of current trailers and creates a 
videometadata record in the DB with related cast & genre records, & 
downloadsthe trailers in a subfolder of the video folder and the poster 
in the standard posters directory.

The trailers can then be viewed with Mythvideo

At the moment the trailer & poster folders are hard coded so need to be 
modified as required ($videoStore & $posterStore) and I don't seem to 
get all the posters. I've set it up to run each night from mythtv's cron 
and it uses the Apple id to check if the trailer's already been loaded - 
I assume the id is unique.

It uses LWP::Simple, XML::Simple, File::Basename, DBI & DBIx:Perlish, 
which is simplifies DB access without the usual DBI overhead.
- get the video & poster directories from the DB
- automatically remove old trailers
- add 'new' to the new trailers (and remove it from existing 
trailers)(or when they have been viewed)
- use a standard DB interface (perl bindings? where are they documented?)
- make a dedicated plugin similar to the existing plugin but showing the 
additional details - is it possible to write a plugin/addon in perl? I'm 
too old to start learning C++

Perhaps someone might find it useful, or have some ideas for improvement....


Here it is

use strict;
use LWP::Simple;
use XML::Simple;
use File::Basename;

use DBI;
use DBIx::Perlish;

my $videoStore = "/storage500/videos/Trailers/";
my $posterStore = "/storage500/posters/";
my $logfile = "apple$$.log";
open LOG, ">$logfile";

my $dbh = DBI->connect('dbi:mysql:mythconverg', "mythtv", "mythtv");

my $list = get 'http://www.apple.com/trailers/home/xml/current.xml';
#my $ref = XMLin($list, ForceArray => [ "name" ]);
my $ref = XMLin($list, ForceArray => [ "name" ]);

my $message;

my %Cast;
my %Movies;
foreach my $movie (keys %{$ref->{movieinfo}}){
   next if(db_fetch {videometadata->inetref eq $movie}); # already go 
this one
   print LOG "Checking  $ref->{movieinfo}{$movie}{info}{title}\n";
   # fetch the trailer & images
   print LOG "Adding...\n";
   my $trailer = $ref->{movieinfo}{$movie}{preview}{large}{content};
   my $trailersize = $ref->{movieinfo}{$movie}{preview}{large}{filesize};
   my $localTrailer = $videoStore . File::Basename::basename($trailer);
   my $rv = getstore($trailer, $localTrailer);
   my $poster = $ref->{movieinfo}{$movie}{poster}{location};
   my $localPoster = $posterStore . File::Basename::basename($poster);
   $rv = getstore($poster, $localPoster);

   # main data to videometadata
   # build the 'plot'
   my $plot = $ref->{movieinfo}{$movie}{info}{description};
   $plot .= "\nRelease Date: " . 
   $plot .= "\nStudio: " . $ref->{movieinfo}{$movie}{info}{studio};
   $plot .= "\n" . $ref->{movieinfo}{$movie}{info}{copyright};
   $ref->{movieinfo}{$movie}{info}{runtime} =~ /^(\d+):(\d+)/;
   my $length = ($1 * 60) + $2;
   my $year = substr($ref->{movieinfo}{$movie}{info}{releasedate}, 0, 4);

   # add the record

   db_insert 'videometadata', {
      title       => $ref->{movieinfo}{$movie}{info}{title},
      director    => $ref->{movieinfo}{$movie}{info}{director},
      plot        => $plot,
      rating      => $ref->{movieinfo}{$movie}{info}{rating},
      inetref     => $movie,
      year        => $year,
      showlevel   => 1,
      length      => $length,
      filename    => $localTrailer,
      coverfile   => $localPoster,
   my $videoid = $dbh->{'mysql_insertid'};

   foreach my $actor (@{$ref->{movieinfo}{$movie}{cast}{name}}){
      _addCast($actor, $videoid);
   foreach my $genre (@{$ref->{movieinfo}{$movie}{genre}{name}}){
      _addGenre($genre, $videoid);

# send me a mail

#my $cmd = "/bin/mailx pinthenet\@payne.ch -s 'Appletrailers' $message";

sub _addCast{
   my($name, $idx) = @_;
   my $castid;

   # if new cast name add record else use existing record
   if(!($castid =  db_fetch {return videocast->intid; videocast->cast eq 
      db_insert 'videocast', {cast => $name,};
      $castid = $dbh->{'mysql_insertid'};

   # now add n:n record

   db_insert 'videometadatacast', {idvideo => $idx, idcast => $castid};


sub _addGenre{
   my($name, $idx) = @_;
   my $genreid;

   # if new cast name add record else use existing record
   if(!($genreid =  db_fetch {return videogenre->intid; 
videogenre->genre eq $name})){
      db_insert 'videogenre', {genre => $name,};
      $genreid = $dbh->{'mysql_insertid'};

   # now add n:n record

   db_insert 'videometadatagenre', {idvideo => $idx, idgenre => $genreid};


More information about the mythtv-users mailing list