diff --git a/src/controller/buffersController.py b/src/controller/buffersController.py index 9d5dfcc0..a2d9b9de 100644 --- a/src/controller/buffersController.py +++ b/src/controller/buffersController.py @@ -437,14 +437,23 @@ class baseBufferController(bufferController): def interact(self): "Select the best action for the currently focused tweet (audio, geocode, URL, etc)." tweet = self.get_tweet() + url=None urls = utils.find_urls(tweet) - #handle audio tweets. - if utils.is_audio(tweet): - return self.audio() + if len(urls) == 1: + url=urls[0] + elif len(urls) > 1: + urls_list = dialogs.urlList.urlList() + urls_list.populate_list(urls) + if urls_list.get_response() == widgetUtils.OK: + url=urls_list.get_string() + if hasattr(urls_list, "destroy"): urls_list.destroy() + if url != None: + if sound.URLPlayer.is_playable(url=url,play=True) == False: + return self.url(url) elif utils.is_geocoded(tweet): - output.speak("Not implemented",True) + return output.speak("Not implemented",True) else: - return self.url() + output.speak("Not actionable.",True) def url(self,url=''): if url == '': diff --git a/src/sound.py b/src/sound.py index 1fe229e5..c9398d7f 100644 --- a/src/sound.py +++ b/src/sound.py @@ -13,6 +13,7 @@ import output system = platform.system() from mysc.repeating_timer import RepeatingTimer import application +import time URLPlayer = None def setup(): @@ -114,7 +115,7 @@ class URLStream(object): log.debug("Transformed URL: %s. Prepared" % (self.url,)) self.prepared = True - def play(self, url, volume=1.0): + def play(self, url=None, volume=1.0, stream=None): if hasattr(self, "stream") and self.stream.is_playing: output.speak(_(u"Stopped")) self.stream.stop() @@ -123,21 +124,34 @@ class URLStream(object): else: output.speak(_(u"Playing...")) log.debug("Attempting to play an URL...") - self.prepare(url) + if url != None: + self.prepare(url) + elif stream != None: + self.stream=stream if self.prepared == True: self.stream = sound_lib.stream.URLStream(url=self.url) + if hasattr(self,'stream'): self.stream.volume = float(volume) self.stream.play() log.debug("played") - def is_playable(self, url): + def is_playable(self, url,play=False,volume=1.0): + print "Playability test invoked." + start=time.time() try: log.debug("Checking URL playability...") self.prepare(url) if self.prepared == True: - self.stream = sound_lib.stream.URLStream(url=self.url) + stream=sound_lib.stream.URLStream(url=self.url) + end=time.time() + print "is_playable algo took",end-start,"seconds." + if play: + return self.play(stream=stream,volume=volume) return True except: + log.exception("Exception.") + end=time.time() + print "is_playable algo took",end-start,"seconds." return False def stop_audio(self): if hasattr(self, "stream") and self.stream.is_playing == True: diff --git a/src/twitter/utils.py b/src/twitter/utils.py index 0237e2bb..009abec0 100644 --- a/src/twitter/utils.py +++ b/src/twitter/utils.py @@ -5,6 +5,8 @@ from twython import TwythonError import config import logging import requests +import time +import sound log = logging.getLogger("twitter.utils") """ Some utilities for the twitter interface.""" @@ -43,6 +45,7 @@ def find_next_reply(id, listItem): return None def is_audio(tweet,force=False): + start=time.time() if force == False and 'is_audio' in tweet: return tweet['is_audio'] try: @@ -61,10 +64,14 @@ def is_audio(tweet,force=False): if config.app["app-settings"]["use_modern_audio_algo"]: for u in find_urls(tweet): if url_is_audio(u): + end=time.time() + print "Codeofdusk algo took",end-start,"seconds." tweet['is_audio']=True return True except: log.exception("Exception while executing is_audio Codeofdusk algorithm.") + end=time.time() + print "Codeofdusk algo took",end-start,"seconds." tweet['is_audio']=False return False @@ -131,7 +138,11 @@ def is_allowed(tweet, clients): allowed = False log.exception("Tuit not allowed: %s" % (tweet["text"],)) return allowed + def url_is_audio(u): + sound.URLPlayer.is_playable(u) + +def old_url_is_audio(u): try: response = requests.head(u,allow_redirects=True) if 'audio' in str(response.headers['content-type']).lower():