↓ Twitter is updated more often, so read it! ↓

QTL reader in Python

I had a really nasty headache all day today—that kind of headache which affects speech, willpower, and mental processes. I also got really annoyed with Yahoo! Movies because its HD trailers are delivered in some kind of Quicktime playlist file instead of just serving up the movie. In the past, I’ve opened the .qtl file, copied and pasted the obvious URL, and fed it to wget.

Well, I got tired of this. So, I decided to write a script to handle this for me. I first tried to do it using Python’s XML processing features, but the qtl file isn’t a valid XML document. I then tried doing it with RegExps, before realizing that I basically suck with Regexps because I never use them.

Frustrated, I went a watched Casablanca, Aeon Flux, and The Girl Next Door, and had one of those brute force moments.

So, here’s the script. If someone can add a GUI to it or do something cool so it doesn’t have to be run at a command line, that’d be great.

#!/usr/bin/python
import sys,re,os
#lifted from somewhere
def htc(m):
    return chr(int(m.group(1),16))
def urldecode(url):
    rex=re.compile('%([0-9a-hA-H][0-9a-hA-H])',re.M)
    return rex.sub(htc,url)
#get our file
contents = open(sys.argv[1],'r').read()
#here's our markers
startmarker = "src=\""
endmarker = "\" autoplay"
#where are the markers?
srcpos = contents.find(startmarker)
endpos = contents.find(endmarker)
#calculate the real start position
startpos = srcpos + len(startmarker)
#SLICE AND DICE
src = contents[startpos:endpos]
#now we have the URL, but we need a pretty file name
#worked before, do it again
startmarker = "&so="
endmarker = "&tcode"
sopos = src.find(startmarker)
endpos = src.find(endmarker)
startpos = sopos + len(startmarker)
so = src[startpos:endpos]
filepath = urldecode(urldecode(so)) #Yahoo! is silly
filesplit = filepath.split('/')
filename = filesplit[len(filesplit)-1]
print "Getting the file from %s " % src
print "Filename is %s" % filename
thecommand = "/usr/bin/wget -O \"%s.mov\" \"%s\"" % (filename, src)
os.system(thecommand)

It’s probably needlessly long and complex, so shortenings are welcome in the comments.

Leave a comment