Last active
September 6, 2018 02:55
-
-
Save jmccardle/4abd8d4e7a8a126c0a5e7dfb26e17453 to your computer and use it in GitHub Desktop.
Use youtube-dl to turn a Youtube playlist into a set of Markdown embeddable image links.
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
import subprocess | |
def get_command_output(cmd): | |
"""Yield full lines of text from an external command.""" | |
#adapted from http://stackoverflow.com/questions/1388753/ddg#1388807 | |
process = subprocess.Popen( | |
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
line = bytes() | |
while True: | |
out = process.stdout.read(1) | |
if out == '' or process.poll() != None: | |
yield line.decode("utf-8") | |
raise StopIteration | |
line += out | |
if out == b'\n': | |
yield line.decode("utf-8").strip() | |
line = bytes() | |
def get_playlist_info(url): | |
"""Use youtube-dl to process sets of 3 lines into a Markdown link.""" | |
cmd = ["youtube-dl", "--get-id", "--get-title", "--get-thumbnail", url] | |
lines = [] | |
for line in get_command_output(cmd): | |
lines.append(line) | |
if len(lines) == 3: | |
yield markdown_img_link(*lines) | |
lines = [] | |
raise StopIteration | |
def markdown_img_link(title, video_id, img_url): | |
return '[![{0}]({1})](https://www.youtube.com/watch?v={2} "{0}")'.format(title, img_url, video_id) | |
if __name__ == '__main__': | |
my_playlist = "https://www.youtube.com/playlist?list=PLocvuO0AgFX5k-E1PovJjts6zJkOj853R" | |
for link in get_playlist_info(my_playlist): | |
print(link) | |
print("") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment