lib/airplayer/media.rb in airplayer-1.0.1 vs lib/airplayer/media.rb in airplayer-1.1.0
- old
+ new
@@ -17,15 +17,10 @@
video/mp2t
video/quicktime
video/mpeg4
)
- SUPPORTED_DOMAINS = %w(
- youtube
- youtu.be
- )
-
class Media
attr_reader :title, :path, :type
def initialize(target)
path = File.expand_path(target)
@@ -33,53 +28,46 @@
if File.exist? path
@path = path
@title = File.basename(path)
@type = :file
else
- uri = URI.encode(target)
- @path = online_media_path(uri)
- @title = online_media_title(uri)
+ @path = YoutubeDl.get_url(target)
+ @title = YoutubeDl.get_title(target)
@type = :url
end
end
- def self.playable?(path)
- MIME::Types.of(path).map(&:simplified).each do |mimetype|
- return SUPPORTED_MIME_TYPES.include?(mimetype)
+ class << self
+ def playable?(path)
+ if is_url?(path)
+ YoutubeDl.supports?(path) || supported_mime_type?(YoutubeDl.filename(path))
+ else
+ supported_mime_type?(path)
+ end
end
- host = URI.parse(URI.escape(path)).host
- SUPPORTED_DOMAINS.each do |domain|
- return true if host =~ /#{domain}/
+ def is_url?(path)
+ uri = URI(path)
+ uri.scheme && uri.absolute?
+ rescue URI::InvalidURIError
+ false
end
- false
+ def supported_mime_type?(path)
+ MIME::Types.of(path).map(&:simplified).each do |mimetype|
+ return SUPPORTED_MIME_TYPES.include?(mimetype)
+ end
+
+ false
+ end
end
+
def file?
@type == :file
end
def url?
@type == :url
end
-
- private
- def online_media_path(uri)
- case URI.parse(uri).host
- when /youtube|youtu\.be/
- uri = `youtube-dl -g #{uri}`
- else
- uri
- end
- end
-
- def online_media_title(uri)
- case URI.parse(uri).host
- when /youtube|youtu\.be/
- title = `youtube-dl -e #{uri}`
- else
- title = File.basename(uri)
- end
- end
end
end