Skip to content

Instantly share code, notes, and snippets.

@midchildan
Created February 13, 2017 11:54
Show Gist options
  • Save midchildan/6f246cc162ac27a4481f5d1a8c7d9560 to your computer and use it in GitHub Desktop.
Save midchildan/6f246cc162ac27a4481f5d1a8c7d9560 to your computer and use it in GitHub Desktop.
A Pythonista extension for downloading videos using youtube-dl.
import appex
import console
import os
import sys
import tempfile
import youtube_dl
def main():
url = None
if not appex.is_running_extension():
print('Running in Pythonista app, using test data...\n')
url = 'https://www.youtube.com/watch?v=BaW_jenozKcj'
if url is None:
url = appex.get_url()
if url is None:
url = appex.get_text()
if url is None:
sys.exit('No input URL found.')
with tempfile.TemporaryDirectory() as d:
dlpath = os.path.join(d, '%(title)s.%(ext)s')
download(url, outtmpl=dlpath)
for f in os.listdir(d):
fpath = os.path.join(d, f)
console.open_in(fpath)
def download(url, **opts):
with youtube_dl.YoutubeDL(opts) as ydl:
ydl.download([url])
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment