wkentaro / gdown

Google Drive Public File Downloader when Curl/Wget Fails
MIT License
4.29k stars 350 forks source link

adapt to /d/id links #94

Closed mbassiouny33 closed 3 years ago

mbassiouny33 commented 3 years ago

Hi there,

Gdown is surely a great tool, but I think forcing each user to adapt his links to uc?id= format may be too much trouble and time consuming so I suggest using a regex to match links, for example if url = the following format https://drive.google.com/file/d/0B1L_hFrWJfRhLUJZdXdSdTdfSWs/edit then rewrite it as : https://drive.google.com/u/0/uc?id=0B1L_hFrWJfRhLUJZdXdSdTdfSWs

I tried to do it with regex but I am not so good with regex so I did a dirty workaround in my nodejs code to split on delimeter slash if /d is found and grab the id

But lucky news is, I found a perl implementation doing my idea

if ($URL=~m#^https?://drive.google.com/file/d/([^/]+)#) {
    $URL="https://docs.google.com/uc?id=$1&export=download";
}
elsif ($URL=~m#^https?://drive.google.com/open\?id=([^/]+)#) {
    $URL="https://docs.google.com/uc?id=$1&export=download";
}

Source: https://github.com/circulosmeos/gdown.pl/blob/master/gdown.pl

It would be great if this could be added to gdown

Thanks

Edit:

I just noticed in parse_url file there is something that is supposed to do the same but the regex seems to be broken

match = re.match(r"^/file/d/(.*?)/view$", parsed.path)
    if match:
        file_id = match.groups()[0]

So I modified it by stealing the regex from the perl project I referenced and did a PR. (first time ever I do PR, sorry if there is nothing I did not do correctly)

My suggestion is :

match = re.match(r"https?://drive.google.com/file/d/([^/]+)", parsed.path)
    if match:
        file_id = match.groups(0)
    else:
        match = re.match(r"https?://drive.google.com/open\?id=([^/]+)", parsed.path)
        if match:
            file_id = match.groups(0)

Feel free to test it and accept the PR if it does not break anything