Skip to content

Commit

Permalink
refactor: export a google_download_url function
Browse files Browse the repository at this point in the history
  • Loading branch information
flexagoon committed Nov 3, 2023
1 parent bdd5042 commit 2f32921
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
10 changes: 10 additions & 0 deletions docs/src/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,15 @@ Downloading a ZIP file from Drive using the `google_download` function
```
julia> google_download("https://drive.google.com/file/d/0B9w48e1rj-MOLVdZRzFfTlNsem8/view", open("file.zip", "w"))
"/home/iamtejas/Downloads/file.zip"
```

### google_download_url

Convert a direct Google Drive / Google Sheets URL to the direct download form.

#### Example

```
julia> google_download_url("https://drive.google.com/file/d/0B9w48e1rj-MOLVdZRzFfTlNsem8/view")
"https://docs.google.com/uc?export=download&id=0B9w48e1rj-MOLVdZRzFfTlNsem8"
```
14 changes: 12 additions & 2 deletions src/GoogleDrive.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module GoogleDrive

using Downloads: download

export google_download
export google_download, google_download_url

"""
google_download(url::AbstractString, io::IO)
Expand All @@ -13,14 +13,24 @@ but `Downloads.download` also mutates an `IO` argument
so we follow its convention.
"""
function google_download(url::AbstractString, io::IO)
url = google_download_url(url)
return download(url, io)
end

"""
google_download_url(url::AbstractString)
Convert a direct Google Drive/Sheets URL to a direct download
URL. The resulting URL can be used with `Downloads`, `DataDeps`
or any other data fetching package.
"""
function google_download_url(url::AbstractString)
if is_drive_url(url)
url = drive_download_url(url)
elseif is_sheet_url(url)
url = sheet_download_url(url)
else
throw(ArgumentError("Unknown URL form $url"))
end
return download(url, io)
end

is_sheet_url(url) = occursin("docs.google.com/spreadsheets", url)
Expand Down

0 comments on commit 2f32921

Please sign in to comment.