Added youtube support. Closes #94

This commit is contained in:
Manuel Cortez 2018-02-20 11:47:33 -06:00
parent 5ff5a4a6d2
commit 0e41fabbe6
6 changed files with 38 additions and 80 deletions

View File

@ -69,6 +69,8 @@ setuptools installs a script, called easy_install. You can find it in the python
* PySocks * PySocks
* win_inet_pton * win_inet_pton
* yandex.translate * yandex.translate
* youtube-dl
* python-vlc
easy_install will automatically get the additional libraries that these packages need to work properly. easy_install will automatically get the additional libraries that these packages need to work properly.
Run the following command to quickly install and upgrade all packages and their dependencies: Run the following command to quickly install and upgrade all packages and their dependencies:

View File

@ -2,6 +2,8 @@
## changes in this version ## changes in this version
* Added support for playing youtube Links directly from the client. ([#94](https://github.com/manuelcortez/TWBlue/issues/94))
* Replaced Bass with libVLC for playing URL streams.
* the checkbox for indicating wether TWBlue will include everyone in a reply or not, will be unchecked by default. * the checkbox for indicating wether TWBlue will include everyone in a reply or not, will be unchecked by default.
* You can request TWBlue to save the state for two checkboxes: Long tweet and mention all, from the global settings dialogue. * You can request TWBlue to save the state for two checkboxes: Long tweet and mention all, from the global settings dialogue.
* For windows 10 users, some keystrokes in the invisible user interface have been changed or merged: * For windows 10 users, some keystrokes in the invisible user interface have been changed or merged:

View File

@ -2,6 +2,7 @@ from audio_services import matches_url
import json import json
import re import re
import urllib import urllib
import youtube_utils
@matches_url('https://audioboom.com') @matches_url('https://audioboom.com')
def convert_audioboom(url): def convert_audioboom(url):
@ -29,8 +30,8 @@ def convert_soundcloud (url):
raise TypeError('%r is not streamable' % url) raise TypeError('%r is not streamable' % url)
@matches_url ('https://www.youtube.com/watch') @matches_url ('https://www.youtube.com/watch')
def convert_long_youtube(url): def convert_youtube_long (url):
return "youtube-url" return youtube_utils.get_video_url(url)
def convert_generic_audio(url): def convert_generic_audio(url):
return url return url

View File

@ -71,8 +71,7 @@ class bufferController(object):
self.session.settings["sound"]["volume"] = 0.0 self.session.settings["sound"]["volume"] = 0.0
else: else:
self.session.settings["sound"]["volume"] -=0.05 self.session.settings["sound"]["volume"] -=0.05
if hasattr(sound.URLPlayer, "stream"): sound.URLPlayer.player.audio_set_volume(int(self.session.settings["sound"]["volume"]*100.0))
sound.URLPlayer.stream.volume = self.session.settings["sound"]["volume"]
self.session.sound.play("volume_changed.ogg") self.session.sound.play("volume_changed.ogg")
self.session.settings.write() self.session.settings.write()
@ -82,8 +81,7 @@ class bufferController(object):
self.session.settings["sound"]["volume"] = 1.0 self.session.settings["sound"]["volume"] = 1.0
else: else:
self.session.settings["sound"]["volume"] +=0.05 self.session.settings["sound"]["volume"] +=0.05
if hasattr(sound.URLPlayer, "stream"): sound.URLPlayer.player.audio_set_volume(int(self.session.settings["sound"]["volume"]*100))
sound.URLPlayer.stream.volume = self.session.settings["sound"]["volume"]
self.session.sound.play("volume_changed.ogg") self.session.sound.play("volume_changed.ogg")
self.session.settings.write() self.session.settings.write()
@ -620,11 +618,8 @@ class baseBufferController(bufferController):
if self.session.settings['sound']['indicate_img'] and utils.is_media(tweet): if self.session.settings['sound']['indicate_img'] and utils.is_media(tweet):
self.session.sound.play("image.ogg") self.session.sound.play("image.ogg")
# @_tweets_exist
def audio(self, url='', *args, **kwargs): def audio(self, url='', *args, **kwargs):
if hasattr(sound.URLPlayer,'stream') and sound.URLPlayer.stream.is_playing == True: if sound.URLPlayer.player.is_playing():
return sound.URLPlayer.stop_audio(delete=True)
elif sound.URLPlayer.mode == "youtube":
return sound.URLPlayer.stop_audio() return sound.URLPlayer.stop_audio()
tweet = self.get_tweet() tweet = self.get_tweet()
if tweet == None: return if tweet == None: return

View File

@ -504,13 +504,13 @@ class Controller(object):
def seekLeft(self, *args, **kwargs): def seekLeft(self, *args, **kwargs):
try: try:
sound.URLPlayer.seek(-5) sound.URLPlayer.seek(-5000)
except: except:
output.speak("Unable to seek.",True) output.speak("Unable to seek.",True)
def seekRight(self, *args, **kwargs): def seekRight(self, *args, **kwargs):
try: try:
sound.URLPlayer.seek(5) sound.URLPlayer.seek(5000)
except: except:
output.speak("Unable to seek.",True) output.speak("Unable to seek.",True)

View File

@ -11,6 +11,7 @@ import subprocess
import platform import platform
import output import output
import youtube_utils import youtube_utils
import vlc
system = platform.system() system = platform.system()
from mysc.repeating_timer import RepeatingTimer from mysc.repeating_timer import RepeatingTimer
from mysc.thread_utils import call_threaded from mysc.thread_utils import call_threaded
@ -96,13 +97,18 @@ class soundSystem(object):
sound_object.play() sound_object.play()
class URLStream(object): class URLStream(object):
def __init__(self, url=None): """ URL Stream Player implementation."""
self.url = url
def __init__(self):
# URL status. Should be True after URL expansion and transformation.
self.prepared = False self.prepared = False
log.debug("URL Player initialized") log.debug("URL Player initialized")
self.mode = "generic" # LibVLC controls.
self.instance = vlc.Instance()
self.player = self.instance.media_player_new()
def prepare(self, url): def prepare(self, url):
""" Takes an URL and prepares it to be streamed. This function will try to unshorten the passed URL and, if needed, to transform it into a valid URL."""
log.debug("Preparing URL: %s" % (url,)) log.debug("Preparing URL: %s" % (url,))
self.prepared = False self.prepared = False
self.url = url_shortener.unshorten(url) self.url = url_shortener.unshorten(url)
@ -112,83 +118,35 @@ class URLStream(object):
if self.url != None: if self.url != None:
transformer = audio_services.find_url_transformer(self.url) transformer = audio_services.find_url_transformer(self.url)
transformed_url = transformer(self.url) transformed_url = transformer(self.url)
if transformed_url == "youtube-url":
self.url = youtube_utils.get_video_url(url)
self.mode = "youtube"
else:
self.url = transformed_url self.url = transformed_url
self.mode = "generic"
log.debug("Transformed URL: %s. Prepared" % (self.url,)) log.debug("Transformed URL: %s. Prepared" % (self.url,))
self.prepared = True self.prepared = True
def seek(self,step): def seek(self, step):
if self.mode == "youtube": pos=self.player.get_time()
return
pos=self.stream.get_position()
pos=self.stream.bytes_to_seconds(pos)
pos+=step pos+=step
pos=self.stream.seconds_to_bytes(pos) pos=self.player.set_time(pos)
if pos<0:
pos=0
self.stream.set_position(pos)
def playpause(self): def playpause(self):
if self.mode == "youtube": if self.player.is_playing() == True:
if youtube_utils.player != None: self.player.pause()
youtube_utils.player.kill()
self.mode = "generic"
youtube_utils.player = None
if self.stream.is_playing==True:
self.stream.pause()
else: else:
self.stream.play() self.player.play()
def play(self, url=None, volume=1.0, stream=None,announce=True): def play(self, url=None, volume=1.0, announce=True):
if announce: if announce:
output.speak(_(u"Playing...")) output.speak(_(u"Playing..."))
log.debug("Attempting to play an URL...") log.debug("Attempting to play an URL...")
if url != None: if url != None:
self.prepare(url) self.prepare(url)
elif stream != None:
self.stream=stream
if self.prepared == True: if self.prepared == True:
if self.mode == "youtube": media = self.instance.media_new(self.url)
youtube_utils.play_video(self.url) self.player.set_media(media)
else: self.player.audio_set_volume(int(volume*100))
self.stream = sound_lib.stream.URLStream(url=self.url) self.player.play()
if hasattr(self,'stream'):
self.stream.volume = float(volume)
self.stream.play()
log.debug("played") log.debug("played")
self.prepared=False self.prepared=False
def stop_audio(self,delete=False): def stop_audio(self):
if self.mode == "youtube":
youtube_utils.stop()
self.prepared = False
output.speak(_(u"Stopped."), True) output.speak(_(u"Stopped."), True)
return True self.player.stop()
else:
if hasattr(self, "stream"):
output.speak(_(u"Stopped."), True)
try:
self.stream.stop()
log.debug("Stopped audio stream.")
except:
log.exception("Exception while stopping stream.")
if delete:
del self.stream
log.debug("Deleted audio stream.")
self.prepared=False
return True
else:
return False
@staticmethod
def delete_old_tempfiles():
for f in glob(os.path.join(tempfile.gettempdir(), 'tmp*.wav')):
try:
os.remove(f)
except:
pass