Skip to content

Commit

Permalink
Minor fixes (direct link for onedrive files) (UsergeTeam#340)
Browse files Browse the repository at this point in the history
* Added direct link for onedrive files

* fixed deepsource errors

* wrong use of requests.get

* fixing double hyphen (--) in urls

due to markdown parsing double hyphens (--) in urls are stripped, so using html parsing instead

* updated onedrive function

* using "html" parsing mode, breaking other direct links as they using markdown anchor format for links
* modified onedrive function to return links in the markdown anchor format as used for other links
  • Loading branch information
naninaveen authored May 27, 2021
1 parent 748206a commit 0a17543
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion userge/plugins/utils/direct_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import requests
from bs4 import BeautifulSoup

from userge import userge, Message
from userge.utils import humanbytes

Expand Down Expand Up @@ -283,7 +284,14 @@ def onedrive(link: str) -> str:
link_without_query = urlparse(link)._replace(query=None).geturl()
direct_link_encoded = str(standard_b64encode(bytes(link_without_query, "utf-8")), "utf-8")
direct_link1 = f"https://api.onedrive.com/v1.0/shares/u!{direct_link_encoded}/root/content"
return requests.head(direct_link1).next.url
resp = requests.head(direct_link1)
if resp.status_code != 302:
return "`Error: Unauthorized link, the link may be private`"
dl_link = resp.next.url
file_name = dl_link.rsplit("/", 1)[1]
resp2 = requests.head(dl_link)
dl_size = humanbytes(int(resp2.headers["Content-Length"]))
return f"[{file_name} ({dl_size})]({dl_link})"


def useragent():
Expand Down

0 comments on commit 0a17543

Please sign in to comment.