Python Program To Generate G-Drive Direct Download Link [without API key]

If you're planning to create a tool that generates direct download links from Google Drive shareable links, you'll typically need to obtain a Google Drive API key from your personal Google Cloud Console account.

But there are limitations come along with it.

What are the limitations of using G-Drive API key?

Quota Limitations:

  • Daily Usage Limit: The Google Drive API enforces a daily quota on API calls, restricting the number of actions your tool can perform per day. Exceeding this limit can result in errors or service stoppage.
  • Rate Limiting: API calls are also subject to rate limits, which regulate the frequency of requests within a short timespan. Rapid calls can trigger temporary restrictions.

API Changes:

  • Potential for Disruption: Google may update or modify the Drive API, potentially impacting tool functionality and necessitating code adjustments to maintain compatibility.

Terms of Service: 

  • Strict Adherence Required: Google's Terms of Service outline permissible API usage. Violations can lead to API key suspension or termination, disrupting tool operations.

So these three reasons may stop you from creating this tool. But what if I told you that you wouldn't even need an API key for creating this tool and also no issues like quota limitations.

Logic behind the program: Getting the drive id from the shareable link will be enough which eats the major part of the logic. After getting that, using print commands we can generate the download link and shrink it as well.

Here is the Python code that can generate direct download link from an shareable link and shrinks the URL using TinyURL shortener (optional). 



import pyshorteners

def get_google_drive_id(link):
    # extract the sharing ID from the link
    start_pos = link.find("/d/") + 3
    end_pos = link.find("/view")
    if end_pos == -1:
        end_pos = len(link)
    drive_id = link[start_pos:end_pos]
    return drive_id

link = input("Enter the drive shareable link: ")
drive_id = get_google_drive_id(link)
print(f"\nThe id is: {drive_id}\n")
final_url = f"https://drive.google.com/uc?export=download&id={drive_id}"
print(f"\nThe direct link is: {final_url}\n")

url = final_url
shortener = pyshorteners.Shortener()
x = shortener.tinyurl.short(url)

print(f"\nThe short url: {x}\n")

Note: This method is useful only if the size of Google Drive file is not more than 100 MB.

The code can be modified for bulk link generation as well which will be a big help if you are a blogger who use Google Drive to host your images and other medias.

Comments