Download audio files support from the details dialogue for songs
This commit is contained in:
		@@ -1,9 +1,11 @@
 | 
				
			|||||||
# -*- coding: utf-8 -*-
 | 
					# -*- coding: utf-8 -*-
 | 
				
			||||||
 | 
					import utils
 | 
				
			||||||
import widgetUtils
 | 
					import widgetUtils
 | 
				
			||||||
import messages
 | 
					import messages
 | 
				
			||||||
import buffers
 | 
					import buffers
 | 
				
			||||||
from pubsub import pub
 | 
					from pubsub import pub
 | 
				
			||||||
from mysc.repeating_timer import RepeatingTimer
 | 
					from mysc.repeating_timer import RepeatingTimer
 | 
				
			||||||
 | 
					from mysc.thread_utils import call_threaded
 | 
				
			||||||
from sessionmanager import session
 | 
					from sessionmanager import session
 | 
				
			||||||
from wxUI import (mainWindow)
 | 
					from wxUI import (mainWindow)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -39,6 +41,7 @@ class Controller(object):
 | 
				
			|||||||
		self.buffers.append(audio)
 | 
							self.buffers.append(audio)
 | 
				
			||||||
		self.window.add_buffer(audio.tab, _(u"My audios"))
 | 
							self.window.add_buffer(audio.tab, _(u"My audios"))
 | 
				
			||||||
		pub.subscribe(self.in_post, "posted")
 | 
							pub.subscribe(self.in_post, "posted")
 | 
				
			||||||
 | 
							pub.subscribe(self.download, "download-file")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	def login(self):
 | 
						def login(self):
 | 
				
			||||||
		self.window.change_status(_(u"Logging in VK"))
 | 
							self.window.change_status(_(u"Logging in VK"))
 | 
				
			||||||
@@ -61,3 +64,6 @@ class Controller(object):
 | 
				
			|||||||
			if hasattr(i, "get_items"):
 | 
								if hasattr(i, "get_items"):
 | 
				
			||||||
				i.get_items()
 | 
									i.get_items()
 | 
				
			||||||
				print "executed for %s" % (i.name)
 | 
									print "executed for %s" % (i.name)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						def download(self, url, filename):
 | 
				
			||||||
 | 
							call_threaded(utils.download_file, url, filename, self.window)
 | 
				
			||||||
@@ -1,4 +1,5 @@
 | 
				
			|||||||
# -*- coding: utf-8 -*-
 | 
					# -*- coding: utf-8 -*-
 | 
				
			||||||
 | 
					import os
 | 
				
			||||||
import arrow
 | 
					import arrow
 | 
				
			||||||
import messages
 | 
					import messages
 | 
				
			||||||
import languageHandler
 | 
					import languageHandler
 | 
				
			||||||
@@ -193,6 +194,7 @@ class audio(postController):
 | 
				
			|||||||
		self.post = postObject
 | 
							self.post = postObject
 | 
				
			||||||
		self.dialog = postDialogs.audio()
 | 
							self.dialog = postDialogs.audio()
 | 
				
			||||||
		self.fill_information()
 | 
							self.fill_information()
 | 
				
			||||||
 | 
							widgetUtils.connect_event(self.dialog.download, widgetUtils.BUTTON_PRESSED, self.download)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	def fill_information(self):
 | 
						def fill_information(self):
 | 
				
			||||||
		if self.post.has_key("artist"):
 | 
							if self.post.has_key("artist"):
 | 
				
			||||||
@@ -208,3 +210,9 @@ class audio(postController):
 | 
				
			|||||||
		if self.post.has_key("lyrics_id"):
 | 
							if self.post.has_key("lyrics_id"):
 | 
				
			||||||
			l = self.session.vk.client.audio.getLyrics(lyrics_id=int(self.post["lyrics_id"]))
 | 
								l = self.session.vk.client.audio.getLyrics(lyrics_id=int(self.post["lyrics_id"]))
 | 
				
			||||||
			self.dialog.set("lyric", l["text"])
 | 
								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)
 | 
				
			||||||
							
								
								
									
										18
									
								
								src/utils.py
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								src/utils.py
									
									
									
									
									
								
							@@ -1,4 +1,5 @@
 | 
				
			|||||||
# -*- coding: utf-8 -*-
 | 
					# -*- coding: utf-8 -*-
 | 
				
			||||||
 | 
					import requests
 | 
				
			||||||
import re
 | 
					import re
 | 
				
			||||||
 | 
					
 | 
				
			||||||
url_re = re.compile("(?:\w+://|www\.)[^ ,.?!#%=+][^ ]*")
 | 
					url_re = re.compile("(?:\w+://|www\.)[^ ,.?!#%=+][^ ]*")
 | 
				
			||||||
@@ -32,3 +33,20 @@ def seconds_to_string(seconds, precision=0):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
def find_urls_in_text(text):
 | 
					def find_urls_in_text(text):
 | 
				
			||||||
	return [s.strip(bad_chars) for s in url_re.findall(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
 | 
				
			||||||
@@ -140,3 +140,8 @@ class audio(widgetUtils.BaseDialog):
 | 
				
			|||||||
		bbox.Add(self.download, 0, wx.ALL, 5)
 | 
							bbox.Add(self.download, 0, wx.ALL, 5)
 | 
				
			||||||
		bbox.Add(close, 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()
 | 
				
			||||||
		Reference in New Issue
	
	Block a user