diff --git a/.github/workflows/run_tests.yaml b/.github/workflows/run_tests.yaml index 629cb5a..94e6b82 100644 --- a/.github/workflows/run_tests.yaml +++ b/.github/workflows/run_tests.yaml @@ -1,4 +1,7 @@ name: check +env: + INSTAGRAM_USERNAME: ${{ secrets.INSTAGRAM_USERNAME }} + INSTAGRAM_SESSION: ${{ secrets.INSTAGRAM_SESSION_JSON }} on: push: pull_request: diff --git a/Tests/video_system/instagram/should_be_0.mp4 b/Tests/video_system/instagram/should_be_0.mp4 new file mode 100644 index 0000000..0beca6a Binary files /dev/null and b/Tests/video_system/instagram/should_be_0.mp4 differ diff --git a/Tests/video_system/instagram/should_be_1.mp4 b/Tests/video_system/instagram/should_be_1.mp4 new file mode 100644 index 0000000..659bb20 Binary files /dev/null and b/Tests/video_system/instagram/should_be_1.mp4 differ diff --git a/Tests/video_system/instagram/should_be_2.mp4 b/Tests/video_system/instagram/should_be_2.mp4 new file mode 100644 index 0000000..c508ec6 Binary files /dev/null and b/Tests/video_system/instagram/should_be_2.mp4 differ diff --git a/Tests/video_system/instagram/should_be_3.mp4 b/Tests/video_system/instagram/should_be_3.mp4 new file mode 100644 index 0000000..43e33f3 Binary files /dev/null and b/Tests/video_system/instagram/should_be_3.mp4 differ diff --git a/Tests/video_system/instagram/should_be_4.mp4 b/Tests/video_system/instagram/should_be_4.mp4 new file mode 100644 index 0000000..d2e75f5 Binary files /dev/null and b/Tests/video_system/instagram/should_be_4.mp4 differ diff --git a/Tests/video_system/instagram/should_be_5.mp4 b/Tests/video_system/instagram/should_be_5.mp4 new file mode 100644 index 0000000..0f36763 Binary files /dev/null and b/Tests/video_system/instagram/should_be_5.mp4 differ diff --git a/Tests/video_system/instagram/should_be_6.mp4 b/Tests/video_system/instagram/should_be_6.mp4 new file mode 100644 index 0000000..b38c8f6 Binary files /dev/null and b/Tests/video_system/instagram/should_be_6.mp4 differ diff --git a/Tests/video_system/instagram/test_instagram.py b/Tests/video_system/instagram/test_instagram.py new file mode 100644 index 0000000..8789aa9 --- /dev/null +++ b/Tests/video_system/instagram/test_instagram.py @@ -0,0 +1,54 @@ +import logging +import os +import shutil + +import pytest + +from Tests.video_system.download_tester import DownloadTester +from src.instagram import InstagramDownloader + +DOWNLOAD_PATH = os.path.join("Tests", "video_system", "instagram", "downloads") + +# no params +TEST_REEL_1 = "https://www.instagram.com/reel/C2-vIqjsosW/" +#extra params +TEST_REEL_2 = "https://www.instagram.com/reel/C2-vIqjsosW/?igsh=eGFma20zbWY0bDJs" +#multiple videos +TEST_REEL_3 = "https://www.instagram.com/p/C9tjADzPwDz/?igsh=MnF5dzhjajhxZ3Rs" + +class TestInstagramDownloader(DownloadTester): + @pytest.fixture(autouse=True) + def remove_all_cache(self): + logging.basicConfig(level=logging.DEBUG) + os.makedirs(DOWNLOAD_PATH, exist_ok=True) + shutil.rmtree(DOWNLOAD_PATH, ignore_errors=True) + yield + # delete the downloads folder after the test. + assert os.path.exists(DOWNLOAD_PATH) + shutil.rmtree(DOWNLOAD_PATH, ignore_errors=True) + + def test_instagram_single_video_download(self): + videos = InstagramDownloader.download_video_from_link( + TEST_REEL_1, path=DOWNLOAD_PATH + ) + should_be_path = os.path.join("Tests", "video_system", "instagram", "should_be_0.mp4") + + assert videos, "couldn't download, probably due to graphql error (login wasn't successful)" + self.download_single_video_test(videos, should_be_path) + + def test_instagram_download_extra_params(self): + videos = InstagramDownloader.download_video_from_link( + TEST_REEL_2, path=DOWNLOAD_PATH + ) + should_be_path = os.path.join("Tests", "video_system", "instagram", "should_be_0.mp4") + + self.download_single_video_test(videos, should_be_path) + + def test_instagram_download_multiple_videos(self): + videos = InstagramDownloader.download_video_from_link( + TEST_REEL_3, path=DOWNLOAD_PATH + ) + + expected_files = [os.path.join("Tests", "video_system", "instagram", should_be) for should_be in [f"should_be_{i}.mp4" for i in range(1, 7)]] + + self.download_multiple_video_test(videos, expected_files) diff --git a/src/instagram.py b/src/instagram.py index 9045576..d04dff8 100644 --- a/src/instagram.py +++ b/src/instagram.py @@ -50,6 +50,15 @@ def _login() -> bool: downloader.load_session(username, session_data) return True + if (session_data:=os.getenv("INSTAGRAM_SESSION")) is not None: + # this function will only be used in github actions + # so we can assume that the session data is in the correct format + # and if not, it should crash anyways + import json # pylint: disable=import-outside-toplevel # this is only used in this branch + downloader.load_session(username, json.loads(session_data)) + return True + + password = os.getenv("INSTAGRAM_PASSWORD") if password is None: logging.error(