module GoogleDriveUrl
Constants
- CONFIRM_HREF_PATTERN
- CONFIRM_INPUT_PATTERN
- CONFIRM_VALUE_FIRST_PATTERN
- DRIVE_HOSTS
- FILE_PATH_ID_PATTERN
- FORM_ID_PATTERN
- FORM_ID_VALUE_FIRST_PATTERN
- FORM_UUID_PATTERN
- FORM_UUID_VALUE_FIRST_PATTERN
- HTML_PREFIX_PATTERN
- META_REFRESH_PATTERN
- QUERY_ID_PATTERN
- USERCONTENT_HOST
Public Instance Methods
Source
# File lib/google_drive_url.rb, line 117 def confirmed_download_url(download_url, token) uri = URI.parse(download_url.to_s) params = URI.decode_www_form(uri.query.to_s) params.reject! { |key, _| key == "confirm" } params << ["confirm", token] uri.query = URI.encode_www_form(params) uri.to_s rescue URI::InvalidURIError separator = download_url.include?("?") ? "&" : "?" "#{download_url}#{separator}confirm=#{token}" end
Source
# File lib/google_drive_url.rb, line 129 def confirmed_url_from_warning_page(html) params = extract_download_form_params(html) return usercontent_download_url(params[:id], confirm: params[:confirm], uuid: params[:uuid]) if params extract_meta_refresh_url(html) end
Source
# File lib/google_drive_url.rb, line 61 def direct_download_url(file_id) usercontent_download_url(file_id) end
Source
# File lib/google_drive_url.rb, line 97 def extract_confirm_token(html) body = html.to_s body = body.dup.force_encoding(Encoding::BINARY) unless body.encoding == Encoding::BINARY return Regexp.last_match(1) if body.match(CONFIRM_INPUT_PATTERN) return Regexp.last_match(1) if body.match(CONFIRM_VALUE_FIRST_PATTERN) return Regexp.last_match(1) if body.match(CONFIRM_HREF_PATTERN) nil end
Source
# File lib/google_drive_url.rb, line 87 def extract_download_form_params(html) body = html.to_s id = body[FORM_ID_PATTERN, 1] || body[FORM_ID_VALUE_FIRST_PATTERN, 1] confirm = body[CONFIRM_INPUT_PATTERN, 1] || body[CONFIRM_VALUE_FIRST_PATTERN, 1] return nil unless id && confirm uuid = body[FORM_UUID_PATTERN, 1] || body[FORM_UUID_VALUE_FIRST_PATTERN, 1] { id: id, confirm: confirm, uuid: uuid } end
Source
# File lib/google_drive_url.rb, line 37 def extract_file_id(url) text = url.to_s return Regexp.last_match(1) if text.match(FILE_PATH_ID_PATTERN) return Regexp.last_match(1) if text.match(QUERY_ID_PATTERN) nil end
Source
# File lib/google_drive_url.rb, line 107 def extract_meta_refresh_url(html) body = html.to_s match = body.match(META_REFRESH_PATTERN) return nil unless match CGI.unescapeHTML(match[1].to_s.strip) rescue StandardError nil end
Source
# File lib/google_drive_url.rb, line 30 def google_drive_url?(url) uri = URI.parse(url.to_s) DRIVE_HOSTS.include?(uri.host.to_s.downcase) rescue URI::InvalidURIError false end
Source
# File lib/google_drive_url.rb, line 74 def html_warning_page?(body) sample = body.to_s.b.byteslice(0, 512).to_s return false if GoogleDriveBody.image_body?(body) return true if sample.match?(HTML_PREFIX_PATTERN) return true if sample.include?("uc-warning-caption") return true if sample.include?("download-form") return true if sample.match?(/name=["']confirm["']/i) return true if sample.match?(/http-equiv=["']refresh["']/i) false end
Source
# File lib/google_drive_url.rb, line 65 def resolve_download_url(url) return url unless google_drive_url?(url) file_id = extract_file_id(url) return url unless file_id direct_download_url(file_id) end
Source
# File lib/google_drive_url.rb, line 57 def uc_download_url(file_id) "https://drive.google.com/uc?export=download&id=#{file_id}" end
Source
# File lib/google_drive_url.rb, line 45 def usercontent_download_url(file_id, confirm: "t", uuid: nil) params = { id: file_id, export: "download", confirm: confirm } params[:uuid] = uuid if uuid && !uuid.to_s.empty? query = URI.encode_www_form(params) "https://#{USERCONTENT_HOST}/download?#{query}" end