Skip to content

Instantly share code, notes, and snippets.

@flyingrub
Last active September 3, 2021 18:28
Show Gist options
  • Save flyingrub/e386bba134d5a4cbd7f22b9559fcda1f to your computer and use it in GitHub Desktop.
Save flyingrub/e386bba134d5a4cbd7f22b9559fcda1f to your computer and use it in GitHub Desktop.
y2mp3
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
"""y2mp3 download all playlist of an user
Usage:
y2mp3
y2mp3 <link>
y2mp3 -h | --help
Options:
-h --help Show this screen
"""
import logging
import os
import signal
import sys
import time
import warnings
import math
import shutil
import requests
import re
import tempfile
import subprocess
from docopt import docopt
logging.basicConfig(level=logging.DEBUG, format='%(message)s')
logging.getLogger('requests').setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.newline = print
arguments = None
path = ''
offset = 0
default_user_id = 'UC3EiVab4t89wJOCLtZCZh1A'
playlist_endpoint = 'https://invidio.xamh.de/api/v1/channels/playlists/{0}'
invalid_chars = '\/:*?|<>"'
def main():
"""
Main function, call parse_url
"""
signal.signal(signal.SIGINT, signal_handler)
global offset
global arguments
# Parse argument
arguments = docopt(__doc__, version='v0.0.1')
logger.debug(arguments)
logger.debug('Downloading to '+os.getcwd()+'...')
download_all_playlists(default_user_id)
def download_all_playlists(id):
url = playlist_endpoint.format(id)
logger.debug(url)
r = requests.get(url)
json = r.json()
playlists = json['playlists']
while json["continuation"] is not None:
r = requests.get(url, params={"continuation": json["continuation"]})
json = r.json()
playlists = playlists + json["playlists"]
logger.debug(len(playlists))
# logger.debug(kplaylists)
for playlist in playlists:
logger.debug(playlist['playlistId'])
dlUrl = 'https://www.youtube.com/playlist?list={0}'.format(playlist['playlistId'])
logger.debug(dlUrl)
download_playlist(dlUrl, playlist['title'])
def download_playlist(playlist_url, playlist_name):
"""
Downloads a playlist
"""
invalid_chars = '\/*:?|<>"'
playlist_name = ''.join(c for c in playlist_name if c not in invalid_chars)
if not os.path.exists(playlist_name):
os.makedirs(playlist_name)
os.chdir(playlist_name)
subprocess.Popen(
('youtube-dl -x --embed-thumbnail --audio-format mp3'
' --ignore-errors --add-metadata -o \"%(title)s.%(ext)s\"'
' --audio-quality 0 --download-archive playlist.txt {0}')
.format(playlist_url), shell=True
).wait()
os.chdir('..')
def signal_handler(signal, frame):
"""
Handle Keyboardinterrupt
"""
logger.newline()
logger.info('Good bye!')
sys.exit(0)
if __name__ == '__main__':
main()
#!/bin/bash
youtube-dl -x --embed-thumbnail --audio-format mp3 --ignore-errors --add-metadata -o "%(playlist)s/%(title)s.%(ext)s" --audio-quality 0 --download-archive "playlist.txt" https://www.youtube.com/c/RonanNello/playlists
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment