diff --git a/src/controller/mainController.py b/src/controller/mainController.py index db7e4b0..6e2906a 100644 --- a/src/controller/mainController.py +++ b/src/controller/mainController.py @@ -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")) @@ -60,4 +63,7 @@ class Controller(object): for i in self.buffers: if hasattr(i, "get_items"): i.get_items() - print "executed for %s" % (i.name) \ No newline at end of file + print "executed for %s" % (i.name) + + def download(self, url, filename): + call_threaded(utils.download_file, url, filename, self.window) \ No newline at end of file diff --git a/src/controller/posts.py b/src/controller/posts.py index 37940de..37a2410 100644 --- a/src/controller/posts.py +++ b/src/controller/posts.py @@ -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"): @@ -207,4 +209,10 @@ class audio(postController): def get_lyrics(self): 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"]) \ No newline at end of file + 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) \ No newline at end of file diff --git a/src/utils.py b/src/utils.py index e35569a..2f46004 100644 --- a/src/utils.py +++ b/src/utils.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +import requests import re url_re = re.compile("(?:\w+://|www\.)[^ ,.?!#%=+][^ ]*") @@ -31,4 +32,21 @@ def seconds_to_string(seconds, precision=0): return string def find_urls_in_text(text): - return [s.strip(bad_chars) for s in url_re.findall(text)] \ No newline at end of file + 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 \ No newline at end of file diff --git a/src/wxUI/dialogs/postDialogs.py b/src/wxUI/dialogs/postDialogs.py index c721e00..707187e 100644 --- a/src/wxUI/dialogs/postDialogs.py +++ b/src/wxUI/dialogs/postDialogs.py @@ -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() \ No newline at end of file