Skip to content

Commit

Permalink
Support new Google URLs (#12)
Browse files Browse the repository at this point in the history
* New Google URLs

* Use replace and startswith

* Sync branch (#14)

* Update doc.yml

* fix

* add Project.toml

* fix

* add ci

* add methods docs

* Update readme
  • Loading branch information
JeffFessler authored Oct 3, 2023
1 parent cdd05df commit 56a648c
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 43 deletions.
22 changes: 8 additions & 14 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
name = "GoogleDrive"
uuid = "91feb7a0-3508-11ea-1e8e-afea2c1c9a19"
authors = ["tejasvaidhyadev <[email protected]>"]
version = "0.1.2"
authors = ["tejasvaidhyadev <[email protected]> and contributors"]
version = "0.2"

[deps]
DataDeps = "124859b0-ceae-595e-8997-d05f6a7a8dfe"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
DataDeps = "124859b0-ceae-595e-8997-d05f6a7a8dfe"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"


[compat]
julia = "1"
HTTP = "1.4.0"
julia = "1.9"
DataDeps = "0.7.10"
Documenter = "0.24,0.27"


[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
Documenter = "1"
HTTP = "1.4.0"
97 changes: 82 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
# GoogleDrive
Download files from Google Drive.

https://github.com/JuliaIO/GoogleDrive.jl

[![action status][action-img]][action-url]
[![pkgeval status][pkgeval-img]][pkgeval-url]
[![codecov][codecov-img]][codecov-url]
[![license][license-img]][license-url]
[![docs-stable][docs-stable-img]][docs-stable-url]
[![docs-dev][docs-dev-img]][docs-dev-url]

[![Build Status](https://travis-ci.com/JuliaIO/GoogleDrive.jl.svg?branch=master)](https://travis-ci.org/JuliaIO/GoogleDrive.jl/)

[![Build Status](https://travis-ci.com/tejasvaidhyadev/GoogleDrive.jl.svg?branch=master)](https://travis-ci.org/tejasvaidhyadev/)
[![](https://img.shields.io/badge/docs-stable-blue.svg)](https://tejasvaidhyadev.github.io/GoogleDrive.jl/)

## Introduction
GoogleDrive.jl provides support for downloading files from Google-Drive on top of Datadeps.

GoogleDrive.jl provides support for downloading files from Google Drive,
directly as an IO stream,
or on top of
[DataDeps.jl](https://github.com/oxinabox/DataDeps.jl).


### Installation
The package can be installed using the [julia package manager in the normal way.](https://julialang.github.io/Pkg.jl/v1/managing-packages/#Adding-packages-1).
Install the package using the
[julia package manager](https://julialang.github.io/Pkg.jl/v1/managing-packages/#Adding-packages-1):

Open the REPL, press <kbd>]</kbd> to enter package mode, and then:

Expand All @@ -19,29 +34,63 @@ pkg> add GoogleDrive

## Details

Some of the methods in this package
may no longer be needed
because of changes to Google Drive API.

To download data into an IO stream from a special URL of the form
`url = "https://docs.google.com/uc?export=download&id=1GqmszfSB_LHGQEQpSjoiPyDROZ5a8Ls4"`,
use the following code:

```julia
using Downloads: download
io = IOBuffer()
download(url, io)
str = String(take!(io)) # (this line for text data only)
```

To download data from a typical Google Drive URL of the form
`url = "https://drive.google.com/file/d/1GqmszfSB_LHGQEQpSjoiPyDROZ5a8Ls4"`,
use the following code
that converts the URL internally for convenience:

```julia
using GoogleDrive: google_download
io = IOBuffer()
google_download(url, io)
str = String(take!(io)) # (this line for text data only)
```


### drive_download
`drive_download(URL, localdir)`


Download flie from Google drive.
Download file from Google drive.
The above function only Download file from google drive.


### sheet_handler
`sheet_handler(long_url)`

Provide URL that can be use as link for registering in Datadeps


### google_download
`google_download(URL, localdir)`

It can also be consider as "maybe google download function"
The function can be used to download file from google-drive,google-sheets or HTTP download method
It can also be consider as "maybe google download function"
The function can be used to download a file from
google-drive, google-sheets or HTTP download method.


## Configuration
This package is build on top of [DataDeps.jl](https://github.com/oxinabox/DataDeps.jl).
To configure, e.g., where downloaded files save to, and read from (and to understand how that works),
This package is build on top of
[DataDeps.jl](https://github.com/oxinabox/DataDeps.jl).
To configure, e.g., where downloaded files save to, and read from
(and to understand how that works),
see the DataDeps.jl readme.


## Examples

Load the package with
Expand All @@ -52,9 +101,9 @@ julia> using GoogleDrive

### Loading different Embeddings

Downloading CSV file from google_drive using google_download function
Downloading CSV file from GoogleDrive using `google_download` function
```
julia>google_download("https://docs.google.com/spreadsheets/d/1tbNIGxnp8wLk31DIMNPD_Hi_CmIdpdTPfzMUDcs1xE/edit#gid=0","/home/iamtejas/Downloads")
julia> google_download("https://docs.google.com/spreadsheets/d/1tbNIGxnp8wLk31DIMNPD_Hi_CmIdpdTPfzMUDcs1xE/edit#gid=0", "/home/iamtejas/Downloads")
┌ Info: Downloading
│ source = "https://docs.google.com/spreadsheets/d/1tbNI-Gxnp8wLk31DIMNPD_Hi_CmIdpdTPfzMUDcs1xE/export?format=csv"
│ dest = "/home/iamtejas/Downloads/InternshipsToApplyFor-Sheet1.csv"
Expand All @@ -73,10 +122,28 @@ julia>google_download("https://docs.google.com/spreadsheets/d/1tbNIGxnp8wLk31DIM

Contributions, in the form of bug-reports, pull requests, additional documentation are encouraged. They can be made to the Github repository.

**All contributions and communications should abide by the [Julia Community Standards](https://julialang.org/community/standards/).**
**All contributions and communications should abide by the
[Julia Community Standards](https://julialang.org/community/standards/).**


### Reference
### Reference

Code:-
[Peter Cheng](https://github.com/chengchingwen/Transformers.jl) ,[HTTPS](https://github.com/JuliaWeb/HTTP.jl)
[Peter Cheng](https://github.com/chengchingwen/Transformers.jl), [HTTPS](https://github.com/JuliaWeb/HTTP.jl)


<!-- URLs -->
[action-img]: https://github.com/JuliaIO/GoogleDrive.jl/workflows/CI/badge.svg
[action-url]: https://github.com/JuliaIO/GoogleDrive.jl/actions
[build-img]: https://github.com/JuliaIO/GoogleDrive.jl/workflows/CI/badge.svg?branch=master
[build-url]: https://github.com/JuliaIO/GoogleDrive.jl/actions?query=workflow%3ACI+branch%3Amaster
[pkgeval-img]: https://juliaci.github.io/NanosoldierReports/pkgeval_badges/G/GoogleDrive.svg
[pkgeval-url]: https://juliaci.github.io/NanosoldierReports/pkgeval_badges/G/GoogleDrive.html
[codecov-img]: https://codecov.io/github/JuliaIO/GoogleDrive.jl/coverage.svg?branch=master
[codecov-url]: https://codecov.io/github/JuliaIO/GoogleDrive.jl?branch=master
[docs-stable-img]: https://img.shields.io/badge/docs-stable-blue.svg
[docs-stable-url]: https://JuliaIO.github.io/GoogleDrive.jl/stable
[docs-dev-img]: https://img.shields.io/badge/docs-dev-blue.svg
[docs-dev-url]: https://JuliaIO.github.io/GoogleDrive.jl/dev
[license-img]: https://img.shields.io/badge/license-MIT-brightgreen.svg
[license-url]: LICENSE
1 change: 1 addition & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ makedocs(
pages = [
"Home" => "index.md",
"Features" => "features.md",
"Methods" => "methods.md",
],
)

Expand Down
10 changes: 10 additions & 0 deletions docs/src/methods.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## Methods list

```@index
```

## Methods usage

```@autodocs
Modules = [GoogleDrive]
```
55 changes: 43 additions & 12 deletions src/GoogleDrive.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,49 @@
module GoogleDrive

using DataDeps
using Dates
using Random: randstring
using HTTP
export drive_download
export sheet_handler
export google_download
"""
unshortlink(url)
return unshorten url or the url if it is not a short link

using DataDeps
using Dates
using Downloads: download
using Random: randstring
using HTTP
export drive_download
export sheet_handler
export google_download


"
google_download_url(url::AbstractString)::String
Convert a GoogleDrive URL of the form
`https://drive.google.com/file/d/XYZ`
to the form needed for raw data download:
`https://docs.google.com/uc?export=download&id=XYZ`
"
function google_download_url(url::AbstractString)
old = "https://drive.google.com/file/d/"
new = "https://docs.google.com/uc?export=download&id="
startswith(url, old) || startswith(url, new) ||
throw(ArgumentError("Unknown URL form $url"))
return replace(url, old => new)
end


"""
google_download(url::AbstractString, io::IO)
Download data from Google URL `url` into `io`, returning `io`.
This mutates `io` so arguably it should be named `google_download!`,
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


"""
unshortlink(url)
return unshorten url or the url if it is not a short link
"""
function unshortlink(url; kw...)
rq = HTTP.request("HEAD", url; redirect=false, status_exception=false, kw...)
while rq.status ÷ 100 == 3
Expand All @@ -23,6 +53,7 @@ function unshortlink(url; kw...)
url
end


isg_sheet(url) = occursin("docs.google.com/spreadsheets", url)
isg_drive(url) = occursin("drive.google.com", url)

Expand Down Expand Up @@ -135,4 +166,4 @@ function drive_download(url, localdir)
filepath
end

end
end # Module
3 changes: 3 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[deps]
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
21 changes: 19 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
using GoogleDrive
using GoogleDrive: google_download, google_download_url
#using Downloads: download
using Test

@testset "GoogleDrive.jl" begin
# Write your own tests here.

src = "https://drive.google.com/file/d/XYZ"
dst = "https://docs.google.com/uc?export=download&id=XYZ"
@test google_download_url(src) == dst
@test google_download_url(dst) == dst
@test_throws ArgumentError google_download_url("foo")

#=
The following test reads a small Julia file from GoogleDrive.
The test will fail if that Julia file ever disappears.
=#
url = "https://drive.google.com/file/d/1GqmszfSB_LHGQEQpSjoiPyDROZ5a8Ls4"
io = IOBuffer()
google_download(url, io) # mutates io
str = String(take!(io))
@test str[1:2] == "#="

end

0 comments on commit 56a648c

Please sign in to comment.