Skip to content

Commit

Permalink
add instagram tests
Browse files Browse the repository at this point in the history
also had to modify Instagram login, might cause issues to other branches we'll see...
  • Loading branch information
kytpbs committed Aug 20, 2024
1 parent 67fd3a1 commit 78a2d6c
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/run_tests.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
name: check
env:
INSTAGRAM_USERNAME: ${{ secrets.INSTAGRAM_USERNAME }}
INSTAGRAM_SESSION: ${{ secrets.INSTAGRAM_SESSION_JSON }}
on:
push:
pull_request:
Expand Down
Binary file added Tests/video_system/instagram/should_be_0.mp4
Binary file not shown.
Binary file added Tests/video_system/instagram/should_be_1.mp4
Binary file not shown.
Binary file added Tests/video_system/instagram/should_be_2.mp4
Binary file not shown.
Binary file added Tests/video_system/instagram/should_be_3.mp4
Binary file not shown.
Binary file added Tests/video_system/instagram/should_be_4.mp4
Binary file not shown.
Binary file added Tests/video_system/instagram/should_be_5.mp4
Binary file not shown.
Binary file added Tests/video_system/instagram/should_be_6.mp4
Binary file not shown.
54 changes: 54 additions & 0 deletions Tests/video_system/instagram/test_instagram.py
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 9 additions & 0 deletions src/instagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 78a2d6c

Please sign in to comment.