Class: AppleEpf::Downloader

Inherits:
Object
  • Object
show all
Includes:
Finder, 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

Constants included from Finder

Finder::ITUNES_FULL_URL, Finder::ITUNES_INCREMENTAL_URL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Finder

#date_to_epf_format, #detect_closest_weeks_for_date, #file_exists?, #find_incremental, #get_daily_incremental_folders_within_week_url, #get_folders_from_url, #get_weekly_folders_from_full_url, #week_include_date?

Methods included from Logging

logger, #logger, #logger_info

Constructor Details

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

Returns a new instance of Downloader.



17
18
19
20
21
22
# File 'lib/apple_epf/downloader.rb', line 17

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.



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

def apple_filename_full
  @apple_filename_full
end

#dirpathObject



42
43
44
# File 'lib/apple_epf/downloader.rb', line 42

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

#download_toObject (readonly)

Returns the value of attribute download_to.



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

def download_to
  @download_to
end

#filedateObject

Returns the value of attribute filedate.



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

def filedate
  @filedate
end

#filenameObject

Returns the value of attribute filename.



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

def filename
  @filename
end

#force_urlObject

Returns the value of attribute force_url.



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

def force_url
  @force_url
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#downloadObject



35
36
37
38
39
40
# File 'lib/apple_epf/downloader.rb', line 35

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



83
84
85
# File 'lib/apple_epf/downloader.rb', line 83

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

#get_filename_by_date_and_typeObject



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
78
79
80
81
# File 'lib/apple_epf/downloader.rb', line 47

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?

  _full_url = apple_filename_full_url(path)
  unless file_exists?(_full_url)
    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"
      _full_url = apple_filename_full_url(path)
      raise AppleEpf::FileNotExist.new("File does not exist #{path}") unless file_exists?(_full_url)
    else
      raise AppleEpf::FileNotExist.new("File does not exist #{path}")
    end
  end

  @apple_filename_full_path = path
  @apple_filename_full_path
end

#prepareObject



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

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