Skip to content

Instantly share code, notes, and snippets.

@magical
Created September 26, 2011 08:28
Show Gist options
  • Save magical/1241854 to your computer and use it in GitHub Desktop.
Save magical/1241854 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from sys import argv, stdout
from urllib.request import urlopen as _urlopen, Request
from urllib.parse import urlunsplit
import json
import re
USER_AGENT = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.815.0 Safari/535.1"
def main():
clip_id = int(argv[1])
with urlopen("http://player.vimeo.com/video/%d" % clip_id) as f:
player_html = f.read()
m = re.search(br'clip\d+_\d+ = ([^;]*)', player_html)
if not m:
return
s = m.group(1)
s = fixstrings(s)
s = re.sub(br'(?<=[{,])(\w+)(?=:)', br'"\1"', s)
data = json.loads(s.decode('utf-8'))
#from pprint import pprint
#pprint(data)
request = data['config']['request']
video = data['config']['video']
# video.files :: {codec: [quality]}
codec = list(video['files'])[0]
quality = video['files'][codec][0]
url = urlunsplit((
'http',
request['player_url'],
'play_redirect',
build_qsl([
('quality', quality),
('codecs', codec),
('clip_id', video['id']),
('time', request['timestamp']),
('sig', request['signature']),
('type', "html5_desktop_embed"),
]),
'',
))
#print(url)
#print(urlopen(url).read(1024))
out = getattr(stdout, 'buffer', stdout)
with urlopen(url) as f:
def read():
return f.read(4096)
for d in iter(read, b''):
out.write(d)
def urlopen(url):
r = Request(url, headers={'User-agent': USER_AGENT})
return _urlopen(r)
def putchar(c):
print(chr(c), end='')
def fixstrings(s):
s = bytearray(s)
i = 0
DQ = ord(b"\"")
SQ = ord(b"\'")
BS = ord(b"\\")
while i < len(s):
if s[i] == DQ:
i += 1
while s[i] != DQ:
if s[i] == BS:
i += 1
i += 1
elif s[i] == SQ:
s[i] = DQ
i += 1
while s[i] != SQ:
#putchar(s[i])
if s[i] == BS:
i += 1
elif s[i] == DQ:
s[i:i+1] = bytes([BS, DQ])
i += 1
i += 1
#print()
s[i] = DQ
i += 1
return bytes(s)
def build_qsl(items):
return '&'.join(key + '=' + str(value) for (key, value) in items)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment