#!/usr/bin/env python3 from youtube_api import YouTubeAPI import sys, os import youtube_dl # Read YouTube API Key. def getYouTubeAPIKey(): return open('youtube.key', 'r').read() def getUploadTime(info): return info[1] def getYear(info): return int(getUploadTime(info)[:4]) def getPrivacy(info): return info[6] def getId(info): return info[7] # Print out information about a video. def printVideoInfo(info): title, date, channel, channelId, description, tags, privacy = info print('Title: ' + title) print('Date: ' + date) print('Channel: {} ({})'.format(channel, channelId) ) print('Description: ' + description) print('Tags: ', tags) print('Privacy: ' + privacy) playlistId = sys.argv[1] # Create download directory downloadDir = 'playlists' playlistDir = downloadDir+'/'+playlistId if not os.path.exists(downloadDir): os.mkdir(downloadDir) if not os.path.exists(playlistDir): os.mkdir(playlistDir) api = YouTubeAPI(getYouTubeAPIKey()) videos = api.getPlaylistVideos(playlistId) videoInfo = [api.getVideoInfo(video['snippet']['resourceId']['videoId']) for video in videos] filteredVideos = [video for video in videoInfo if getPrivacy(video) == 'unlisted' and getYear(video) <= 2017] ydl_opts = { 'outtmpl': '{}/%(extractor)s-%(id)s-%(title)s.%(ext)s'.format(playlistDir) } for v in filteredVideos: print(getYear(v), getId(v)) with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.download(['https://www.youtube.com/watch?v='+getId(v)])