Skip to content

Commit

Permalink
Add unit test for agdm.tv
Browse files Browse the repository at this point in the history
  • Loading branch information
rkwyu committed Apr 25, 2024
1 parent 2308c15 commit 9f11b2e
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 8 deletions.
16 changes: 8 additions & 8 deletions anime_dl/scrapper/agdm_tv_scrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def get_episodes(self, url: str) -> typing.List[Episode]:
for episode in episodes:
e, x_servers = self.parse_episode(episode.referer_url)
# check if video source available
if requests.get(e.video_url, headers=general.REQUEST["header"]).ok:
if self.test_connectivity(e.video_url):
episode = (
episode.set_series_name(e.series_name)
.set_season(e.season)
Expand All @@ -44,9 +44,7 @@ def get_episodes(self, url: str) -> typing.List[Episode]:
)
logger.info(f"try another server: {x_url}")
e, _ = self.parse_episode(x_url)
if requests.get(
e.video_url, headers=general.REQUEST["header"]
).ok:
if self.test_connectivity(e.video_url):
episode = (
episode.set_series_name(e.series_name)
.set_season(e.season)
Expand All @@ -65,7 +63,7 @@ def get_episodes(self, url: str) -> typing.List[Episode]:
episode_no = m.groups()[2]
episode, x_servers = self.parse_episode(url)
# check if video source available
if requests.get(episode.video_url, headers=general.REQUEST["header"]).ok:
if self.test_connectivity(episode.video_url):
return [episode]
# try other servers
else:
Expand All @@ -74,9 +72,7 @@ def get_episodes(self, url: str) -> typing.List[Episode]:
x_url = f"https://agdm.tv/play/{id}-{x_server}-{episode_no}.html"
logger.info(f"try another server: {x_url}")
episode, _ = self.parse_episode(x_url)
if requests.get(
episode.video_url, headers=general.REQUEST["header"]
).ok:
if self.test_connectivity(episode.video_url):
return [episode]
else:
logger.warning(f"video source fail: {x_url} ({episode.video_url})")
Expand Down Expand Up @@ -151,3 +147,7 @@ def parse_episode(self, url: str) -> tuple[Episode, typing.List[int]]:
except Exception as e:
logger.error(f"{url}: {e}")
return Episode()

def test_connectivity(url):
headers = general.REQUEST["header"]
return requests.get(url, headers=headers).ok
88 changes: 88 additions & 0 deletions tests/scrapper/test_agdm_tv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import pytest

from unittest.mock import Mock, patch
from anime_dl.object.episode import Episode
from anime_dl.scrapper.agdm_tv_creator import AgdmTvCreator
from anime_dl.scrapper.agdm_tv_scrapper import AgdmTvScrapper


@pytest.fixture()
def url():
return {
"episode": "https://agdm.tv/play/35480-1-1.html",
"series": "https://agdm.tv/vod/35480.html",
}


def test_creator():
creator = AgdmTvCreator()
scrapper = creator.factory_method()
assert isinstance(scrapper, AgdmTvScrapper)


@pytest.fixture()
def scrapper():
return AgdmTvScrapper()


@pytest.mark.scrapping
def test_get_episodes_empty(scrapper):
with pytest.raises(Exception) as e_info:
scrapper.get_episodes("")


@pytest.mark.scrapping
def test_get_episodes_episode(scrapper, url):
url = url["episode"]
# given
scrapper.parse_episode = Mock(
return_value=(
(
Episode()
.set_series_name("SN")
.set_season("na")
.set_episode_name("EN")
.set_episode_no("EN")
.set_video_url("VU")
.set_referer_url(url)
.set_image_src("na")
), [],
)
)
scrapper.test_connectivity = Mock(return_value=True)
# test
res = scrapper.get_episodes(url)
assert len(res) == 1 and res[0].series_name == "SN" and res[0].video_url == "VU"


@pytest.mark.scrapping
def test_get_episodes_series(scrapper, url):
url = url["series"]
# given
scrapper.parse_episode = Mock(
return_value=(
(
Episode()
.set_series_name("SN")
.set_season("na")
.set_episode_name("EN")
.set_episode_no("EN")
.set_video_url("VU")
.set_referer_url(url)
.set_image_src("na")
), [],
)
)
scrapper.parse_series = Mock(
return_value=[
(
Episode()
.set_referer_url(url)
.set_image_src("IS")
)
]
)
scrapper.test_connectivity = Mock(return_value=True)
# test
res = scrapper.get_episodes(url)
assert len(res) == 1 and res[0].video_url == "VU"

0 comments on commit 9f11b2e

Please sign in to comment.