commit b7dfa2c3979d14da42ff75a5106433a259ac0424 Author: Sean Corrigan Date: Sun Apr 3 23:22:26 2022 -0400 V0.1 Deploy to DEV diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c40b6cf --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.webm +__pycache__/main.cpython-310.pyc +__pycache__/test_main.cpython-310.pyc diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f66457d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +# set base image (host OS) +FROM python:3.9.10-slim-buster +# set the working directory in the container + +WORKDIR /app +# copy the dependencies file to the working directory +COPY require.txt . +# install dependencies +RUN pip install -r require.txt +# copy the content of the local src directory to the working directory +COPY main.py . + +# command to run on container start +CMD [ "python3", "main.py" ] \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..bd14d48 --- /dev/null +++ b/main.py @@ -0,0 +1,60 @@ +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() + \ No newline at end of file diff --git a/require.txt b/require.txt new file mode 100644 index 0000000..398bdf7 --- /dev/null +++ b/require.txt @@ -0,0 +1,3 @@ +yt-dlp +requests +numpy \ No newline at end of file diff --git a/test_main.py b/test_main.py new file mode 100644 index 0000000..db53674 --- /dev/null +++ b/test_main.py @@ -0,0 +1,13 @@ +import unittest +import main + + +class youtubeAPIunitTest(unittest.TestCase): + def test_listVideosType(self): + self.assertEqual(type(main.getPlaylistVideoIDs()), list) + + def test_listVideosLen(self): + self.assertGreater(len(main.getPlaylistVideoIDs()), 0) + + def test_scanVideoLibrary(self): + self.assertGreater(len(main.getVideosOnDisk()), 0) \ No newline at end of file