Sign up here and you can log into the forum!

UMSP Plugin: IMDb Trailers

UMSP Plugin Depot, the place for sharing plugins & info about them. No requests, just plugins/code.

UMSP Plugin: IMDb Trailers   

Postby TroNik » Sun Dec 25, 2011 11:53 pm

Image
http://www.imdb.com/features/hdgallery


The plugin supports:
1) Most Viewed
2) Recent
3) Popular Movies
4) Search
5) Configuration to select default video resolution

Install the plugin using the Web Frontend (Addons Manager -> UMSP Plugins -> Trailers -> IMDb Trailers)

Code: Select all
<?php

function _pluginMain($prmQuery) {
  if (strpos($prmQuery, '&amp;') !== false) $prmQuery = str_replace('&amp;', '&', $prmQuery);
  parse_str($prmQuery, $params);
  if (isset($params['f']) && function_exists($params['f'])) {
    if (isset($params['args'])) {
      return call_user_func_array($params['f'], $params['args']);
    } else {
      return call_user_func($params['f']);
    }
  }
  return main_menu();
}

function _pluginSearch($prmQuery) {
  if (preg_match('/and dc:(?:title|album|genre) contains "(.*?)"/', $prmQuery, $query)) {
    $query = $query[1];
    return search($query);
  }
}

function build_query($f, $args = array()) {
  return http_build_query(array('f' => $f, 'args' => $args), '', '&amp;');
}

function build_umsp_url($f, $args = array()) {
  return 'umsp://plugins/imdb-trailers/imdb-trailers?' . build_query($f, $args);
}

function main_menu() {
  $Items[] = array (
    'id' => build_umsp_url('trailers', array('top_hd')),
    'dc:title' => 'Most Viewed',
    'upnp:album_art'=> 'http://lh6.googleusercontent.com/-FSUuQc9U7g0/TvGyaHqwl_I/AAAAAAAAAD8/2bRNHGaKh1I/s220/popcorn.png',
    'upnp:class' => 'object.container',
  );
  $Items[] = array (
    'id' => build_umsp_url('trailers', array('recent')),
    'upnp:album_art'=> 'http://lh5.googleusercontent.com/-Sm0NVoqNK4E/TvGyeR-aprI/AAAAAAAAAEc/bhg9IkceDX0/s220/clapperboard.png',
    'dc:title' => 'Recent',
    'upnp:class' => 'object.container',
  );
  $Items[] = array (
    'id' => build_umsp_url('trailers', array('popular')),
    'dc:title' => 'Popular Movies',
    'upnp:album_art'=> 'http://lh6.googleusercontent.com/-xYe4abRaD0M/TvGya-uROdI/AAAAAAAAAEE/ycci4mroXzc/s220/oscar.png',
    'upnp:class' => 'object.container',
  );
  $Items[] = array (
    'id' => build_umsp_url('search_button'),
    'dc:title' => 'Search IMDb trailers: press search on remote or use this option',
    'upnp:album_art'=> 'http://lh3.googleusercontent.com/-F9qS4ys3A6M/TvGzha58eYI/AAAAAAAAAEw/2b1JcrEneXA/s220/search_icon.png',
    'upnp:class' => 'object.container',
  );
  $Items[] = array (
    'id' => build_umsp_url('config'),
    'dc:title' => 'Configure Plugin',
    'upnp:album_art'=> 'http://lh5.googleusercontent.com/-xsH3IJAYXd0/TvwfRdc7DMI/AAAAAAAAAFk/NmvkjuqP_eo/s220/Settings.png',
    'upnp:class' => 'object.container',
  );
  return $Items;
}

function trailers($type, $token = 0) {
  $url = "http://www.imdb.com/video/trailers/data/_json?list=$type&token=$token";
  $data = json_decode(file_get_contents($url));
  $resolution = getConfigValue('DEFAULT_RESOLUTION', '');
  foreach ($data->{'videos'} as $video) {
    $video_id = $video->{'video'};
    $title = $video->{'title_title'};
    $thumb = get_thumb_url($video->{'poster'});
    if ($resolution) {
      $Items[] = array(
        'id' => build_umsp_url('play', array($video_id)),
        'res' => 'http://127.0.0.1/umsp/plugins/imdb-trailers/imdb-trailers.php?video=' . urlencode($video_id) . '&format=' . $resolution,
        'dc:title' => $title,
        'upnp:album_art' => 'http://127.0.0.1/umsp/plugins/imdb-trailers/img-proxy.php?url=' . urlencode($thumb),
        'upnp:class' => 'object.item.videoitem',
        'protocolInfo' => 'http-get:*:video/*:*',
      );
    } else {
      $Items[] = array(
        'id' => build_umsp_url('trailer', array($video_id, $title)),
        'dc:title' => $title,
        'upnp:album_art' => 'http://127.0.0.1/umsp/plugins/imdb-trailers/img-proxy.php?url=' . urlencode($thumb),
        'upnp:class' => 'object.container',
      );
    }
  }
  if ($data->{'next_token'}) {
    $Items[] = array(
      'id' => build_umsp_url('trailers', array($type, $data->{'next_token'})),
      'dc:title' => 'Next Page',
      'upnp:album_art' => 'http://lh5.googleusercontent.com/-NbZDnulY1HA/TvGycw-YIlI/AAAAAAAAAEM/mu3w8ZWlRkc/s200/next.png',
      'upnp:class' => 'object.container',
    );
  }
  return $Items;
}

function trailer($video, $title) {
  $Items = array();
  foreach (array('720p', '480p' ,'240p') as $format) {
    $url = "http://www.imdb.com/video/imdb/$video/html5?format=$format";
    $data = file_get_contents($url);
    if (preg_match('@\'(?P<stream>http://video-http.media-imdb.com/.+?)\'@', $data, $stream) &&
        preg_match('@\'(?P<thumb>http://ia.media-imdb.com/.+?)\'@', $data, $thumb)) {
      $Items[] = array(
        'id' => build_umsp_url('trailer', array($stream['stream'])),
        'res' => $stream['stream'],
        'dc:title' => $title . ' - ' . $format,
        'upnp:album_art' => 'http://127.0.0.1/umsp/plugins/imdb-trailers/img-proxy.php?url=' . urlencode(get_thumb_url($thumb['thumb'])),
        'upnp:class' => 'object.item.videoitem',
        'protocolInfo' => 'http-get:*:video/*:*',
      );
    }
  }
  return $Items;
}

function get_thumb_url($thumb_url) {
  return str_replace('._V1_.jpg', '._V1._SY220_.jpg', $thumb_url);
}

function search_button() {
  exec("sudo chmod 666 /tmp/ir_injection && sudo echo E > /tmp/ir_injection &");
  return main_menu();
}

function search($query, $popular_titles = true) {
  $Items = array();
  $movies = ImdbFind($query, $popular_titles);
  if ($popular_titles && empty($movies)) {
    return search($query, false);
  }
  foreach ($movies as $movie) {
    $Items[] = array(
      'id' => build_umsp_url('movie', array($movie['id'])),
      'dc:title' => $movie['title'] . ' (' . $movie['year'] . ')',
      'upnp:album_art' => 'http://127.0.0.1/umsp/plugins/imdb-trailers/img-proxy.php?url=' . urlencode(get_thumb_url($movie['image'])),
      'upnp:class' => 'object.container',
    );
  }
  if ($popular_titles) {
    $Items[] = array(
      'id' => build_umsp_url('search', array($query, false)),
      'dc:title' => 'More matches...',
      'upnp:album_art' => 'https://lh3.googleusercontent.com/-Lb2M2n5a1nc/Tvgl4iIoAzI/AAAAAAAAAFc/MD4BA9jxIlA/s200/imdb-logo.png',
      'upnp:class' => 'object.container',
    );
  }
  if (empty($Items)) {
    $Items[] = array(
      'id' => build_umsp_url('main_menu'),
      'dc:title' => 'No matches for ' . $query,
      'upnp:class' => 'object.container',
    );
  }
  return $Items;
}

function movie($imdbid) {
  $Items = array();
  $movie_info = ImdbGetInfo($imdbid);
  $trailer = $movie_info->{'trailer'};
  foreach ($trailer->{'encodings'} as $encoding) {
    $Items[] = array(
      'id' => build_umsp_url('imdb_trailer', array($imdbid)),
      'res' => $encoding->{'url'},
      'dc:title' => $trailer->{'relatedTitle'}->{'title'} . ' (' . $trailer->{'relatedTitle'}->{'year'} . ') - ' . $encoding->{'format'},
      'upnp:album_art' => 'http://127.0.0.1/umsp/plugins/imdb-trailers/img-proxy.php?url=' . urlencode($trailer->{'slates'}[0]->{'url'}),
      'upnp:class' => 'object.item.videoitem',
      'protocolInfo' => 'http-get:*:video/*:*',
    );
   
  }
  if (empty($Items)) {
    $Items[] = array(
      'id' => build_umsp_url('main_menu'),
      'dc:title' => 'No trailers',
      'upnp:class' => 'object.container',
    );
  }
  return $Items;
}

function ImdbFind($query, $popular_titles) {
  $query = str_replace(' ', '+', $query);
  $movies = array();
  $url = 'http://app.imdb.com/find?q=' . $query;
  $response = json_decode(file_get_contents($url));
  $data = $response->{'data'};
  if (isset($data->results)) {
    foreach ($data->results as $result) {
      if (($popular_titles && $result->{'label'} == 'Popular Titles') || (!$popular_titles && $result->{'label'} != 'Popular Titles')) {
        foreach ($result->{'list'} as $item) {
          if (isset($item->{'tconst'}) && isset($item->{'image'}->{'url'})) {
            $movies[] = array(
              'id' => $item->{'tconst'},
              'title' => $item->{'title'},
              'year' => $item->{'year'},
              'image' => $item->{'image'}->{'url'},
            );
          }
        }
      }
    }
  }
  return $movies;
}

function ImdbGetInfo($imdbid) {
  $url = "http://app.imdb.com/title/maindetails?tconst=$imdbid";
  $response = json_decode(file_get_contents($url));
  $data = $response->{'data'};
  return $data;
}

function config($key = null, $value = null) {
  if ($key != null) {
    putConfigValue($key, $value);
  }
  $resolution = getConfigValue('DEFAULT_RESOLUTION', '');
  $Items[] = array(
    'id' => build_umsp_url('config_resolution'),
    'dc:title' => "Select default resolution. Current value: $resolution",
    'upnp:class' => 'object.container',
  );
  $Items[] = array(
    'id' => build_umsp_url('main_menu'),
    'dc:title' => 'Back',
    'upnp:album_art'=> 'http://lh3.googleusercontent.com/-dsT4ZvjCth4/TvwihbvNZLI/AAAAAAAAAF0/1Jp9s8dLNlY/s220/back_button_icon.png',
    'upnp:class' => 'object.container',
  );
  return $Items;
}

function config_resolution() {
  $resolutions = array(
    '' => 'Prompt Me',
    '720p' => '720p',
    '480p' => '480p',
    '240p' => '240p',
  );
  foreach ($resolutions as $resolution => $title) {
    $Items[] = array(
      'id' => build_umsp_url('config', array('DEFAULT_RESOLUTION', $resolution)),
      'dc:title' => $title,
      'upnp:class' => 'object.container',
    );
  }
  return $Items;
}

function getConfigValue($key, $default_value) {
  $conf_dir = function_exists('_getUMSPConfPath') ? _getUMSPConfPath() : '/conf';
  $config = file_get_contents($conf_dir . '/config');
  if (preg_match("/IMDBTRAILERS_$key='(.+)'/", $config, $matches)) {
    return trim($matches[1]);
  }
  return $default_value;
}

function putConfigValue($key, $value) {
  exec("sudo config_tool -c IMDBTRAILERS_$key='$value' >/dev/null 2>&1");
}

if (isset($_GET['video'])) {
  $video = $_GET['video'];
  $format = $_GET['format'];
  $url = "http://www.imdb.com/video/imdb/$video/html5?format=$format";
  $data = file_get_contents($url);
  if (preg_match('@\'(?P<stream>http://video-http.media-imdb.com/.+?)\'@', $data, $stream)) {
    header('Location: ' . $stream['stream']);
    exit();
  }
}

?>


Code: Select all
<?php

# meta-name="IMDb Trailers"
# meta-desc="IMDb Trailers"
# meta-author="TroNik"
# meta-date="2012-01-15"
# meta-version="0.0.3"
# meta-type="umsp"
# meta-url="http://forum.wdlxtv.com/viewtopic.php?f=53&t=6191"
# meta-filename="imdb-trailers.php"
# meta-id="imdb-trailers"
# meta-basefirmware="*"
# meta-thumbnail="http://lh3.googleusercontent.com/-Lb2M2n5a1nc/Tvgl4iIoAzI/AAAAAAAAAFc/MD4BA9jxIlA/s60/imdb-logo.png"
# meta-rating="General"
# meta-category="Trailers"

global $pluginInfo;

$pluginInfo = array (
   'name'      => 'IMDb Trailers',
   'desc'      => 'IMDb Trailers',
   'author'   => 'TroNik',
   'date'      => '2012-01-15',
   'version'   => '0.0.3',
   'url'      => 'http://forum.wdlxtv.com/viewtopic.php?f=53&t=6191',
   'id'      => 'imdb-trailers',
   'thumb'      => 'http://lh3.googleusercontent.com/-Lb2M2n5a1nc/Tvgl4iIoAzI/AAAAAAAAAFc/MD4BA9jxIlA/s60/imdb-logo.png',
   'art'      => 'http://lh3.googleusercontent.com/-Lb2M2n5a1nc/Tvgl4iIoAzI/AAAAAAAAAFc/MD4BA9jxIlA/s200/imdb-logo.png',
);

?>
Last edited by TroNik on Sun Jan 15, 2012 2:13 am, edited 1 time in total.
TroNik
Developer
 
Posts: 46
Joined: Wed Dec 21, 2011 2:56 am

Re: UMSP Plugin: IMDb Trailers   

Postby namtih » Wed Dec 28, 2011 7:32 am

Great plugin, love it.
A "Default resolution" WEC-option like in the Apple-Trailers plugin would be nice. Perhaps a feature for a future version.
Moved on to the RaspberryPi + xbian (XBMC).
Thank you all for 2 great years and all your work to provide such fantastic firmware. Also a big thank you to the guys in the DD thread for all their ongoing support.
Good luck to all of you and perhaps we will see us in any Raspberry forum, cheers :-)
namtih
Donor
 
Posts: 238
Joined: Thu Dec 23, 2010 6:41 am
Location: Germany

Re: UMSP Plugin: IMDb Trailers   

Postby TroNik » Thu Dec 29, 2011 12:50 am

Yes it's in the TODO.
TroNik
Developer
 
Posts: 46
Joined: Wed Dec 21, 2011 2:56 am

Re: UMSP Plugin: IMDb Trailers   

Postby namtih » Thu Dec 29, 2011 10:05 am

TroNik wrote:Yes it's in the TODO.

Sounds great, thank you.
Can someone please upload it to the svn directory? Thanks.
Moved on to the RaspberryPi + xbian (XBMC).
Thank you all for 2 great years and all your work to provide such fantastic firmware. Also a big thank you to the guys in the DD thread for all their ongoing support.
Good luck to all of you and perhaps we will see us in any Raspberry forum, cheers :-)
namtih
Donor
 
Posts: 238
Joined: Thu Dec 23, 2010 6:41 am
Location: Germany

Re: UMSP Plugin: IMDb Trailers   

Postby mad_ady » Fri Dec 30, 2011 3:13 am

It's now part of the svn...
User avatar
mad_ady
Developer
 
Posts: 3028
Joined: Fri Nov 05, 2010 9:08 am
Location: Bucharest, Romania

Re: UMSP Plugin: IMDb Trailers   

Postby namtih » Sat Dec 31, 2011 9:45 am

You have to replace the https urls with http. Otherwise the album arts aren't shown on the WD.
Moved on to the RaspberryPi + xbian (XBMC).
Thank you all for 2 great years and all your work to provide such fantastic firmware. Also a big thank you to the guys in the DD thread for all their ongoing support.
Good luck to all of you and perhaps we will see us in any Raspberry forum, cheers :-)
namtih
Donor
 
Posts: 238
Joined: Thu Dec 23, 2010 6:41 am
Location: Germany

Re: UMSP Plugin: IMDb Trailers   

Postby TroNik » Sun Jan 15, 2012 2:15 am

namtih wrote:Great plugin, love it.
A "Default resolution" WEC-option like in the Apple-Trailers plugin would be nice. Perhaps a feature for a future version.

Done. Update the plugin from the WDLXTV Addons Manager to get the latest version.
namtih wrote:You have to replace the https urls with http. Otherwise the album arts aren't shown on the WD.

Fixed in the latest version.
TroNik
Developer
 
Posts: 46
Joined: Wed Dec 21, 2011 2:56 am

Re: UMSP Plugin: IMDb Trailers   

Postby TroNik » Sat Jun 30, 2012 12:57 am

Fixed plugin that broke a couple of days ago when IMDB changed their API.
TroNik
Developer
 
Posts: 46
Joined: Wed Dec 21, 2011 2:56 am

Re: UMSP Plugin: IMDb Trailers   

Postby TroNik » Tue Dec 25, 2012 7:32 pm

Fixed plugin again due to a recent IMDB change. Also added trailer's description (genre, plot, director, stars, user rating) for those using UMSPExtra.
TroNik
Developer
 
Posts: 46
Joined: Wed Dec 21, 2011 2:56 am


Return to UMSP Plugins

Who is online

Users browsing this forum: No registered users and 3 guests