Last active
March 10, 2024 13:26
-
-
Save thedtvn/5932ef95198c21a3b1875b2b5537d111 to your computer and use it in GitHub Desktop.
download pdf onedrive that block download
This file contains 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 json | |
import re | |
import requests | |
from urllib.parse import urlparse, parse_qs | |
file = input("File url: ") | |
def custom_url_encode(input_string): | |
encoded_string = "" | |
for char in input_string: | |
# Encode alphanumeric characters | |
if char.isalnum(): | |
encoded_string += char | |
else: | |
# Encode non-alphanumeric characters | |
encoded_string += "%" + "{:02X}".format(ord(char)) | |
return encoded_string | |
session = requests.Session() | |
session.headers.update({ | |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.142.86 Safari/537.36"}) | |
real_res = session.get(file) | |
url_parse = urlparse(real_res.url) | |
page_data_raw = re.search(r"var _spPageContextInfo=(.*?);", real_res.text).group(1) | |
page_data = json.loads(page_data_raw) | |
query = parse_qs(url_parse.query) | |
file_id = query["id"][0] | |
root_info = session.get(page_data["driveInfo"][".driveUrl"]).json() | |
a1_data = urlparse(root_info["webUrl"]).path | |
# to do custom encode | |
file_raw = file_id.rsplit('/', 1) | |
RootFolder = custom_url_encode(file_raw[0]) | |
file_name = file_raw[1] | |
a1 = custom_url_encode(f"'{(a1_data)}'") | |
hd = {"accept": "application/json;odata=verbose", "content-type": "application/json;odata=verbose"} | |
body = {"parameters": {"__metadata": {"type": "SP.RenderListDataParameters"}, "RenderOptions": 5445383, | |
"AllowMultipleValueFilterForTaxonomyFields": True, "AddRequiredFields": True, | |
"RequireFolderColoringFields": True}} | |
path_sub_api = f"/_api/web/GetListUsingPath(DecodedUrl=@a1)/RenderListDataAsStream?@a1={a1}&RootFolder={RootFolder}" | |
rurl = page_data["webAbsoluteUrl"] + path_sub_api | |
full_info = session.post(rurl, headers=hd, data=json.dumps(body)).json() | |
env_json = full_info["ListSchema"] | |
env_json[".method"] = "passthrough" | |
for i in full_info["ListData"]["Row"]: | |
env_json.update(i) | |
for k, v in env_json.copy().items(): | |
if k.startswith("."): | |
env_json[k.replace(".", "")] = v | |
file_data = session.get(full_info["ListSchema"][".transformUrl"].replace(".", "").format(**env_json)).content | |
with open(file_name, "wb") as f: | |
f.write(file_data) |
This file contains 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
requests |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment