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
20 print "Usage: vkget.py <search query>"
23 def remove_html_tags(data
):
24 p
= re
.compile(r
'<.*?>')
25 return p
.sub('', data
).replace(' ', ' ');
27 def fetchCookies(response
):
31 for header
,content
in response
.getheaders():
32 if header
.lower()=='set-cookie':
33 for cookie
in content
.split(', '):
36 # result.append(cookie[0:cookie.index(';')])
37 result
+=cookie
[0:cookie
.index(';')]+'; '
38 # result.append(content[0:content.index(" ")])
41 #conn = httplib.HTTPConnection("m.vk.com")
42 #conn.request("POST", "/")
44 authParams
= urllib
.urlencode({
47 'from_host':'m.vk.com',
49 'email':'awaken.seven@gmail.com',
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()
77 headers
["Cookie"] += fetchCookies(response
)
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()
89 #authParams2 = urllib.urlencode({
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({
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(' ','+')+"§ion=audio", "", headers
)
114 response
= conn
.getresponse()
115 html
= response
.read()
117 print " > Fetching URL..."
119 start
=html
.index('operate(')+9;
121 print " > Nothing found :("
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"
134 print " > Track name: "+track
135 print " > Track URL: "+url
137 os
.system('wget -c '+url
+' -O "'+track
+'.mp3"')