A year ago, I wanted a flash drive with a vinaigrette from the music that I had in my contact playlist. And there, by the way, almost 1,400 tracks.
So, the first thing I did was open the list of extensions of the opera and typed in the search for “vkontakte”. But here is the case, all the applications offered to download one track at a time, mostly adding a “download” button next to it. In principle, it is convenient if you need to pull out one or two tracks from there, but if there are 1,400 of them ...
My next step was to open a page with music and attentive meditation over it, in order to parse the page, having pulled out links and titles from there. A little thought, I decided, why not use the VK API again? After all, I already have the experience of using (wrote a desktop player on Qt). But to sculpt a full-fledged application, especially on the pros or some other language, it seemed to me a slightly non-smart decision. And then I thought, damn it, there is python, I rivet the script and pull everything. So, the choice is made, we start to code.
')
We open Geany and write the first lines ... And it was here that my imagination and the desire to sculpt combine harvesters from nothing again broke out. Further under the cut.
So, the editor is open, brains work, Judas Priest plays in headphones, and another piece comes out from under my pen.
Since I have no Windows on workstations, computers and laptops, as everywhere is Linux, it was decided to use a regular wget to download. Yes, and the console exhaust is beautiful.
To begin, I decided to make a small function that will check the connection to the Internet. To do this, try to open google.com.
def checkConnection(): #, . try: response = urllib2.urlopen('http://google.com', timeout = 1) return True except urllib2.URLError as err: pass return False
If Google is open - the function returns true, if not - false.
Then my inflexible imagination decided to put another check, this time on the presence in the wget system.
def checkWget(): # wget print " wget. - **..." testfilename = "x_8091546b.jpg" cmd = "wget http://cs5705.vkontakte.ru/u403273/139647416/x_8091546b.jpg" os.popen(cmd) chk = os.path.exists(testfilename) if chk == True: print ", wget , , . , !" os.remove(testfilename) else: print " , . wget, -!" exit() return
So, the check functions are ready, we start the download process itself. Fantasy decided to add a bit of humor to the script, so that everything was not as trite as always, and, in fact, this is the result.
Without further ado, I decided to use a regular system browser for authorization on the contact servers, and not to do it all with python tools, as it seemed to me that it would be faster and more convenient, especially since the script will not run every day.
print ", ! grabVK. ." print " , , , ." print " " print " , " print " " print " ..." inON = checkConnection() # . if inON == True: print " !" else: print " , . , =)" exit() print ", , " print ", , wget!" print " wget?" print " , - y, - n, =)" answer = raw_input(" wget?: ") if answer == "y": print " , . , !" else: checkWget() print " - , ? " print " - , - !" print " , ? !" print " , , , access_token, expires_in user_id, & " print " , " answer = raw_input("?: ") if answer == "y": print " , !" else: print " ! !" exit() webbrowser.open_new_tab("http://api.vkontakte.ru/oauth/authorize?client_id=2223684&scope=audio&redirect_uri=http://api.vk.com/blank.html&display=page&response_type=token") access_token = raw_input("access_token: ") print ", expires_in." expires_in = raw_input("expires_in: ") print " ! ." user_id = raw_input("user_id: ") print " ! . . !" print ", , !"
So, when executing this code, the system browser opens, which shows the authorization page on the vkontakte API server. In order for the authorization to work, you need to create a vkontakte application that will be assigned an ID, you need to specify it in the authorization link. I have created such an application for a long time, even for a dextup player. A line appears in the browser with three parameters given by the server that need to be transferred to the script using the copy-paste method; they are necessary to get the database in XML format.
Parameters are received, everything is ready to get a list and parse it. Lxml is used for parsing XML.
url = "https://api.vkontakte.ru/method/audio.get.xml?uid=" + user_id + "&access_token=" + access_token page = urllib2.urlopen(url) html = page.read() print " , ..." artistMas = [] titleMas = [] urlMas = [] number = 0 print " ..." doc = lxml.html.document_fromstring(html) for artist in doc.cssselect('artist'): artistMas.append(artist.text) number = number + 1 print "OK" print " ..." for title in doc.cssselect('title'): titleMas.append(title.text) print "OK" print " ..." for urlm in doc.cssselect('url'): urlMas.append(urlm.text) print "OK" print ""
We received lists of artists, tracks and links. It's time to start downloading. The download will be made to the download directory. Also, the download is implemented - if the music files are already in the download directory, they will not be downloaded again and will be skipped, which is convenient if you just need to sometimes synchronize the vkontakte playlist with the local one.
print " , ! , , !" print ".. - " print ", " path = "download" if os.path.exists(path): " , " else: os.makedirs(path) print " . ..." print number answer = raw_input("?: ") if answer == "y": print " , !" else: print " ! !" exit() for i in xrange(0, number-1): print ":" print i print " " filename_new = path+"/"+artistMas[i]+ " - " + titleMas[i] + ".mp3"; if os.path.exists(filename_new): print " , " else: downCmd = "wget -P" + path + " " + urlMas[i] os.popen(downCmd) p = re.compile(r"[0-9a-zA-Z]+\.mp3$") filename = p.findall(urlMas[i]) try: os.rename(path+"/"+filename[0], path+"/"+artistMas[i]+ " - " + titleMas[i] + ".mp3") except: print " , !" print " " print " ! !"
That's all. The script was tested several times, always successfully - the backup was made at 100%, with a resume and so on.
It is also worth noting that in linux it is not necessary to put it where the download directory will be created; it is enough to go there with the cd command and run the script along a direct path.
cd /data/vkmusic
python /home/zhbert/projects/pufrabvk.py
I don’t remember how it will be in Windows, because I haven’t seen it in my eyes for a long time.
The description and the file itself are
here .
And finally, a screenshot of the script:
