updated on Fri Jan 20 12:03:34 UTC 2012
[aur-mirror.git] / vkget / vkget.py
blob50df29455e9707f79a93b7f92db5afd7cf98f44f
1 #!/usr/bin/env python2
3 # VKGet - CLI mp3 audio downloader for vkontakte.ru
4 # Copyright (C) 2010-2011 Andrew Dunai
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 # You should have received a copy of the GNU General Public
11 # License along with this library; if not, write to the
12 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
13 # Boston, MA 02111-1307, USA.
16 import httplib, urllib
17 import sys, os, re
19 if len(sys.argv)!=2:
20 print "Usage: vkget.py <search query>"
21 exit(1)
23 def remove_html_tags(data):
24 p = re.compile(r'<.*?>')
25 return p.sub('', data).replace('&nbsp;', ' ');
27 def fetchCookies(response):
28 # result=[]
29 result=''
30 i=0
31 for header,content in response.getheaders():
32 if header.lower()=='set-cookie':
33 for cookie in content.split(', '):
34 i+=1
35 if i%2:
36 # result.append(cookie[0:cookie.index(';')])
37 result+=cookie[0:cookie.index(';')]+'; '
38 # result.append(content[0:content.index(" ")])
39 return result[0:-2]
41 #conn = httplib.HTTPConnection("m.vk.com")
42 #conn.request("POST", "/")
44 authParams = urllib.urlencode({
45 'act':'login',
46 'to':'',
47 'from_host':'m.vk.com',
48 'pda':1,
49 'email':'awaken.seven@gmail.com',
50 'pass':'andersonbot',
53 headers = {"Content-type": "application/x-www-form-urlencoded", "Accept":"text/plain"}
55 print " > Logging in..."
56 conn = httplib.HTTPConnection("login.vk.com:80")
57 #conn.request("POST", "/?act=login&to=&from_host=m.vk.com&pda=1", authParams, authHeaders);
58 conn.request("POST", "/", authParams, headers)
59 response = conn.getresponse()
60 html = response.read()
62 print " > Parsing post-authoriation cookies and proceeding login..."
63 headers["Cookie"] = fetchCookies(response);
65 redirect=response.getheaders()[6][1]
66 redirect=redirect[redirect.rfind('/'):1000]
68 conn = httplib.HTTPConnection("m.vk.com:80")
69 conn.request("GET", redirect, '', headers);
70 #print "GETTING" + redirect;
72 response = conn.getresponse()
73 #print response.getheaders()
74 html=response.read()
75 #print html
77 headers["Cookie"] += fetchCookies(response)
79 #print " > ..."
81 ###conn = httplib.HTTPConnection("m.vk.com:80")
82 ###conn.request("GET", "/", '', headers);
84 ###response = conn.getresponse()
85 #print response.getheaders()
86 ###html=response.read()
87 #print html
89 #authParams2 = urllib.urlencode({
90 # 's':s,
91 # 'op':'slogin',
92 # 'redirect':1,
93 # 'expire':0,
94 # 'to':''
95 # })
96 #conn.close()
98 #print " > Authorizing..."
99 #conn = httplib.HTTPConnection("m.vk.com:80")
100 #conn.request("POST", "/login", authParams2, headers);
101 #response = conn.getresponse()
102 #headers["Cookie"] += fetchCookies(response)
103 ##print headers["Cookie"];
105 #searchParams = urllib.urlencode({
106 # 'from':'audio',
107 # 'q':sys.argv[1],
108 # 'section':'audio',
109 # })
111 print " > Searching for a track..."
112 conn = httplib.HTTPConnection("vk.com")
113 conn.request("GET", "/gsearch.php?from=audio&q="+sys.argv[1].replace(' ','+')+"&section=audio", "", headers)
114 response = conn.getresponse()
115 html = response.read()
117 print " > Fetching URL..."
118 try:
119 start=html.index('operate(')+9;
120 except ValueError:
121 print " > Nothing found :("
122 exit(0)
123 end=html.index(');', start);
124 operate=html[start:end].split(',');
125 operate[3]=operate[3][1:-1];
127 start=html.index('audioTitle')+15
128 end=html.index('fl_l', start)-15;
129 track=remove_html_tags(html[start:end])
131 url="http://cs"+operate[1]+".vkontakte.ru/u"+operate[2]+"/audio/"+operate[3]+".mp3"
133 print " > Done!"
134 print " > Track name: "+track
135 print " > Track URL: "+url
137 os.system('wget -c '+url+' -O "'+track+'.mp3"')