Skip to main content

Google Drive API wrapper written in python 3.

Project description

gdnan

A Google Drive API wrapper written in python by @viperadnan with ♥️.

Project's Base

The base of the project taken from lzzy12's repo python-aria-mirror-bot and modified, improved and compiled in a pip package.

Installing

Install using pip

pip3 install gdnan

Usage

# Importing Modules
from gdnan import GoogleDrive, Auth, extractId, create_link

# Client ID & Secret Obtained from Google Developer Console
GooogleDriveClientID = "9999199991-128shwbnwinpcqv6hn7b29wuww4gmji.apps.googleusercontent.com"
GooogleDriveClientSecret = "2lhkFmsQ0W7wJaua72HwodjZ"


# The below method is only for one time, you cam use saved 
# credentials for later use.
# Call the Auth module 
auth = Auth(GooogleDriveClientID, GooogleDriveClientSecret)

# Get the Authorization URL
print("Open this url in browser and enter the received code")
print(auth.get_url())
received_code = input("Enter received code: ")

# Exchange received code and get credentials
# You can store the credentials wherever you want
credentials = auth.exchange_code(received_code)
# Or save it in token.pickle or any other file
                                    #optional
auth.exchange_code(received_code, "token.pickle")


# Now after generating .pickle or credentials you need to use only below steps
# Authorized via credentials
gd = GoogleDrive(credentials)
# Or You can Authorize via .pickle file too
# and can set the working directory, like for share drive.
workdir_id = extractId("https://drive.google.com/folderview?id=1Aricl6VpSiMmgFkgUSeTXiQh7WYxW6np")
                                 #optional
gd = GoogleDrive("token.pickle", workdir_id)

# Upload file from local storage
uploaded_file = gd.upload("path/to/file/or/folder/example.txt")
# Upload file to custom folder using folder's id               #Optional
uploaded_file = gd.upload("path/to/file/or/folder/example.txt", "root")
print(uploaded_file.name)
>> example.txt

# To get Google Drive url
print(uploaded_file.url)
>> https://drive.google.com/uc?id=10xN4KBjKJXUwIHUv1R5rihbthYuENMUB&export=download
# Alternate way
print(create_link(uploaded_file.id, uploaded_file.mimeType))
>> https://drive.google.com/uc?id=10xN4KBjKJXUwIHUv1R5rihbthYuENMUB&export=download

# To create a folder named "Hello World !"
#                           # Name       # Parent ID
folder = gd.create_folder("Hello World!", workdir_id)
print(folder.name)
>> Hello World!

# Clone/Copy file or folder, you can specify custom folder id by "folder" parameter
cloned_file = gd.clone(uploaded_file.id)
print(cloned_file.name)
>> example.txt

# Move file from one folder to another
gd.move(cloned_file.id, folder.id)

# Make files public or set permission for the file to publically viewable
gd.make_public(cloned_file.id)

# Search for the file       #optional    #optional
files, next_page_token = gd.search(uploaded_file.name, limit=2, folder=workdir_id)
for file in files:
    print(file.name)
>> example.txt

# Move file to trash
gd.delete(uploaded_file.id)
# Delete file permanently
gd.delete(folder.id, True)

# Empty Trash of the users account
gd.emptyTrash()

Using Service Accounts

If you want to use service accounts than put a copy of all of your service accounts in a folder and use code below

from gdnan import GoogleDrive
gd = GoogleDrive("path/to/service/account/folder", workdir_id)

this will automatically rename your service accounts to 0.json 1.json 2.json... (if not renamed) and automatically switch between service accounts if daily quota exceeded.

Testing

Test code by running test.py in your terminal with python3 test.py, don't forget to change the GooogleDriveClientID and GooogleDriveClientSecret.

Copyright & License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gdnan-1.0.0.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

gdnan-1.0.0-py3-none-any.whl (20.1 kB view details)

Uploaded Python 3

File details

Details for the file gdnan-1.0.0.tar.gz.

File metadata

  • Download URL: gdnan-1.0.0.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.5

File hashes

Hashes for gdnan-1.0.0.tar.gz
Algorithm Hash digest
SHA256 53edcda22955b382e67033c471274a732b3456ef5b5440f8941e8c62d6b9bbd1
MD5 b4a18f7f9014f3199d19aad0f5cc9f4e
BLAKE2b-256 3a930ca47a884b5f9fd92e1d7757433cb2851b554a01628f5ff5d8578b6a5e6d

See more details on using hashes here.

File details

Details for the file gdnan-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: gdnan-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 20.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.5

File hashes

Hashes for gdnan-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 021916935b9a0a8e29aa0fee30f8550f13f48ea83338359a3e29419948a3be2d
MD5 aa16deed03b72eb6316b1d6462624858
BLAKE2b-256 4f937551f9276bb4883bc65268fa90806492c82a3a18f96a359bd07264afdcf5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page