Skip to content

Commit

Permalink
Fix for if only one library exists (either movie or tv, or vice versa)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dean Vizer committed Jan 21, 2013
1 parent bc67ed6 commit ae97b2f
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 45 deletions.
136 changes: 100 additions & 36 deletions lib/siriproxy-xbmc.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand All @@ -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
44 changes: 35 additions & 9 deletions lib/xbmc_library.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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={})
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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

0 comments on commit ae97b2f

Please sign in to comment.