Last active
November 12, 2023 19:08
-
-
Save hiroshil/d7d0029f6c816c5013bf3fd38d40dd1c to your computer and use it in GitHub Desktop.
Retrieve list of folder from Onedrive folder share link and generate direct download links
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64 | |
import json | |
import requests | |
# https://learn.microsoft.com/en-us/onedrive/developer/rest-api/api/shares_get?view=odsp-graph-online | |
# https://stackoverflow.com/questions/40454101/access-publicly-shared-onedrive-folder-via-api | |
# https://stackoverflow.com/questions/19483351/converting-json-string-to-dictionary-not-list | |
sharing_url = "PUT_SHARE_LINK_HERE" | |
exclude = [] | |
def download_folder(sharing_url, parent_name=''): | |
base64_value = base64.b64encode(sharing_url.encode('utf-8')).decode('utf-8') | |
encoded_url = "u!" + base64_value.rstrip('=').replace('/', '_').replace('+', '-') | |
r = requests.get("https://api.onedrive.com/v1.0/shares/" + encoded_url + "/root?expand=children") | |
res = json.loads(r.content) | |
print(f"Found {res['[email protected]']} items!") | |
links = [] | |
for child in res['children']: | |
if child['name'] not in exclude: | |
if '@content.downloadUrl' not in child: | |
parent_name += res['name'] + '_' | |
download_folder(child['webUrl'], parent_name) | |
else: | |
links.append(child['@content.downloadUrl']) | |
with open(parent_name + "links.txt","a") as f: | |
f.write('\n'.join(links)) | |
print("Done! Links are saved to links.txt!") | |
download_folder(sharing_url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment