Class: AppleEpf::Downloader

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/apple_epf/downloader.rb

Constant Summary collapse

ITUNES_FLAT_FEED_URL =
'https://feeds.itunes.apple.com/feeds/epf/v3/full'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

logger, #logger, #logger_info

Constructor Details

#initialize(type, filename, filedate, force_url = nil) ⇒ Downloader

Returns a new instance of Downloader.



15
16
17
18
19
20
# File 'lib/apple_epf/downloader.rb', line 15

def initialize(type, filename, filedate, force_url = nil)
  @type = type
  @filename = filename #itunes, popularity, match, pricing
  @filedate = filedate
  @force_url = force_url
end

Instance Attribute Details

#apple_filename_fullObject (readonly)

Returns the value of attribute apple_filename_full.



13
14
15
# File 'lib/apple_epf/downloader.rb', line 13

def apple_filename_full
  @apple_filename_full
end

#dirpathObject



40
41
42
# File 'lib/apple_epf/downloader.rb', line 40

def dirpath
  File.join((@dirpath || AppleEpf.extract_dir), @type)
end

#download_toObject (readonly)

Returns the value of attribute download_to.



13
14
15
# File 'lib/apple_epf/downloader.rb', line 13

def download_to
  @download_to
end

#filedateObject

Returns the value of attribute filedate.



11
12
13
# File 'lib/apple_epf/downloader.rb', line 11

def filedate
  @filedate
end

#filenameObject

Returns the value of attribute filename.



11
12
13
# File 'lib/apple_epf/downloader.rb', line 11

def filename
  @filename
end

#force_urlObject

Returns the value of attribute force_url.



11
12
13
# File 'lib/apple_epf/downloader.rb', line 11

def force_url
  @force_url
end

#typeObject

Returns the value of attribute type.



11
12
13
# File 'lib/apple_epf/downloader.rb', line 11

def type
  @type
end

Instance Method Details

#downloadObject



33
34
35
36
37
38
# File 'lib/apple_epf/downloader.rb', line 33

def download
  prepare
  @download_processor = AppleEpf.download_processor.new(@apple_filename_full, @download_to)
  @download_processor.download_and_check
  @download_to
end

#downloaded_file_base_nameObject



79
80
81
# File 'lib/apple_epf/downloader.rb', line 79

def downloaded_file_base_name
  File.basename(@download_to, '.tbz') #popularity20130109
end

#get_filename_by_date_and_typeObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/apple_epf/downloader.rb', line 45

def get_filename_by_date_and_type
  #today = DateTime.now
  path = ""
  case @type
    when "full"
      path = "#{main_dir_date}/#{@filename}#{main_dir_date}.tbz"

    when "incremental"
      date_of_file = date_to_epf_format(@filedate)
      path = "#{main_dir_date}/incremental/#{date_of_file}/#{@filename}#{date_of_file}.tbz"

    when "file"
      #TODO: FIX THIS
      # date = date_to_epf_format( @filedate, check_if_in_previous_week, check_if_in_thursday )
      # path = "#{file}#{date}.tbz"
  end

  # Return false if no url was suggested or file does not exist
  raise AppleEpf::DownloaderError.new("Unable to find out what file do you want to download") if path.empty?

  unless file_exists?(path)
    if @type == 'incremental'
      #force prev week. Apple sometimes put files for Sunday to prev week, not current.
      path = "#{main_dir_date(true)}/incremental/#{date_of_file}/#{@filename}#{date_of_file}.tbz"
      raise AppleEpf::FileNotExist.new("File does not exist #{path}") unless file_exists?(path)
    else
      raise AppleEpf::FileNotExist.new("File does not exist #{path}")
    end
  end

  @apple_filename_full_path = path
  @apple_filename_full_path
end

#prepareObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/apple_epf/downloader.rb', line 22

def prepare
  _prepare_folders
  if @force_url
    @apple_filename_full = @force_url
  else
    get_filename_by_date_and_type
    @apple_filename_full = apple_filename_full_url(@apple_filename_full_path)
  end
  @download_to = File.join(dirpath, File.basename(@apple_filename_full))
end