Class: MTP::Podcast

Inherits:
Object
  • Object
show all
Defined in:
lib/mtp/podcast.rb

Defined Under Namespace

Classes: Feed

Constant Summary collapse

@@logger =
Logger.new(STDERR)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device, options = {}) ⇒ Podcast

Returns a new instance of Podcast.



43
44
45
46
47
48
# File 'lib/mtp/podcast.rb', line 43

def initialize(device, options = {})
  @device = device
  @playlist_name = options[:playlist] || "podcasts"
  @configuration_dir = File.join(ENV['HOME'], '.mtp-podcast')
  @configuration_file = options[:configuration] || File.join(@configuration_dir, 'config.yml')
end

Class Method Details

.loggerObject



18
19
20
# File 'lib/mtp/podcast.rb', line 18

def self.logger
  @@logger
end

Instance Method Details

#configurationObject



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/mtp/podcast.rb', line 68

def configuration
  return @configuration unless @configuration.nil?

  Dir.mkdir(@configuration_dir) unless File.exists?(@configuration_dir)
  if File.exists?(@configuration_file)
    File.open(@configuration_file, 'r') do |f|
      @configuration = YAML.load(f)
    end
  else
    @configuration = default_configuration
  end
  @configuration
end

#default_configurationObject



64
65
66
# File 'lib/mtp/podcast.rb', line 64

def default_configuration
  [ "feeds" => [] ]
end

#feedsObject



82
83
84
85
86
87
88
# File 'lib/mtp/podcast.rb', line 82

def feeds
  @feeds ||= configuration["feeds"].keys.map do |k| 
    options = configuration["feeds"][k]
    options["directory"] = File.join(@configuration_dir, 'feeds', k) if options["directory"].nil?
    Feed.new(k, options)
  end
end

#loggerObject



22
23
24
# File 'lib/mtp/podcast.rb', line 22

def logger
  @@logger
end

#playlistObject



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mtp/podcast.rb', line 50

def playlist
  return @playlist unless @playlist.nil?
  
  @playlist = @device.playlists.detect { |p| p.name == @playlist_name }
  
  if @playlist.nil?
    @playlist = Playlist.new
    @device.send(@playlist)
    @playlist.name = @playlist_name
  end
    
  @playlist
end

#updateObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/mtp/podcast.rb', line 90

def update
  items = feeds.inject([]) { |ary, feed| ary + feed.items }
  items.sort! { |a, b| a.pubdate <=> b.pubdate }

  
  playlist.each do |track|
    in_rss_list = nil
    items.each do |item|
      if File.basename(item.enclosure.url) == track.filename
        in_rss_list = item
        break
      end
    end

    # remove from device the tracks which aren't in rss or which are too old
    if in_rss_list.nil? or in_rss_list.pubdate.to_time < in_rss_list.keep.days.ago
      puts "removing #{track.filename}"
      playlist.delete(track)
      @device.delete(track)
    else
      puts "skipping #{track.filename}"
      items.delete(in_rss_list)
    end
  end
  
  # download missing items
  items.each do |item|
    puts "downloading #{File.basename(item.enclosure.url)}"
    uri = URI.parse(item.enclosure.url)
    
    # connect to server
    Net::HTTP.start(uri.host, uri.port) do |http|
      
      # big hack to access socket io
      netio = http.instance_variable_get(:@socket)
      if netio.respond_to?(:io)
        sock = netio.io # 1.9
      else
        sock = netio.instance_variable_get(:@socket) # 1.8
      end
      
      # execute request
      request = Net::HTTP::Get.new(uri.path)
      request["host"] = (uri.port ? "#{uri.host}:#{uri.port}" : uri.host)
      request.exec(sock, Net::HTTP::HTTPVersion, request.path)
      
      # fetch headers
      while (str = sock.readline) != "\r\n"
        if str.match(/^Content-Length:\s*(\d+)\s*$/)
          length = $1.to_i
        end
      end

      # store object
      object = MP3Track.new
      object.filename = File.basename(item.enclosure.url)
      object.compressed_size = length
      @device.send(object, sock) do |written, total|
        printf("%02u%% %u/%u\r", written * 100/total, written, total)
      end
      printf("\n")
      object.name = item.title
      object.artist = item.itunes_author
      object.duration = item.itunes_duration * 1000
      playlist << object
    end
  end
end