Module: AppleEpf::Finder
Constant Summary collapse
- ITUNES_FULL_URL =
"https://feeds.itunes.apple.com/feeds/epf/v3/full/".freeze
- ITUNES_INCREMENTAL_URL =
"https://feeds.itunes.apple.com/feeds/epf/v3/full/%s/incremental/".freeze
Instance Method Summary collapse
- #date_to_epf_format(date) ⇒ Object
-
#detect_closest_weeks_for_date(all_weeks, date) ⇒ Object
date is Date object.
- #file_exists?(full_url) ⇒ Boolean
-
#find_incremental(date, filename) ⇒ Object
AppleEpf::Finder.find_incremental(Date.parse(‘20140311’), ‘popularity’) => “feeds.itunes.apple.com/feeds/epf/v3/full/20140305/incremental/20140311/popularity20140311.tbz”.
- #get_daily_incremental_folders_within_week_url(week_folder) ⇒ Object
- #get_folders_from_url(url) ⇒ Object
- #get_weekly_folders_from_full_url ⇒ Object
-
#week_include_date?(week_date, daily_date) ⇒ Boolean
daily_date is Date object.
Instance Method Details
#date_to_epf_format(date) ⇒ Object
64 65 66 |
# File 'lib/apple_epf/finder.rb', line 64 def date_to_epf_format(date) date.strftime("%Y%m%d") end |
#detect_closest_weeks_for_date(all_weeks, date) ⇒ Object
date is Date object
33 34 35 36 |
# File 'lib/apple_epf/finder.rb', line 33 def detect_closest_weeks_for_date(all_weeks, date) date_range = ((date - 16)..(date + 16)).to_a all_weeks.select{|d| date_range.include?(Date.parse(d))} end |
#file_exists?(full_url) ⇒ Boolean
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/apple_epf/finder.rb', line 68 def file_exists?(full_url) uri = URI.parse(full_url) request = Net::HTTP::Head.new(full_url) request.basic_auth(AppleEpf.apple_id, AppleEpf.apple_password) r = Net::HTTP.new(uri.host, uri.port) r.use_ssl = true response = r.start { |http| http.request(request) } raise AppleEpf::BadCredentialsError.new('Bad credentials') if response.code == "401" response.code == "200" end |
#find_incremental(date, filename) ⇒ Object
AppleEpf::Finder.find_incremental(Date.parse(‘20140311’), ‘popularity’)
> “feeds.itunes.apple.com/feeds/epf/v3/full/20140305/incremental/20140311/popularity20140311.tbz”
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/apple_epf/finder.rb', line 9 def find_incremental(date, filename) all_weeks = get_weekly_folders_from_full_url potential_dates = detect_closest_weeks_for_date(all_weeks, date) founded_url = nil potential_dates.reverse.each do |potential| found = week_include_date?(potential, date) if found _founded_url = (ITUNES_INCREMENTAL_URL % potential) + date_to_epf_format(date) + "/" + "#{filename}#{date_to_epf_format(date)}.tbz" founded_url = _founded_url if file_exists?(_founded_url) break if founded_url end end founded_url end |
#get_daily_incremental_folders_within_week_url(week_folder) ⇒ Object
38 39 40 41 |
# File 'lib/apple_epf/finder.rb', line 38 def get_daily_incremental_folders_within_week_url(week_folder) url = ITUNES_INCREMENTAL_URL % week_folder get_folders_from_url(url) end |
#get_folders_from_url(url) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/apple_epf/finder.rb', line 48 def get_folders_from_url(url) uri = URI(url) req = Net::HTTP::Get.new(uri.request_uri) req.basic_auth(AppleEpf.apple_id, AppleEpf.apple_password) res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) {|http| http.request(req) } body = res.body avaliable_dates = Nokogiri::HTML(body).xpath("//td/a").map(&:text).select{|s| s[0..7] =~ /\d{8}/}.map{|s| s.chomp("/")} avaliable_dates end |
#get_weekly_folders_from_full_url ⇒ Object
43 44 45 |
# File 'lib/apple_epf/finder.rb', line 43 def get_weekly_folders_from_full_url get_folders_from_url(ITUNES_FULL_URL) end |
#week_include_date?(week_date, daily_date) ⇒ Boolean
daily_date is Date object
27 28 29 30 |
# File 'lib/apple_epf/finder.rb', line 27 def week_include_date?(week_date, daily_date) dates = get_daily_incremental_folders_within_week_url(week_date) dates.detect{|d| Date.parse(d) == daily_date} end |