Skip to content

Commit

Permalink
Refactorize UATimeoutSafeTransport into TimeoutSafeTransport (#1171)
Browse files Browse the repository at this point in the history
* Merge UATimeoutSafeTransport and TimeoutSafeTransport
  • Loading branch information
getzze authored Sep 24, 2024
1 parent 109b297 commit 85028e0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 18 deletions.
1 change: 1 addition & 0 deletions changelog.d/1171.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Refactorize UATimeoutSafeTransport into TimeoutSafeTransport.
10 changes: 9 additions & 1 deletion subliminal/providers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,17 @@ class TimeoutSafeTransport(SafeTransport):

timeout: float | None

def __init__(self, timeout: float | None, *args: Any, **kwargs: Any) -> None:
def __init__(
self,
*args: Any,
timeout: float | None = None,
user_agent: str | None = None,
**kwargs: Any,
) -> None:
super().__init__(*args, **kwargs)
self.timeout = timeout
if user_agent is not None:
self.user_agent = user_agent

def make_connection(self, host: Any) -> HTTPSConnection:
"""Make connection to host.
Expand Down
18 changes: 1 addition & 17 deletions subliminal/providers/opensubtitles.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,10 @@

if TYPE_CHECKING:
from collections.abc import Set
from http.client import HTTPConnection

logger = logging.getLogger(__name__)


class UATimeoutSafeTransport(TimeoutSafeTransport):
"""TimeoutSafeTransport with a user agent."""

def set_user_agent(self, user_agent: str) -> None:
"""Set user agent."""
self._user_agent = user_agent

def send_headers(self, connection: HTTPConnection, headers: list) -> None:
"""Send headers."""
if self._user_agent:
connection.putheader('User-Agent', self.user_agent)
super().send_headers(connection, headers)


class OpenSubtitlesSubtitle(Subtitle):
"""OpenSubtitles Subtitle."""

Expand Down Expand Up @@ -202,8 +187,7 @@ def __init__(
*,
timeout: int = 10,
) -> None:
transport = UATimeoutSafeTransport(timeout)
transport.set_user_agent('VLSub')
transport = TimeoutSafeTransport(timeout=timeout, user_agent='VLSub')
self.server = ServerProxy(self.server_url, transport)
if any((username, password)) and not all((username, password)):
msg = 'Username and password must be specified'
Expand Down

0 comments on commit 85028e0

Please sign in to comment.