Skip to content

Commit

Permalink
Merge branch 'podcast' for Issue tomohiro#5
Browse files Browse the repository at this point in the history
  • Loading branch information
tomohiro committed Nov 21, 2012
2 parents 04347f4 + eccd69c commit 1b6868d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Gemfile.lock
Expand Up @@ -4,6 +4,7 @@ PATH
airplayer (0.0.5)
airplay
mime-types
nokogiri
rack
ruby-progressbar
thor
Expand All @@ -27,6 +28,7 @@ GEM
mime-types (1.19)
net-http-digest_auth (1.2.1)
net-http-persistent (2.8)
nokogiri (1.5.5)
rack (1.4.1)
rake (0.9.2.2)
rspec (2.11.0)
Expand Down
1 change: 1 addition & 0 deletions airplayer.gemspec
Expand Up @@ -20,6 +20,7 @@ Gem::Specification.new do |gem|
gem.add_runtime_dependency 'airplay'
gem.add_runtime_dependency 'rack'
gem.add_runtime_dependency 'mime-types'
gem.add_runtime_dependency 'nokogiri'

gem.add_development_dependency 'rake'
gem.add_development_dependency 'rspec'
Expand Down
2 changes: 1 addition & 1 deletion lib/airplayer/app.rb
Expand Up @@ -2,7 +2,7 @@

module AirPlayer
class App < Thor
desc 'play <URI|FILE|DIR> [-r|--repeat] [-s|--shuffle]', 'Play video(URI or local video file path or video directory)'
desc 'play <URI|Podcast XML|FILE|DIR> [-r|--repeat] [-s|--shuffle]', 'Play video(URI or Podcast XML URI or local video file path or video directory)'
method_option :repeat, :aliases => '-r', :desc => 'Repeat play', :type => :boolean
method_option :shuffle, :aliases => '-s', :desc => 'Shuffle play', :type => :boolean
def play(target)
Expand Down
33 changes: 29 additions & 4 deletions lib/airplayer/playlist.rb
@@ -1,3 +1,6 @@
require 'rss'
require 'nokogiri'

module AirPlayer
class Playlist < Array
def initialize(options = {})
Expand All @@ -6,8 +9,14 @@ def initialize(options = {})
end

def add(item)
path = File.expand_path(item)
Dir.exists?(path) ? concat(media_in(path)) : push(Media.new(item))
case type(item)
when :local
concat(media_in_local(item))
when :podcast
concat(media_in_podcast(item))
when :url
push(Media.new(item))
end
self
end

Expand All @@ -20,10 +29,26 @@ def entries(&blk)
end

private
def media_in(path)
Dir.entries(path).sort.map do |node|
def type(item)
if Dir.exists?(File.expand_path(item))
:local
elsif RSS::Parser.parse(open(item))
:podcast
else
:url
end
end

def media_in_local(path)
Dir.entries(File.expand_path(path)).sort.map do |node|
Media.new(File.expand_path(node, path)) if Media.playable? node
end.compact
end

def media_in_podcast(path)
Nokogiri::XML(open(path)).search('enclosure').map do |node|
Media.new(node.attributes['url'].text)
end
end
end
end
7 changes: 7 additions & 0 deletions spec/airplayer_spec.rb
Expand Up @@ -46,6 +46,13 @@
expect(playlist.first.path).to match 'http'
end

it 'add Podcast rss to list' do
playlist.add('http://rss.cnn.com/services/podcasting/cnnnewsroom/rss.xml')
playlist.entries do |media|
expect(media).to be_kind_of AirPlayer::Media
end
end

it 'add file to list, and that media type is file' do
expect(playlist.add('./LICENSE').size).to eq 1
expect(playlist.add('./Gemfile').size).to eq 2
Expand Down

0 comments on commit 1b6868d

Please sign in to comment.