Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add basic search support #13

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
05ef3b4
commands un/watched added
mekanics Aug 29, 2013
6d097ef
make the command url constant
mekanics Aug 30, 2013
2aeed22
add basic search support
Jul 6, 2014
dabfbdb
fix merge conflict
Nov 22, 2014
a0e874e
rework to not use class_eval
Nov 22, 2014
739cab2
Bumped version
Dec 5, 2014
fd72252
Changed travis ruby versions
Dec 5, 2014
771bcaf
Add support for viewing the current sessions.
bok- Feb 28, 2015
b4eecf7
Add missing require
bok- Feb 28, 2015
2698901
Sessions are videos, not sections.
bok- Feb 28, 2015
0c24410
Videos only take the node as an initialiser (I should read before pus…
bok- Feb 28, 2015
d9325ab
Add support for players.
bok- Feb 28, 2015
8b1731b
Missed a thing
bok- Feb 28, 2015
0f2bc09
Correct URI escape
jodyalbritton Jul 23, 2015
c0992c1
Merge pull request #17 from jodyalbritton/patch-2
pboehm Jul 23, 2015
981dd17
Bumped version to 1.5.3
pboehm Jul 23, 2015
d9ecc4d
Add support for reading Music sections
rspeicher Jul 22, 2014
94b08e8
Add Medias to Track
rspeicher Aug 15, 2015
d235072
feat: add support for refreshing section
Nov 8, 2016
6d41407
use https, ignore cert
decrypted Dec 9, 2016
d107262
Merge pull request #1 from joshcano/master
decrypted Dec 10, 2016
8083577
Merge pull request #2 from tsigo/rs-music-support
decrypted Dec 10, 2016
9f96647
Merge pull request #3 from bok-/master
decrypted Dec 10, 2016
8b5ed5b
Merge pull request #4 from mekanics/watched_unwatched
decrypted Dec 10, 2016
e69f1e6
Merge pull request #5 from ygelfand/master
decrypted Dec 10, 2016
dd496c1
update test urls to https
decrypted Dec 10, 2016
b2067e9
call URI.open
ygelfand Jul 11, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
rvm:
- 1.9.2
- 1.9.3
- 2.0.0
- 2.1.1
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 1.5.3
* The player name is now escaped correctly in the URI (@jodyalbritton)

## 1.5.2
* Added support for authentication token via `Plex.configure`

## 1.5.1
* `Plex::Video` now supports multiple media entries (`@media` renamed to `@medias`)

Expand Down
9 changes: 7 additions & 2 deletions lib/plex-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ def self.configure
# Custom open func which adds the required headers configured by
# <tt>Plex.configure</tt>
def self.open(url)
headers = {}
headers = {:ssl_verify_mode => 0}
headers["X-Plex-Token"] = config.auth_token if config.auth_token

super(url, headers)
URI.open(url, headers)
end

# Converts camel case names that are commonly found in the Plex APIs into
Expand Down Expand Up @@ -62,10 +62,15 @@ def self.camelize(string, first_letter_uppercase = false)
require 'plex-ruby/video'
require 'plex-ruby/media'
require 'plex-ruby/part'
require 'plex-ruby/player'
require 'plex-ruby/stream'
require 'plex-ruby/status'
require 'plex-ruby/tags'
require 'plex-ruby/show'
require 'plex-ruby/season'
require 'plex-ruby/episode'
require 'plex-ruby/movie'
require 'plex-ruby/artist'
require 'plex-ruby/album'
require 'plex-ruby/track'

85 changes: 85 additions & 0 deletions lib/plex-ruby/album.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
module Plex
class Album

ATTRIBUTES = %w(ratingKey guid title summary index thumb year addedAt updatedAt)

attr_reader :section, :key, :attribute_hash

# @param [Artist] Artist this album belongs to
# @param [String] key to use to later grab this Album
def initialize(section, key)
@section = section
@key = key
@attribute_hash = {}

directory.attributes.each do |method, val|
@attribute_hash[Plex.underscore(method)] = val.value
define_singleton_method(Plex.underscore(method).to_sym) do
val.value
end
end

@attribute_hash.merge({'key' => @key})
end

# The list of Tracks in the library that belong to this Album
#
# @return [Array] list of Tracks that appear on this Album
def tracks
search_children children
end

# @private
def url #:nodoc:
section.url
end

# @private
def ==(other) #:nodoc:
if other.is_a? Plex::Album
key == other.key
else
super
end
end

# @private
def inspect #:nodoc:
"#<Plex::Album: key=\"#{key}\" title=\"#{title}\">"
end

private

def base_doc
Nokogiri::XML( open(url+key) )
end

def children_base
Nokogiri::XML( open(url+key+'/children') )
end

def xml_doc
@xml_doc ||= base_doc
end

def children
@children ||= children_base
end

def directory
@directory ||= xml_doc.search('Directory').first
end

def search_children(node)
node.search('Track').map do |track|
plex_track.new(self, track.attr('key'), track.search('Media'))
end
end

def plex_track
@plex_track ||= Plex::Track
end

end

end
85 changes: 85 additions & 0 deletions lib/plex-ruby/artist.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
module Plex
class Artist

ATTRIBUTES = %w(ratingKey guid title summary index thumb addedAt updatedAt)

attr_reader :section, :key, :attribute_hash

# @param [Section] section this artist belongs to
# @param [String] key to use to later grab this Artist
def initialize(section, key)
@section = section
@key = key
@attribute_hash = {}

directory.attributes.each do |method, val|
@attribute_hash[Plex.underscore(method)] = val.value
define_singleton_method(Plex.underscore(method).to_sym) do
val.value
end
end

@attribute_hash.merge({'key' => @key})
end

# The list of Albums in the library that belong to this Artist
#
# @return [Array] list of Albums that are credited to this Artist
def albums
@albums ||= search_children children
end

# @private
def url #:nodoc:
section.url
end

# @private
def ==(other) #:nodoc:
if other.is_a? Plex::Artist
key == other.key
else
super
end
end

# @private
def inspect #:nodoc:
"#<Plex::Artist: key=\"#{key}\" title=\"#{title}\">"
end

private

def base_doc
Nokogiri::XML( open(url+key) )
end

def children_base
Nokogiri::XML( open(url+key+'/children') )
end

def xml_doc
@xml_doc ||= base_doc
end

def children
@children ||= children_base
end

def directory
@directory ||= xml_doc.search('Directory').first
end

def search_children(node)
node.search('Directory[type="album"]').map do |album|
plex_album.new(self, album.attr('key')[0..-10]) # Remove /children
end
end

def plex_album
@plex_album ||= Plex::Album
end

end

end
2 changes: 1 addition & 1 deletion lib/plex-ruby/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def inspect #:nodoc:
private

def player_url
url+"/system/players/#{name}"
URI.escape(url+"/system/players/#{name}")
end

def ping(url)
Expand Down
17 changes: 17 additions & 0 deletions lib/plex-ruby/library.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ class Library

attr_reader :server

WATCHED_LINK = "/:/scrobble?identifier=com.plexapp.plugins.library&key="
UNWATCHED_LINK = "/:/unscrobble?identifier=com.plexapp.plugins.library&key="

# @param [Server] server this libary belongs to
def initialize(server)
@server = server
Expand Down Expand Up @@ -33,6 +36,20 @@ def sections!
@sections = search_sections(xml_doc!)
end

# Set the video as watched
#
# @param [Video] video to be set as watched
def watched(video)
open(url+WATCHED_LINK+video.rating_key)
end

# Set the video as unwatched
#
# @param [Video] video to be set as unwatched
def unwatched(video)
open(url+UNWATCHED_LINK+video.rating_key)
end

# @private
def key #:nodoc:
"/library/sections"
Expand Down
4 changes: 4 additions & 0 deletions lib/plex-ruby/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ def parse_directory
case node.attr('type')
when 'show'
Plex::Show.new( parent, node.attr('key')[0..-10] ) # Remove /children
when 'artist'
Plex::Artist.new( parent, node.attr('key')[0..-10] )
when 'album'
Plex::Album.new( parent, node.attr('key')[0..-10] )
else
raise "Unsupported Directory type #{node.attr('type')}"
end
Expand Down
27 changes: 27 additions & 0 deletions lib/plex-ruby/player.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Plex
class Player

ATTRIBUTES = %w(machineIdentifier platform product state title)

attr_reader :parts

# @param [Nokogiri::XML::Element] nokogiri element that represents this
# Media
def initialize(node)
node.attributes.each do |method, val|
define_singleton_method(Plex.underscore(method).to_sym) do
val.value
end
end
end

def ==(other)
if other.is_a? Media
id == other.id
else
super
end
end

end
end
19 changes: 10 additions & 9 deletions lib/plex-ruby/section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def initialize(library, node)
}
end

# NOT IMPLEMENTED
def refresh(deep = false, force = false)
def refresh
Plex.open(url + key + '/refresh')
end


Expand All @@ -35,13 +35,14 @@ def refresh(deep = false, force = false)
# on_deck - videos that are "on deck" in this Section
#
# @return [Array] list of Shows or Movies in that group
GROUPS.each { |method|
class_eval %(
def #{Plex.underscore(method)}
Plex::Parser.new( self, Nokogiri::XML(Plex.open(url+key+'/#{method}')) ).parse
end
)
}
GROUPS.each do |method|
define_method(Plex.underscore(method).to_sym) do |options = {}|
path = '/' + method + '?'
path += "title=#{CGI::escape(options[:title])}" if options[:title]
path += "type=4" if options[:episodes]
Plex::Parser.new( self, Nokogiri::XML(Plex.open(url+key+path )) ).parse
end
end

# Find TV Shows / Episodes by categories
#
Expand Down
9 changes: 8 additions & 1 deletion lib/plex-ruby/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ def initialize(host, port)
def library
@library ||= Plex::Library.new(self)
end

# The current status of this server
#
# @return [Status] this Servers status
def status
@status ||= Plex::Status.new(self)
end

# The Plex clients that are connected to this Server
#
Expand All @@ -29,7 +36,7 @@ def clients!

# @private
def url #:nodoc:
"http://#{host}:#{port}"
"https://#{host}:#{port}"
end

# @private
Expand Down
Loading