Last active
February 25, 2025 09:28
-
-
Save elderlabs/51e8e25ca8e85228ffd5e2de7b9b748b to your computer and use it in GitHub Desktop.
A YoutubeDL wrapper, built to diagnose and tweak interactions between Sinusbot and YTDL.
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
#!/usr/bin/python3 -u | |
""" | |
A YoutubeDL wrapper, built to diagnose and tweak interactions between Sinusbot and YTDL. | |
TL;DR something to fix Soundcloud, given it's prone to breaking quite often. | |
To use this script, it must be accessible and executable by your Sinusbot user. I recommend r-x (500) perms. | |
Support is not guaranteed and there are absolutely no warranties, implied or otherwise. Modify this script at your own risk. | |
Written by: Dooley_labs <dooleylabs.com> | https://gist.github.com/elderlabs/51e8e25ca8e85228ffd5e2de7b9b748b | |
Version: 1.3.1 (2025-02-25) | |
""" | |
import json | |
import requests | |
import subprocess | |
import sys | |
LOG = False # if set to True, the logfile below MUST EXIST, otherwise this script will fail. | |
LOG_PATH = '/opt/sinusbot/ytdl_wrapper.log' | |
YTDL_PATH = '/usr/local/bin/yt-dlp' | |
# I need cookies because I am hosting Sinusbot from a datacenter and my IP has been banned for commercial use and abuse of the Google API | |
# If you need cookies, refer to yt-dlp's documentation to export yours. Use a burner account. You have been warned. | |
COOKIE = False | |
COOKIE_PATH = '/opt/sinusbot/cookies.txt' | |
# enable the ability to search YT natively in commands? (may break things) | |
YTDL_SEARCH = False | |
#enable prepending `youtu.be` to what may be a YT media ID? | |
YTDL_PREPEND = False | |
def ytdlp_wrapper(): | |
if YTDL_PREPEND and '--no-call-home' in sys.argv and not sys.argv[-1].startswith('http') and len(sys.argv[-1]) == 11: | |
sys.argv[-1] = f"https://youtu.be/{sys.argv[-1]}" | |
if YTDL_SEARCH and '--no-call-home' in sys.argv and not sys.argv[-1].startswith('http'): | |
sys.argv[-1] = f"ytsearch1:{sys.argv[-1]}" | |
if LOG: | |
log = open(LOG_PATH, 'a') | |
log.write(f'called with args: {" ".join(x for x in sys.argv[1:])}\n') | |
log.close() | |
if COOKIE: | |
proc_args = [YTDL_PATH, '--cookies', COOKIE_PATH] + sys.argv[1:] | |
else: | |
proc_args = [YTDL_PATH] + sys.argv[1:] | |
proc = subprocess.Popen(proc_args, stdout=subprocess.PIPE) | |
data, _ = proc.communicate() | |
data = str(data, "utf=8") | |
if '--version' not in sys.argv: | |
data = json.loads(data) | |
if '--no-call-home' in sys.argv: | |
if YTDL_SEARCH and 'entries' in data: | |
data = data['entries'][0] | |
if 'acodec' not in data and 'audio_ext' in data: | |
data['acodec'] = data['audio_ext'] | |
if 'formats' in data: | |
formats_list = [] | |
for x in data['formats']: | |
if x['url'] != data['url']: | |
continue | |
if not 'acodec' in x and 'audio_ext' in x: | |
x['acodec'] = x['audio_ext'] | |
if not 'filesize' in x: | |
x['filesize'] = int(requests.head(x['url']).headers['Content-Length']) | |
formats_list.append(x) | |
data['formats'] = formats_list | |
new_data = {} | |
data_keys = ['title', 'uploader', 'original_url', 'formats', 'thumbnail'] | |
format_keys = ['abr', 'asr', 'acodec', 'filesize', 'url'] | |
for x in data_keys: | |
try: | |
new_data[x] = data[x] | |
except KeyError: | |
continue | |
new_formats = new_data['formats'][0].copy() | |
for k, v in new_data['formats'][0].items(): | |
if k not in format_keys: | |
del new_formats[k] | |
new_data['formats'][0] = new_formats | |
data = new_data | |
else: | |
return print(data) | |
return print(json.dumps(data)) | |
if __name__ == '__main__': | |
ytdlp_wrapper() |
Thanks for the script, it works as far as making it possible to play certain problematic tracks but I believe there's a problem in the logging because instead of creating the specified log-file it just creates a file named LOG_PATH in /opt/sinusbot ^^
Corrected. Thank you.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the script, it works as far as making it possible to play certain problematic tracks but I believe there's a problem in the logging because instead of creating the specified log-file it just creates a file named LOG_PATH in /opt/sinusbot ^^