module GoogleDriveDownload
Constants
- MAX_CONFIRM_FOLLOWS
Public Instance Methods
Source
# File lib/google_drive_download.rb, line 12 def applicable?(url) GoogleDriveUrl.google_drive_url?(url) end
Source
# File lib/google_drive_download.rb, line 86 def confirmed_url_from_body(body, download_url:, original_url:) confirmed = GoogleDriveUrl.confirmed_url_from_warning_page(body) return confirmed if confirmed token = GoogleDriveUrl.extract_confirm_token(body) return nil unless token file_id = GoogleDriveUrl.extract_file_id(download_url) || GoogleDriveUrl.extract_file_id(original_url) if file_id GoogleDriveUrl.usercontent_download_url(file_id, confirm: token) else GoogleDriveUrl.confirmed_download_url(download_url, token) end end
Source
# File lib/google_drive_download.rb, line 16 def fetch(url, fetch:, api_key: nil) fetch_with_trace(url, fetch: fetch, api_key: api_key)[:body] end
Source
# File lib/google_drive_download.rb, line 101 def fetch_step(steps, url, fetch) body = fetch.call(url) steps << GoogleDriveBody.describe_fetch(url, body) body rescue StandardError => e steps << GoogleDriveBody.describe_fetch(url, nil, error: e) nil end
Source
# File lib/google_drive_download.rb, line 20 def fetch_with_trace(url, fetch:, api_key: nil) steps = [] body = try_api_download(url, fetch: fetch, api_key: api_key, steps: steps) return { body: body, steps: steps } if body && GoogleDriveBody.image_body?(body) scrape_result = scrape_with_trace(url, fetch: fetch) steps.concat(scrape_result[:steps]) { body: scrape_result[:body], steps: steps } end
Source
# File lib/google_drive_download.rb, line 55 def follow_warning_pages(body, url:, download_url:, steps:, fetch:) current_body = body current_url = download_url attempts = 0 while current_body && GoogleDriveUrl.html_warning_page?(current_body) && attempts < MAX_CONFIRM_FOLLOWS attempts += 1 next_url = confirmed_url_from_body(current_body, download_url: current_url, original_url: url) break unless next_url break if steps.any? { |step| step[:url] == next_url } current_url = next_url current_body = fetch_step(steps, next_url, fetch) end current_body end
Source
# File lib/google_drive_download.rb, line 42 def scrape_with_trace(url, fetch:) steps = [] download_url = GoogleDriveUrl.resolve_download_url(url) body = fetch_step(steps, download_url, fetch) if applicable?(url) body = follow_warning_pages(body, url: url, download_url: download_url, steps: steps, fetch: fetch) body = try_uc_fallback(body, url: url, steps: steps, fetch: fetch) end { body: body, steps: steps } end
Source
# File lib/google_drive_download.rb, line 30 def try_api_download(url, fetch:, api_key:, steps:) key = api_key.to_s.strip return nil unless GoogleDriveApi.configured?(api_key: key) file_id = GoogleDriveUrl.extract_file_id(url) return nil unless file_id api_result = GoogleDriveApi.fetch_with_trace(file_id, fetch: fetch, api_key: key) steps.concat(api_result[:steps]) api_result[:body] end
Source
# File lib/google_drive_download.rb, line 73 def try_uc_fallback(body, url:, steps:, fetch:) return body if body.nil? || GoogleDriveBody.image_body?(body) return body unless applicable?(url) file_id = GoogleDriveUrl.extract_file_id(url) return body unless file_id uc_url = GoogleDriveUrl.uc_download_url(file_id) return body if steps.any? { |step| step[:url] == uc_url } fetch_step(steps, uc_url, fetch) end