#!/usr/bin/ruby # Apple Trailer downloader/viewer require 'net/http' require 'rexml/document' require 'thread' MplayerExec = "/usr/bin/mplayer -prefer-ipv4 -display :0.0 -fs -zoom"; TrailerPath = '/home/apoc/media/trailers/' # Quality: trl_replace = 'h720p' # others: h640w, h1080p # Original Quality/ Fallback trl_original = 'h640w' # default quality in feed trailer_feed = 'http://www.apple.com/trailers/home/xml/current.xml' $trailers = [] xml_data = Net::HTTP.get_response(URI.parse(trailer_feed)).body doc = REXML::Document.new(xml_data) doc.elements.each('records/movieinfo/preview/large') do |node| $trailers << node.text.gsub(/#{trl_original}/, trl_replace) end def download_trailer(idx) url = $trailers[idx] url_parts = URI.parse url url_parts.path.match /\/([^\/]+)$/ filename = $1 http = Net::HTTP.new(url_parts.host) response = http.request_head(url_parts.path) url_size = response['content-length'].to_i puts "download_trailer: filename: #{TrailerPath+filename} size: #{url_size}" if File.exists?(TrailerPath+filename) and File.size(TrailerPath+filename) == url_size then puts "File exists" return end puts "Download File.." `wget -c -O #{TrailerPath+filename} -q "#{url}"` end for i in 0...$trailers.length-1 url = $trailers[i] url_parts = URI.parse url url_parts.path.match /\/([^\/]+)$/ filename = $1 puts "Next Trailer: #{filename}" download_trailer(i) mplayer_thread = Thread.new(TrailerPath+filename) do |file| puts "Mplayer play: #{file}" `#{MplayerExec} #{file} 2&> /dev/null` end download_trailer(i+1) while(mplayer_thread.alive?) do end end