Skip to content

Commit

Permalink
Merge pull request #13 from chengchingwen/fix_gcode
Browse files Browse the repository at this point in the history
Fix google drive download
  • Loading branch information
foldfelis authored Feb 20, 2022
2 parents e474e92 + 8487429 commit 684b088
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions src/gdrive.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@ function gfile_handler(url)
return "https://docs.google.com/uc?export=download&id=$(h.captures[])"
end

function find_gcode(cookies)
for cookie in cookies
(match(r"download_warning_", cookie.name) !== nothing) && (return cookie.value)
end

return
end

function find_filename(header)
m = match(r"filename=\\\"(.*)\\\"", header)
if isnothing(m)
Expand All @@ -64,16 +56,16 @@ function find_filename(header)
end

function download_gdrive(url, localdir)
cookiejars = Dict{String, Set{HTTP.Cookies.Cookie}}()
HTTP.request("GET", url; cookies=true, cookiejar=cookiejars)
gcode = find_gcode(cookiejars["docs.google.com"])
m = match(r"confirm=([^;&]+)", url)
isnothing(m) && (url = "$url&confirm=pbef")

!isnothing(gcode) && (url = "$url&confirm=$gcode")
r = HTTP.head(url; status_exception = false)
HTTP.iserror(r) && r.status != 303 && throw(HTTP.StatusError(r.status, r.request.method, r.request.target, r))
r.status == 303 && (url = r["Location"])

filepath = Ref{String}("")
HTTP.open(
"GET", url, ["Range"=>"bytes=0-"],
cookies=true, cookiejar=cookiejars, redirect_limit=10
"GET", url, ["Range"=>"bytes=0-"], redirect_limit=10
) do stream
response = HTTP.startread(stream)
eof(stream) && return
Expand Down

0 comments on commit 684b088

Please sign in to comment.