Last month I bought a box media player and I filled with I bunch of movies that were stuck in my laptop. I realized that if a invite my friends to watch a movie, it would be nice to have the trailer so I don’t have to tell them what’s the movie is about.
So I wrote this little script which given a movie name or a file with a list of names, it will search for it and download to the current directory.
All trailers will be downloaded from www.apple.com/trailers but the links are provided byÂ http://www.hd-trailers.net/
what you need:
import urllib import re import sys import os import string from urllib import FancyURLopener class myOpenUrl(FancyURLopener): version = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:184.108.40.206) Gecko/20071127 Firefox/220.127.116.11' #strap movie url from html def getMovieUrl(movieName, resolution): moviePageUrl = getMoviePage(movieName,resolution) f = urllib.urlopen(moviePageUrl) s = f.read() s = re.findall(r'http.+apple.+'+resolution+'.+mov', s) if s: return s else: print "Trailer not found" #strap movie page from google search def getMoviePage(movieName, resolution): try: myopener = myOpenUrl() page = myopener.open('http://www.google.com/search?q='+ string.join(movieName.split(), "+") + "+site:http://www.hd-trailers.net/") html = page.read() s = re.findall(r'href=['"]/url.q=([^'"& >]+)',html) return s except e: print "Search failed: %s" % e def main(): #if you want you can change to 720 or 1080, but is not garantee that it will find it resolution = "480" movieNameList =  if len(sys.argv) > 1: if (sys.argv == "-h"): print "usages: ./trailerDownloader.py" print "OR" print "usage: ./trailerDownloader.py listOfMovie.txt" sys.exit(0) source = open(sys.argv, 'r') movieNameList = source.readlines() source.close() else: movieName = raw_input("Name of the movie : ") movieNameList.append(movieName) for movieName in movieNameList: print "Searching for '"+movieName+"'..." movieUrl = getMovieUrl(movieName,resolution) if movieUrl: try: print "starting to download : "+ movieUrl cmd = 'wget -U QuickTime/7.6.2 ' + movieUrl os.system(cmd) except e: print "Error when trying to download : " + movieName else: print "movie not found" if __name__ == '__main__': main()
to run the script first you have to give permission
$ chmod +x ./trailerDownloader.py
to run you have 2 options:
1) if you want to download only one trailer, the script will ask you for the name.
2)if you want to download more than you.
$ ./trailerDownloader.py listOfMovies.txt
Feel free to improve the script, and please let me know so I can update here.
And of course, use it at your own risk
Leave your comments