# Youtube downloader
# warning: use most recent version of youtube-dl, old versions give 404 error for subtitles

from __future__ import unicode_literals
import youtube_dl
import requests
import os

#directory for downloads - change
path = "/home/hans/youtube_writing_with_film/"
os.chdir(path)
#download list op pad - change here to use another list
sourcelist = 'http://pad.constantvzw.org/p/video-sources-links/export/txt'

#get download list from pad and split it in list of links to download
r = requests.get(sourcelist)
linklist = r.content.split()
#linklist = [linklist[1]] #shorter for testing

#downloading with youtube_dl

class MyLogger(object):
    def debug(self, msg):
        pass
    def warning(self, msg):
        pass

    def error(self, msg):
        print(msg)


def my_hook(d):
    if d['status'] == 'finished':
        print('Done downloading, now converting ...')

#options - for overview check https://github.com/rg3/youtube-dl/blob/master/youtube_dl/YoutubeDL.py#L121-L269
ydl_opts = {
#   'listsubtitles': 'True'
   'writesubtitles': 'True',
   'subtitlesformat': 'srt'
#   'allsubtitles': 'True'
#   'writeautomaticsub': 'True' #Write the automatically generated subtitles to a file
}

for link in linklist:
   with youtube_dl.YoutubeDL(ydl_opts) as ydl:
      ydl.download([link])