Class: PigMediaServer::KindleSend

Inherits:
Object
  • Object
show all
Defined in:
lib/pig-media-server/kindle_send.rb

Instance Method Summary collapse

Instance Method Details

#runObject



8
9
10
11
12
13
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
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pig-media-server/kindle_send.rb', line 8

def run
  while true
    begin
      GC.start
      pit_config = Pit.get 'Pig Media Server'
      from_hash = Hash[*Dir.glob("#{pit_config['user_data_path']}/*.json").map{|t| JSON.parse open(t).read}.select{|t| t['kindle_to'] and t['kindle_from']}.map{|t| [t['kindle_to'], t['kindle_from']]}.select{|t| t.first != '' and t.last != ''}.flatten]
      Dir.glob("#{pit_config['user_data_path']}/kindle/queue/*").each{|x|
        begin
          queue = x.split('/').last
          key = queue.split('_').first
          user_id = queue.split('_').last
          config = JSON.parse open("#{pit_config['user_data_path']}/#{user_id}.json").read
          p [key, user_id, config['kindle_to'], config['kindle_from']]

          pig = Pig.find key
          puts 'start send' 
          from_hash.each{|to, from|
            p [to, from]
            Pony.mail({
              :to => to,
              from: from,
              subject: '',
              body: '',
              attachments: {"#{key}.mobi" => File.read(pig.record.path)},
              :via => :smtp,
              :via_options => pit_config['smtp'].dup
            })
          }
        rescue =>e
          p e
          p e.backtrace
        end
        FileUtils.cd '/home/ssig33/dev/pig'
        FileUtils.rm x
        puts 'end '+x
      }
    rescue => e
      p e
      p e.backtrace
    end
    sleep 1
  end
end