upnp_content_explorer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9b4988b418c9159f786046868740f5bec4e30a3b
4
+ data.tar.gz: 3e0fa7672ef7e7aa4f0b0a1c52ab38ec0806a5ba
5
+ SHA512:
6
+ metadata.gz: 4335c1369615a03dea77582c64171a00a8c3b37f7660df50e9e57bc76e766db40361ac6c661637e4e278e75b433ba51913ec73a0710b31e730db427780761cf1
7
+ data.tar.gz: 4627fbb79b24530685dd49dc0b7d11f61dc8b51cf41045f28a75c26fd4a20f8311ff9a323229eb9f5572e2dd821c8b088deb7f0d271f773b4d5fe2be1dd4edde
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.iml
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Christopher
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,2 @@
1
+ require 'upnp_content_explorer/explorer'
2
+ require 'upnp_content_explorer/version'
@@ -0,0 +1,100 @@
1
+ require 'nokogiri'
2
+ require 'nori'
3
+
4
+ require 'upnp_content_explorer/node'
5
+ require 'upnp_content_explorer/item'
6
+
7
+ module UpnpContentExplorer
8
+ class PathNotFoundError < StandardError; end
9
+
10
+ class Explorer
11
+ def initialize(service)
12
+ @service = service
13
+ @root = Node.new({title: 'Root', id: 0})
14
+ end
15
+
16
+ def node_at(path)
17
+ find_terminal_node(prepare_path(path))
18
+ end
19
+
20
+ def children_of(path)
21
+ node_at(path).children
22
+ end
23
+
24
+ def items_of(path)
25
+ node_at(path).items
26
+ end
27
+
28
+ def scrape(path)
29
+ node = find_terminal_node(prepare_path(path))
30
+
31
+ child_items = node.children.map do |child|
32
+ scrape("#{path}/#{child.title}")
33
+ end
34
+
35
+ all_items = []
36
+ all_items += node.items
37
+ all_items += child_items.flatten
38
+ end
39
+
40
+ private
41
+ def prepare_path(path)
42
+ path = Pathname.new(path)
43
+ path.each_filename.to_a
44
+ end
45
+
46
+ def find_terminal_node(path, node = @root, traversed_path = '')
47
+ node.load!(get_node(node.id)) unless node.loaded?
48
+
49
+ return node if path.empty?
50
+
51
+ next_node = path.shift
52
+ next_traversed_path = "#{traversed_path}/#{next_node}"
53
+ child = node.child(next_node)
54
+
55
+ raise PathNotFoundError, "Path doesn't exist: #{next_traversed_path}" if child.nil?
56
+
57
+ find_terminal_node(path, child, next_traversed_path)
58
+ end
59
+
60
+ def get_node(node_id)
61
+ response = @service.Browse(
62
+ ObjectID: node_id,
63
+ BrowseFlag: 'BrowseDirectChildren',
64
+ Filter: '*',
65
+ StartingIndex: '0',
66
+ RequestedCount: '0'
67
+ )
68
+
69
+ # Some UPnP servers (i.e., MediaTomb) screw up the namespacing
70
+ node_data = response[:Result].gsub('xmlns=', 'xmlns:didl=')
71
+ content = Nokogiri::XML(node_data)
72
+
73
+ children = content.xpath('/DIDL-Lite/container').map do |child|
74
+ node_data = parse_nori_node(child)
75
+ Node.new(node_data)
76
+ end
77
+
78
+ items = content.xpath('/DIDL-Lite/item').map do |item|
79
+ item_data = parse_nori_node(item)
80
+ Item.new(item_data)
81
+ end
82
+
83
+ children = Hash[ children.map { |x| [x.title, x] } ]
84
+ items = Hash[ items.map { |x| [x.title, x] } ]
85
+
86
+ { children: children, items: items }
87
+ end
88
+
89
+ def parse_nori_node(node)
90
+ raw_map = Nori.new(:strip_namespaces => true).parse(node.to_xml)
91
+ raw_map = raw_map[raw_map.keys.first]
92
+
93
+ Hash[
94
+ raw_map.map do |k,v|
95
+ [k.gsub('@', '').to_sym, v]
96
+ end
97
+ ]
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,12 @@
1
+ module UpnpContentExplorer
2
+ class Item
3
+ def initialize(data)
4
+ @data = data
5
+ end
6
+
7
+ def method_missing(key)
8
+ return @data[key] if @data.has_key?(key)
9
+ super
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,40 @@
1
+ module UpnpContentExplorer
2
+ class Node
3
+ def initialize(data)
4
+ @data = data
5
+ end
6
+
7
+ def loaded?
8
+ return false if @data[:loaded?].nil?
9
+ @data[:loaded?]
10
+ end
11
+
12
+ def method_missing(key)
13
+ return @data[key] if @data.has_key?(key)
14
+ super
15
+ end
16
+
17
+ def load!(data)
18
+ merged_data = @data.merge(data)
19
+ merged_data[:loaded?] = true
20
+
21
+ @data = merged_data
22
+ end
23
+
24
+ def children
25
+ @data[:children].values || []
26
+ end
27
+
28
+ def items
29
+ @data[:items].values || []
30
+ end
31
+
32
+ def child(key)
33
+ @data[:children][key]
34
+ end
35
+
36
+ def item(key)
37
+ @data[:items][key]
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module UpnpContentExplorer
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require 'upnp_content_explorer'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "upnp_content_explorer"
9
+ spec.version = UpnpContentExplorer::VERSION
10
+ spec.authors = ["Christopher Mullins"]
11
+ spec.email = ["chris@sidoh.org"]
12
+
13
+ spec.summary = %q{Provides a convenient way to explore and access content provided by a UPnP media server.}
14
+ spec.homepage = "http://www.github.com/sidoh/upnp_content_explorer"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "nori", "~> 2.6"
23
+ spec.add_dependency "easy_upnp", "~> 0.1"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.10"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.3"
28
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: upnp_content_explorer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Christopher Mullins
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nori
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '2.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '2.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: easy_upnp
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '0.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '0.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '3.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '3.3'
83
+ description:
84
+ email:
85
+ - chris@sidoh.org
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - Rakefile
94
+ - lib/upnp_content_explorer.rb
95
+ - lib/upnp_content_explorer/explorer.rb
96
+ - lib/upnp_content_explorer/item.rb
97
+ - lib/upnp_content_explorer/node.rb
98
+ - lib/upnp_content_explorer/version.rb
99
+ - upnp_content_explorer.gemspec
100
+ homepage: http://www.github.com/sidoh/upnp_content_explorer
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.4.8
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: Provides a convenient way to explore and access content provided by a UPnP
124
+ media server.
125
+ test_files: []