Download audio files support from the details dialogue for songs

This commit is contained in:
Manuel Cortez 2016-02-15 16:49:09 -06:00
parent 8c6e89ba9c
commit e5a7f461c3
4 changed files with 40 additions and 3 deletions

View File

@ -1,9 +1,11 @@
# -*- coding: utf-8 -*-
import utils
import widgetUtils
import messages
import buffers
from pubsub import pub
from mysc.repeating_timer import RepeatingTimer
from mysc.thread_utils import call_threaded
from sessionmanager import session
from wxUI import (mainWindow)
@ -39,6 +41,7 @@ class Controller(object):
self.buffers.append(audio)
self.window.add_buffer(audio.tab, _(u"My audios"))
pub.subscribe(self.in_post, "posted")
pub.subscribe(self.download, "download-file")
def login(self):
self.window.change_status(_(u"Logging in VK"))
@ -61,3 +64,6 @@ class Controller(object):
if hasattr(i, "get_items"):
i.get_items()
print "executed for %s" % (i.name)
def download(self, url, filename):
call_threaded(utils.download_file, url, filename, self.window)

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import os
import arrow
import messages
import languageHandler
@ -193,6 +194,7 @@ class audio(postController):
self.post = postObject
self.dialog = postDialogs.audio()
self.fill_information()
widgetUtils.connect_event(self.dialog.download, widgetUtils.BUTTON_PRESSED, self.download)
def fill_information(self):
if self.post.has_key("artist"):
@ -208,3 +210,9 @@ class audio(postController):
if self.post.has_key("lyrics_id"):
l = self.session.vk.client.audio.getLyrics(lyrics_id=int(self.post["lyrics_id"]))
self.dialog.set("lyric", l["text"])
def download(self, *args, **kwargs):
f = u"{0} - {1}.mp3".format(self.post["title"], self.post["artist"])
path = self.dialog.get_destination_path(f)
if path != None:
pub.sendMessage("download-file", url=self.post["url"], filename=f)

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import requests
import re
url_re = re.compile("(?:\w+://|www\.)[^ ,.?!#%=+][^ ]*")
@ -32,3 +33,20 @@ def seconds_to_string(seconds, precision=0):
def find_urls_in_text(text):
return [s.strip(bad_chars) for s in url_re.findall(text)]
def download_file(url, local_filename, window):
r = requests.get(url, stream=True)
window.change_status(_(u"Downloading {0}").format(local_filename,))
total_length = r.headers.get("content-length")
dl = 0
total_length = int(total_length)
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=64):
if chunk: # filter out keep-alive new chunks
dl += len(chunk)
f.write(chunk)
done = int(100 * dl / total_length)
msg = _(u"Downloading {0} ({1}%)").format(local_filename, done)
window.change_status(msg)
window.change_status(_(u"Ready"))
return local_filename

View File

@ -140,3 +140,8 @@ class audio(widgetUtils.BaseDialog):
bbox.Add(self.download, 0, wx.ALL, 5)
bbox.Add(close, 0, wx.ALL, 5)
def get_destination_path(self, filename):
saveFileDialog = wx.FileDialog(self, _(u"Save this file"), "", filename, _(u"Audio Files(*.mp3)|*.mp3"), wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
if saveFileDialog.ShowModal() == wx.ID_OK:
return saveFileDialog.GetPath()
saveFileDialog.Destroy()