Skip to content

Commit

Permalink
Added direct link for onedrive files (UsergeTeam#337)
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
  • Loading branch information
naninaveen authored May 25, 2021
1 parent f2e96ca commit 858a561
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions userge/plugins/utils/direct_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
import re
import json
import urllib.parse
from base64 import standard_b64encode
from os import popen
from random import choice
from urllib.parse import urlparse

import requests
from bs4 import BeautifulSoup

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

Expand All @@ -23,7 +24,7 @@
'header': "Generate a direct download link",
'supported links': [
'Google Drive', 'Cloud Mail', 'Yandex.Disk', 'AFH',
'MediaFire', 'SourceForge', 'OSDN', 'GitHub'],
'MediaFire', 'SourceForge', 'OSDN', 'GitHub', 'Onedrive'],
'usage': "{tr}direct [link]"})
async def direct_(message: Message):
"""direct links generator"""
Expand All @@ -36,7 +37,7 @@ async def direct_(message: Message):
if not links:
await message.err("No links found!")
return
reply = "**Direct Links** :\n\n"
reply = "<b>Direct Links</b> :\n\n"
for link in links:
if 'drive.google.com' in link:
reply += f" πŸ‘‰ {gdrive(link)}\n"
Expand All @@ -54,9 +55,11 @@ async def direct_(message: Message):
reply += f" πŸ‘‰ {github(link)}\n"
elif 'androidfilehost.com' in link:
reply += f" πŸ‘‰ {androidfilehost(link)}\n"
elif "1drv.ms" in link:
reply += f" πŸ‘‰ {onedrive(link)}\n"
else:
reply += f" πŸ‘€ {link} is not supported!\n"
await message.edit(reply)
await message.edit(reply, parse_mode="html")


def gdrive(url: str) -> str:
Expand Down Expand Up @@ -276,6 +279,13 @@ def androidfilehost(url: str) -> str:
return reply


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


def useragent():
"""useragent random setter"""
useragents = BeautifulSoup(
Expand Down

0 comments on commit 858a561

Please sign in to comment.