Created
September 17, 2012 20:34
-
-
Save baverman/3739622 to your computer and use it in GitHub Desktop.
Prints a downloadable url for VK video search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
import sys | |
import re | |
from urllib2 import build_opener, HTTPCookieProcessor | |
from urllib import urlencode | |
from cookielib import CookieJar | |
from netrc import netrc | |
from lxml import html | |
from ast import literal_eval | |
class ErrorMessage(Exception): pass | |
HD_RES = { | |
1: 360, | |
2: 480, | |
3: 720, | |
} | |
cj = CookieJar() | |
opener = build_opener(HTTPCookieProcessor(cj)) | |
def login(): | |
result = netrc().authenticators('vk.com') | |
if not result: | |
raise ErrorMessage('You have no entries in ~/.netrc file associated with vk.com') | |
email, _, pwd = result | |
resp = opener.open("http://vk.com") | |
root = html.parse(resp) | |
params = {node.attrib['name']:node.attrib.get('value', '') | |
for node in root.xpath('//form//input[@type!="submit"]')} | |
params['email'] = email | |
params['pass'] = pwd | |
opener.open(root.xpath('//form')[0].attrib['action'], urlencode(params)) | |
def search_video(query): | |
login() | |
params = { | |
'act': 'search_video', | |
'al': '1', | |
'hd': '1', | |
'offset': '0', | |
'order': '1', | |
'q': query, | |
'show_adult': '0', | |
} | |
resp = opener.open('http://vk.com/al_video.php', urlencode(params)) | |
data = resp.read() | |
data = literal_eval(data[data.find('[['):].decode('windows-1251').encode('utf-8')) | |
for i, row in enumerate(data, 1): | |
print '{0}. {1[3]} {1[9]}'.format(i, row) | |
idx = int(raw_input("URL to show: ")) | |
get_url('http://vk.com/video{}_{}?section=search'.format(*data[idx-1])) | |
def get_url(url): | |
_, _, vid = url.rpartition('/') | |
if not vid.startswith('video'): | |
print >> sys.stderr, 'Bad url:', url | |
return | |
params = { | |
'act': 'show', | |
'al': '1', | |
'list': '', | |
'module': 'video', | |
'video': vid[5:].partition('?')[0] | |
} | |
resp = opener.open('http://vk.com/al_video.php', urlencode(params)) | |
match = re.search('var\svars\s=\s(\{.+?\})', resp.read()) | |
data = literal_eval(match.group(1).decode('windows-1251').encode('utf-8').replace('\\"', '"')) | |
if 'vkadre.ru' in data['host'] and not data['no_flv']: | |
print "http://{host}/assets/videos/{vtag}{vkid}.vk.flv".format(**data) | |
else: | |
res = HD_RES[data.get('hd', 1)] | |
print "http://cs{host}.vk.com/u{uid}/videos/{vtag}.{res}.mp4".format(res=res, **data) | |
if __name__ == '__main__': | |
try: | |
login() | |
q = sys.argv[1] | |
if q.startswith('http://'): | |
get_url(q) | |
else: | |
search_video(sys.argv[1]) | |
except ErrorMessage as e: | |
print e | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment