From ae97b2f540a4346c26864db0177cf61268ebfe26 Mon Sep 17 00:00:00 2001 From: Dean Vizer Date: Mon, 21 Jan 2013 15:48:49 +0000 Subject: [PATCH] Fix for if only one library exists (either movie or tv, or vice versa) https://github.com/brainwave9/SiriProxy-XBMC/issues/9 --- lib/siriproxy-xbmc.rb | 136 +++++++++++++++++++++++++++++++----------- lib/xbmc_library.rb | 44 +++++++++++--- 2 files changed, 135 insertions(+), 45 deletions(-) mode change 100644 => 100755 lib/siriproxy-xbmc.rb mode change 100644 => 100755 lib/xbmc_library.rb diff --git a/lib/siriproxy-xbmc.rb b/lib/siriproxy-xbmc.rb old mode 100644 new mode 100755 index 80a3c1f..1ab23b6 --- a/lib/siriproxy-xbmc.rb +++ b/lib/siriproxy-xbmc.rb @@ -103,6 +103,51 @@ def initialize(config) request_completed #always complete your request! Otherwise the phone will "spin" at the user! end + # scan + listen_for /^scan/i do + if (@xbmc.connect(@active_room)) + if @xbmc.scan() + say "I'm scanning for new content" + else + say "There was a problem scanning for new content" + end + end + request_completed #always complete your request! Otherwise the phone will "spin" at the user! + end + + # recently added movies + listen_for /recent.*movies/i do + if (@xbmc.connect(@active_room)) + data = @xbmc.get_recently_added_movies() + list = "" + data["movies"].each { |movie| list = list + movie["label"] + "\n" } + say list, spoken: "Here are your recently added movies" + end + request_completed #always complete your request! Otherwise the phone will "spin" at the user! + end + + # recently added episodes + listen_for /recent.*tv/i do + if (@xbmc.connect(@active_room)) + data = @xbmc.get_recently_added_episodes() + + shows = {} + tvdata = @xbmc.get_tv_shows() + tvdata["tvshows"].each do |show| + shows[show["tvshowid"]] = show["label"] + end + + list = "" + data["episodes"].each do |episode| + episode_data = @xbmc.get_episode(episode["episodeid"]) + list = list + shows[episode_data["episodedetails"]["tvshowid"]] + ": " + episode["label"] + "\n" + end + say list, spoken: "Here are your recently added TV episodes" + end + request_completed #always complete your request! Otherwise the phone will "spin" at the user! + end + + # resume playing listen_for /^resume|unpause|continue/i do if (@xbmc.connect(@active_room)) @@ -115,7 +160,6 @@ def initialize(config) request_completed #always complete your request! Otherwise the phone will "spin" at the user! end - # set default room # set default room listen_for /(?:(?:[Ii]'m in)|(?:[Ii] am in)|(?:[Uu]se)|(?:[Cc]ontrol)) the (.*)/i do |roomname| roomname = roomname.downcase.strip @@ -128,43 +172,63 @@ def initialize(config) request_completed #always complete your request! Otherwise the phone will "spin" at the user! end - #play movie or episode - listen_for /play (.+?)(?: in the (.*))?$/i do |title,roomname| - if (roomname == "" || roomname == nil) - roomname = @active_room - else - roomname = roomname.downcase.strip + #play movie + listen_for /play movie (.+?)(?: in the (.*))?$/i do |title,roomname| + if (roomname == "" || roomname == nil) + roomname = @active_room + else + roomname = roomname.downcase.strip + end + + if (@xbmc.connect(roomname)) + if @roomlist.has_key?(roomname) + @active_room = roomname + end + + movie = @xbmc.find_movie(title) + if (movie == "") + say "Title not found, please try again" + else + say "Now playing \"#{movie["title"]}\"", spoken: "Now playing \"#{movie["title"]}\"" + @xbmc.play(movie["file"]) + end + else + say "The XBMC interface is unavailable, please check the plugin configuration or check if XBMC is running" + end + + request_completed #always complete your request! Otherwise the phone will "spin" at the user! end - - if (@xbmc.connect(roomname)) - if @roomlist.has_key?(roomname) - @active_room = roomname - end - - tvshow = @xbmc.find_show(title) - if (tvshow == "") - movie = @xbmc.find_movie(title) - if (movie == "") - say "Title not found, please try again" + + #play TV + listen_for /play TV show (.+?)(?: in the (.*))?$/i do |title,roomname| + if (roomname == "" || roomname == nil) + roomname = @active_room else - say "Now playing \"#{movie["title"]}\"", spoken: "Now playing \"#{movie["title"]}\"" - @xbmc.play(movie["file"]) + roomname = roomname.downcase.strip end - else - episode = @xbmc.find_first_unwatched_episode(tvshow["tvshowid"]) - if (episode == "") - say "No unwatched episode found for the \"#{tvshow["label"]}\"" - else - say "Now playing \"#{episode["title"]}\" (#{episode["showtitle"]}, Season #{episode["season"]}, Episode #{episode["episode"]})", spoken: "Now playing \"#{episode["title"]}\"" - @xbmc.play(episode["file"]) + + if (@xbmc.connect(roomname)) + if @roomlist.has_key?(roomname) + @active_room = roomname + end + + tvshow = @xbmc.find_show(title) + if (tvshow == "") + say "Title not found, please try again" + else + episode = @xbmc.find_first_unwatched_episode(tvshow["tvshowid"]) + if (episode == "") + say "No unwatched episode found for the \"#{tvshow["label"]}\"" + else + say "Now playing \"#{episode["title"]}\" (#{episode["showtitle"]}, Season #{episode["season"]}, Episode #{episode["episode"]})", spoken: "Now playing \"#{episode["title"]}\"" + @xbmc.play(episode["file"]) + end + end + else + say "The XBMC interface is unavailable, please check the plugin configuration or check if XBMC is running" + end + + request_completed #always complete your request! Otherwise the phone will "spin" at the user! end - end - else - say "The XBMC interface is unavailable, please check the plugin configuration or check if XBMC is running" - end - - request_completed #always complete your request! Otherwise the phone will "spin" at the user! - end - -end +end \ No newline at end of file diff --git a/lib/xbmc_library.rb b/lib/xbmc_library.rb old mode 100644 new mode 100755 index eb24d64..8615cc7 --- a/lib/xbmc_library.rb +++ b/lib/xbmc_library.rb @@ -24,7 +24,6 @@ def set_xbmc_config(location="default") return true end - # API interaction: Invokes the given method with given params, parses the JSON response body, maps it to # a HashWithIndifferentAccess and returns the :result subcollection def xbmc(method, params={}) @@ -34,8 +33,8 @@ def xbmc(method, params={}) # Raw API interaction: Invoke the given JSON RPC Api call and return the raw response (which is an instance of # HTTParty::Response) def invoke_json_method(method, params={}) - response = self.class.post('/jsonrpc', :body => {"jsonrpc" => "2.0", "params" => params, "id" => "1", "method" => method}.to_json,:headers => { 'Content-Type' => 'application/json' } ) -raise XBMCLibrary::UnauthorizedError, "Could not authorize with XBMC. Did you set up the correct user name and password ?" if response.response.class == Net::HTTPUnauthorized + response = self.class.post('/jsonrpc', :body => {"jsonrpc" => "2.0", "params" => params, "id" => "1", "method" => method}.to_json,:headers => { 'Content-Type' => 'application/json' } ) + raise XBMCLibrary::UnauthorizedError, "Could not authorize with XBMC. Did you set up the correct user name and password ?" if response.response.class == Net::HTTPUnauthorized response # Capture connection errors and send them out with a custom message @@ -52,16 +51,20 @@ def test() def connect(location) puts "[#{@appname}] Connecting to the XBMC interface (#{location})" $apiLoaded = false + begin if (set_xbmc_config(location)) $apiVersion = "" $apiVersion = xbmc('JSONRPC.version') - if ($apiVersion["version"] == 2) - puts "[#{@appname}] XBMC API Version #{$apiVersion["version"]} - Dharma" - else - puts "[#{@appname}] XBMC API Version #{$apiVersion["version"]} - Eden" - end + if ($apiVersion["version"] == {"major"=>6, "minor"=>0, "patch"=>0}) + puts "[#{@appname}] XBMC API Version 6 - Frodo" + elsif ($apiVersion["version"] == 4) + puts "[#{@appname}] XBMC API Version #{$apiVersion["version"]} - Eden" + elsif ($apiVersion["version"] == 2) + puts "[#{@appname}] XBMC API Version #{$apiVersion["version"]} - Dharma" + end + $apiLoaded = true end rescue @@ -76,7 +79,7 @@ def get_video_player() if ($apiVersion["version"] == 2) players = xbmc('Player.GetActivePlayers') result = players - else + elsif players = xbmc('Player.GetActivePlayers') players.each { |player| if (player["type"] == "video") @@ -188,6 +191,29 @@ def pause() end return false end + def scan() + if ($apiVersion["version"] == 2) + xbmc('VideoLibrary.ScanForContent') + else + xbmc('VideoLibrary.Scan') + end + return true + end + def get_recently_added_episodes() + return xbmc('VideoLibrary.GetRecentlyAddedEpisodes') + end + + def get_recently_added_movies() + return xbmc('VideoLibrary.GetRecentlyAddedMovies') + end + + def get_tv_shows() + return xbmc('VideoLibrary.GetTVShows') + end + + def get_episode(id) + return xbmc('VideoLibrary.GetEpisodeDetails', { :episodeid => id, :properties => ['tvshowid'] }) + end end