Class: Ipodcastly::Sync

Inherits:
Object
  • Object
show all
Defined in:
lib/ipodcastly/sync.rb

Class Method Summary collapse

Class Method Details

.api_keyObject



3
4
5
6
7
8
9
10
# File 'lib/ipodcastly/sync.rb', line 3

def self.api_key
  begin
    @api_key ||= YAML::load(File.open(File.join(ENV['HOME'], '.ipodcastly')))['api_key']
  rescue
    puts "API key is missing."
    exit
  end
end

.start(options = {}) ⇒ Object

self.sync_podcastly



68
69
70
71
72
73
74
# File 'lib/ipodcastly/sync.rb', line 68

def self.start(options = {})
  puts "Syncing Podcastly -> iTunes"
  sync_itunes

  puts "\nSyncing iTunes -> Podcastly"
  sync_podcastly
end

.sync_itunesObject

User listened an episode in Podcastly, he didn’t listen it in the iTunes, the episode already downloaded in the iTunes.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ipodcastly/sync.rb', line 14

def self.sync_itunes
  Server::Podcast.all.each do |server_podcast|
    until client_podcast = Client::Podcast.find_by_title(server_podcast.title)
      Client::Podcast.subscribe(server_podcast.url)
      puts "Subscribing to #{server_podcast.url}"
    end

    puts "\n+ Podcast \"#{server_podcast.title}\""

    # Only episodes the user listened
    server_podcast.episodes.each do |server_episode|
      next unless client_episode = client_podcast.episodes.detect { |client_episode| client_episode.title == server_episode.title }

      if client_episode.updateable?
        if !server_episode.duration.zero? && (server_episode.position == server_episode.duration)
          client_episode.mark_as_listened
        else
          client_episode.position = server_episode.position / 1000.0
        end

        puts "    + Episode \"#{server_episode.title}\" #{server_episode.duration.zero? ? "#{(server_episode.position / 1000)} sec." : "#{(server_episode.position * 100/server_episode.duration).to_i}%"}"
      end
    end
  end
end

.sync_podcastlyObject

self.sync_itunes



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ipodcastly/sync.rb', line 40

def self.sync_podcastly
  Client::Podcast.all.each do |client_podcast|
    unless server_podcast = Server::Podcast.find_by_title(client_podcast.title)
      puts "\n- Podcast \"#{client_podcast.title}\" skipped"
      next
    else
      puts "\n+ Podcast \"#{client_podcast.title}\""
    end

    client_podcast.episodes.each do |client_episode|
      if client_episode.listened? && client_episode.audio?
        if server_podcast.episodes.detect { |e| e.title == client_episode.title }
          puts "    - Episode \"#{client_episode.title}\" skipped"
          next
        end

        server_episode = Server::Episode.new(nil, server_podcast.id, client_episode.title, client_episode.position * 1000, client_episode.duration * 1000)

        if server_episode.save
          puts "    + Episode \"#{client_episode.title}\" #{(client_episode.position * 100/client_episode.duration).to_i}%"
        else
          puts "    ? Episode \"#{client_episode.title}\" failed"
        end
      end
    end
  end
end