Class: Podcastinator::FileFeed::FileItem

Inherits:
Item
  • Object
show all
Defined in:
lib/podcastinator/file_feed.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(feed, filename) ⇒ FileItem

Returns a new instance of FileItem.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/podcastinator/file_feed.rb', line 39

def initialize(feed, filename)
  @feed           = feed
  @filename       = filename
  @local_filename = File.join(feed.local_path, filename)
  @url            = "#{ feed.url }/#{ URI.escape @filename.to_s }".gsub("//", "/")

  @image_url =
    if File.file? File.join(feed.local_path, "#{ filename }.jpg")
      "#{ @url }.jpg"
    else
      feed.image_url
    end

  TagLib::FileRef.open(local_filename) do |fileref|
    tag      = fileref.tag

    @title    = tag.title
    @author   = tag.artist
    @album    = tag.album
    @track    = if tag.track.present? && tag.track != 0 then tag.to_s.track[/^\d+/] end
    @subtitle = if author.present? && @track.present? then %[#{ album } ##{ track }] end
    @duration = fileref.audio_properties.length
  end
end

Class Method Details

.build(feed, files) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/podcastinator/file_feed.rb', line 105

def self.build(feed, files)
  files.map do |filename|
    title, subtitle, author, time, image_url = nil

    TagLib::FileRef.open(local_filename) do |fileref|
      tag      = fileref.tag

      title    = tag.title
      author   = tag.artist
      album    = tag.album
      track    = if tag.track.present? && tag.track != 0 then tag.to_s.track[/^\d+/] end
      subtitle = if album.present? && track.present? then %[#{ album } ##{ track }] end
    end

    new(feed, filename, title, subtitle, author, time, image_url)
  end
end

.globsObject



101
102
103
# File 'lib/podcastinator/file_feed.rb', line 101

def self.globs
  mime_types.keys.map { |ext| "**/*.#{ ext }" }
end

.mime_typesObject



95
96
97
98
99
# File 'lib/podcastinator/file_feed.rb', line 95

def self.mime_types
  {
    "mp3" => "audio/mpeg",
  }
end

Instance Method Details

#guidObject



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/podcastinator/file_feed.rb', line 71

def guid
  md5 = Digest::MD5.new

  File.open(local_filename, 'rb') do |io|
    while (buf = io.read(4096)) && (buf.length > 0)
      md5.update(buf)
    end
  end

  md5.to_s
end

#is_guid_permalink?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/podcastinator/file_feed.rb', line 83

def is_guid_permalink?
  false
end

#keywordsObject



91
92
93
# File 'lib/podcastinator/file_feed.rb', line 91

def keywords
  []
end

#mime_typeObject



64
65
66
67
68
69
# File 'lib/podcastinator/file_feed.rb', line 64

def mime_type
  case File.extname(filename).downcase
  when ".mp3" then "audio/mpeg"
  else raise UnknownMimeType, "podcastinator can't handle #{ filename }"
  end
end

#timeObject



87
88
89
# File 'lib/podcastinator/file_feed.rb', line 87

def time
  @time || File.mtime(local_filename)
end