60 lines
1.5 KiB
Python
60 lines
1.5 KiB
Python
from typing import List
|
|
from xmlrpc.client import Boolean
|
|
from numpy import save
|
|
import requests, json, os
|
|
import yt_dlp
|
|
|
|
apikey = os.environ['apikey']
|
|
playlistId = os.environ['playlistid']
|
|
saveDirectory = '/videos'
|
|
|
|
|
|
def getPlaylistVideoIDs() -> List:
|
|
url = "https://www.googleapis.com/youtube/v3/playlistItems"
|
|
|
|
querystring = {
|
|
"key": apikey,
|
|
"playlistId": playlistId,
|
|
"maxResults": "50",
|
|
"part":"contentDetails",
|
|
}
|
|
|
|
payload = ""
|
|
response = requests.request("GET", url, data=payload, params=querystring)
|
|
|
|
videoIDs = []
|
|
|
|
for video in json.loads(response.text)['items']:
|
|
videoIDs.append(video['contentDetails']['videoId'])
|
|
|
|
return videoIDs
|
|
|
|
def getVideosOnDisk() -> List:
|
|
movies = os.listdir(saveDirectory)
|
|
moviesUpdated = []
|
|
for movie in movies:
|
|
moviesUpdated.append(movie.split("[")[1].split(']')[0])
|
|
|
|
return set(moviesUpdated)
|
|
|
|
def downloadVideo(videoId) -> Boolean:
|
|
ydl_opts = { 'outtmpl': f'{saveDirectory}/%(title)s[%(id)s].%(ext)s'}
|
|
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
|
ydl.download([f'https://www.youtube.com/watch?v={videoId}'])
|
|
return True
|
|
|
|
def main() -> None:
|
|
IDs = getPlaylistVideoIDs()
|
|
alreadyDL = getVideosOnDisk()
|
|
for ID in IDs:
|
|
print(f"Checking disk for {ID}: ", end="")
|
|
if ID not in alreadyDL:
|
|
print("Downloading...", end="")
|
|
downloadVideo(ID)
|
|
print("\n\n\n")
|
|
else:
|
|
print("Already in Library")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|