Last active
May 13, 2022 21:59
-
-
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 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. | |
Written by: Dooley_labs <dooleylabs.com> | https://gist.github.com/elderlabs/51e8e25ca8e85228ffd5e2de7b9b748b | |
""" | |
import json | |
import requests | |
import subprocess | |
import sys | |
LOG = False | |
LOG_PATH = '/opt/sinusbot/ytdl_wrapper.log' | |
YTDL_PATH = '/usr/local/bin/yt-dlp' | |
def ytdlp_wrapper(): | |
args = '' | |
for x in sys.argv[1:]: | |
args += x + ' ' | |
if LOG: | |
log = open(LOG_PATH, 'a') | |
log.write(f'called with args: {args}\n') | |
log.close() | |
proc_args = [YTDL_PATH] + sys.argv[1:] | |
proc = subprocess.Popen(proc_args, stdout=subprocess.PIPE) | |
data, _ = proc.communicate() | |
data = str(data, "utf=8") | |
if not '--version' in sys.argv[1:]: | |
data = json.loads(data) | |
if '--no-call-home' in sys.argv[1:]: | |
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 ^^