Skip to content

Commit

Permalink
Catch exception when trying to download private video and return 403. (
Browse files Browse the repository at this point in the history
  • Loading branch information
deepaerial committed Apr 21, 2024
1 parent 4af50aa commit 39ff976
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.4.13] - 2024-04-21
### Fixed
- Catch exception when trying to download private video and return 403.

## [1.4.13] - 2024-03-24
### Added
- Sorting downloads by date in descending order in Deta datasource.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ profile = "black"

[tool.poetry]
name = "ytdl-api"
version = "1.4.13"
version = "1.4.14"
description = "API for web-based youtube-dl client"
authors = ["Nazar Oleksiuk <nazarii.oleksiuk@gmail.com>"]
license = "MIT"
Expand Down
7 changes: 7 additions & 0 deletions tests/endpoints/test_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,10 @@ def test_get_preview_422(app_client: TestClient):
assert response.status_code == 422
json_response = response.json()
assert json_response["detail"][0]["msg"] == "Bad youtube video link provided."


def test_get_preview_private_video(app_client: TestClient):
response = app_client.get("/api/preview", params={"url": "https://www.youtube.com/watch?v=mCk1ChMlqt0"})
assert response.status_code == 403
json_response = response.json()
assert "is a private video" in json_response["detail"]
14 changes: 12 additions & 2 deletions ytdl_api/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from logging import Logger

from fastapi.requests import Request
from pytube.exceptions import AgeRestrictedError, RegexMatchError
from pytube.exceptions import AgeRestrictedError, RegexMatchError, VideoPrivate
from starlette.responses import JSONResponse
from starlette.status import HTTP_401_UNAUTHORIZED, HTTP_500_INTERNAL_SERVER_ERROR
from starlette.status import HTTP_401_UNAUTHORIZED, HTTP_403_FORBIDDEN, HTTP_500_INTERNAL_SERVER_ERROR


def make_internal_error(
Expand Down Expand Up @@ -60,11 +60,21 @@ async def on_pytube_regexmatch_error(logger: Logger, request: Request, exc: Rege
)


async def on_pytube_videoprivate_error(logger: Logger, request: Request, exc: VideoPrivate):
logger.exception(exc)
return make_internal_error(
"private-video",
exc.error_string,
HTTP_403_FORBIDDEN,
)


ERROR_HANDLERS = (
(RemoteDisconnected, on_remote_disconnected),
(socket.timeout, on_socket_timeout),
(RuntimeError, on_runtimeerror),
(AgeRestrictedError, on_pytube_agerestricted_error),
(RegexMatchError, on_pytube_regexmatch_error),
(VideoPrivate, on_pytube_videoprivate_error),
(Exception, on_default_exception_handler),
)

0 comments on commit 39ff976

Please sign in to comment.