Module: Download

Defined in:
lib/sphinx_tv/download.rb

Class Method Summary collapse

Class Method Details

.exists?Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
# File 'lib/sphinx_tv/download.rb', line 38

def self.exists?
  if File.exists?(filename)
    return true
  else
    puts "File hasn't been downloaded #{filename}".red
    return false
  end
end

.find_mounted_volume(search) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sphinx_tv/download.rb', line 61

def self.find_mounted_volume search
  not_found = true
  retries = 0
  while not_found && retries < 30
    volumes = Dir.glob "/Volumes/#{search}"
    if volumes.size == 0
      retries += 1
      sleep 2
    elsif volumes.size > 1
      puts "Too many similar disk images mounted, please unmount all but latest version".red
      puts "Here are the mounted matching images:"
      volumes.each do |volume|
        puts volume
      end
      return ""
    else
      return volumes[0]
    end
  end
  return ""
end

.mount(filename, volume_search, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sphinx_tv/download.rb', line 47

def self.mount(filename, volume_search, &block)
  if File.exists?(filename)
    result = %x[open #{filename}]
    volume = self.find_mounted_volume volume_search
    if volume.size == 0
      puts "Could not find #{filename} volume".red
    else
      yield volume
    end
  else
    yield filename
  end
end

.open_volume(filename, volume_search, &block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sphinx_tv/download.rb', line 24

def self.open_volume(filename, volume_search, &block)
  if File.exists?(filename)
    result = %x[open #{filename}]
    volume = find_mounted_volume volume_search
    if volume.size == 0
      puts "Could not find #{filename} volume".red
    else
      yield volume
    end
  else
    puts "File hasn't been downloaded #{filename}".red
  end
end

.url(file_url, filename) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sphinx_tv/download.rb', line 2

def self.url file_url, filename
  z_option = (File.exists?(filename) ? " -z #{filename}" : "")
  result = %x[curl -L -w "%{http_code} %{url_effective}"#{z_option} -o #{filename}.download #{file_url}]
  if /304/.match(result)
    puts "Up to date: ".green + filename
    return false
  else
    if /200/.match(result)
      begin
        File.rename "#{filename}.download", filename
      rescue
      end
      puts "Updated: ".yellow + filename
      return true
    else
      File.delete "#{filename}.download" if File.exists? "#{filename}.download"
      puts "File not found: ".red + file_url
      return false
    end
  end
end