Implemented OMXPlayer file search and unified all searches under one method

This commit is contained in:
Fabio Manganiello 2018-04-25 00:13:17 +02:00
parent 77da94769e
commit acd06c2193
2 changed files with 29 additions and 18 deletions

View file

@ -45,7 +45,7 @@ $(document).ready(function() {
} else { } else {
request = { request = {
type: 'request', type: 'request',
action: 'video.omxplayer.youtube_search', action: 'video.omxplayer.search',
args: { query: resource } args: { query: resource }
}; };

View file

@ -26,7 +26,7 @@ class VideoOmxplayerPlugin(Plugin):
} }
def __init__(self, args=[], media_dirs=[], *argv, **kwargs): def __init__(self, args=[], media_dirs=[], *argv, **kwargs):
super().__init__(argv, kwargs) super().__init__(*argv, **kwargs)
self.args = args self.args = args
self.media_dirs = list( self.media_dirs = list(
@ -60,7 +60,7 @@ class VideoOmxplayerPlugin(Plugin):
try: try:
self.player = OMXPlayer(resource, args=self.args) self.player = OMXPlayer(resource, args=self.args)
# self._init_player_handlers() self._init_player_handlers()
except DBusException as e: except DBusException as e:
logging.warning('DBus connection failed: you will probably not ' + logging.warning('DBus connection failed: you will probably not ' +
'be able to control the media') 'be able to control the media')
@ -199,26 +199,32 @@ class VideoOmxplayerPlugin(Plugin):
self.player.pauseEvent += self.on_pause() self.player.pauseEvent += self.on_pause()
self.player.stopEvent += self.on_stop() self.player.stopEvent += self.on_stop()
def youtube_search_and_play(self, query): def search(self, query, types=None, queue_results=False, autoplay=False):
self.videos_queue = self.youtube_search(query).output results = []
ret = None if types is None:
types = { 'youtube', 'file' }
while self.videos_queue: if 'file' in types:
video = self.videos_queue.pop(0) file_results = self.file_search(query).output
logging.info('Playing "{}" from [{}]'.format(video['url'], video['title'])) results.extend(file_results)
try: if 'youtube' in types:
ret = self.play(video['url']) yt_results = self.youtube_search(query).output
break results.extend(yt_results)
except Exception as e:
logging.exception(e)
logging.info('YouTube playback error, trying next video')
return ret if results:
if queue_results:
self.videos_queue = [_['url'] for _ in results]
if autoplay:
self.play(self.videos_queue.pop(0))
elif autoplay:
self.play(results[0]['url'])
return Response(output=results)
def file_search(self, query): def file_search(self, query):
results = [] results = []
query_tokens = [_.lower() for _ in re.split('\s+', query.trim())] query_tokens = [_.lower() for _ in re.split('\s+', query.strip())]
for media_dir in self.media_dirs: for media_dir in self.media_dirs:
logging.info('Scanning {} for "{}"'.format(media_dir, query)) logging.info('Scanning {} for "{}"'.format(media_dir, query))
@ -242,11 +248,16 @@ class VideoOmxplayerPlugin(Plugin):
if not matches_query: if not matches_query:
continue continue
results.append(path + os.sep + f) results.append({
'url': 'file://' + path + os.sep + f,
'title': f,
})
return Response(output=results) return Response(output=results)
def youtube_search(self, query): def youtube_search(self, query):
logging.info('Searching YouTube for "{}"'.format(query))
query = urllib.parse.quote(query) query = urllib.parse.quote(query)
url = "https://www.youtube.com/results?search_query=" + query url = "https://www.youtube.com/results?search_query=" + query
response = urllib.request.urlopen(url) response = urllib.request.urlopen(url)