aboutsummaryrefslogtreecommitdiff
path: root/_plugins/generate-torrent.rb
blob: fd38d85082f4eb95ee9570c64ebc79577a9dc775 (about) (plain) (blame)
1
2
3
4
5
6
7
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
TRACKERS = '-a udp://tracker.coppersurfer.tk:6969/announce -a udp://tracker.ccc.de:80/announce -a udp://tracker.publicbt.com:80 -a udp://tracker.istole.it:80 -a http://tracker.openbittorrent.com:80/announce -a http://tracker.ipv6tracker.org:80/announce'

module Jekyll
  class TorrentGenerator < Generator
    safe true

    MEDIA_EXTENSION = {
      'podcasts' => 'ogg',
      'screencasts' => 'webm'
    }

    def generate(site)
      site.collections.each do |name, collection|
        if ['podcasts', 'screencasts'].include? name then
          collection.docs.each do |document|
            date = document.data['date'].strftime('%Y-%m-%d')
            slug = document.data['slug']
            extension = MEDIA_EXTENSION[name]
            file = "#{date}-#{slug}.#{extension}"
            media =  "resources/#{name}/#{file}"
            torrent = "#{media}.torrent"

            unless File.exist? torrent then
              webseed = "#{site.config['url']}/#{media}"
              puts "Missing '#{torrent}' file, generating..."
              puts `mktorrent #{TRACKERS} -f -v -d -c '#{document.content}' -n #{file} -w #{webseed} -o #{torrent} #{media}`
            end

            checksum_file = "#{torrent}.checksum"
            checksum = `sha256sum #{media} #{torrent} | sha256sum | awk '{ print $1 }'`
            if File.exist? checksum_file then
              unless checksum == File.read(checksum_file)
                raise "Checksum mismatch for '#{media}'.\nRe-generate the torrent files and checksums with:\n\nrm '#{torrent}' '#{checksum_file}'"
              end
            else
              puts "Missing checksum for '#{media}', generating..."
              File.write(checksum_file, checksum)
            end
          end
        end
      end
    end
  end
end