From 37c8b7ae455c435a919a621c4429bf5ad8060339 Mon Sep 17 00:00:00 2001 From: taizan-hokouto <55448286+taizan-hokuto@users.noreply.github.com> Date: Sun, 1 Nov 2020 21:58:41 +0900 Subject: [PATCH] Use client instead of direct httpx --- pytchat/processors/html_archiver.py | 6 +++--- pytchat/tool/videoinfo.py | 3 ++- tests/test_videoinfo.py | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/pytchat/processors/html_archiver.py b/pytchat/processors/html_archiver.py index 95ada14..e33d2fc 100644 --- a/pytchat/processors/html_archiver.py +++ b/pytchat/processors/html_archiver.py @@ -3,7 +3,6 @@ import os import re import time from base64 import standard_b64encode -from httpx import NetworkError, ReadTimeout from .chat_processor import ChatProcessor from .default.processor import DefaultProcessor from ..exceptions import UnknownConnectionError @@ -48,6 +47,7 @@ class HTMLArchiver(ChatProcessor): ''' def __init__(self, save_path, callback=None): super().__init__() + self.client = httpx.Client(http2=True) self.save_path = self._checkpath(save_path) self.processor = DefaultProcessor() self.emoji_table = {} # tuble for custom emojis. key: emoji_id, value: base64 encoded image binary. @@ -118,9 +118,9 @@ class HTMLArchiver(ChatProcessor): err = None for _ in range(5): try: - resp = httpx.get(url, timeout=30) + resp = self.client.get(url, timeout=30) break - except (NetworkError, ReadTimeout) as e: + except httpx.HTTPError as e: print("Network Error. retrying...") err = e time.sleep(3) diff --git a/pytchat/tool/videoinfo.py b/pytchat/tool/videoinfo.py index 8e789b6..19baa78 100644 --- a/pytchat/tool/videoinfo.py +++ b/pytchat/tool/videoinfo.py @@ -82,6 +82,7 @@ class VideoInfo: def __init__(self, video_id): self.video_id = extract_video_id(video_id) + self.client = httpx.Client(http2=True) err = None for _ in range(3): try: @@ -103,7 +104,7 @@ class VideoInfo: err = None for _ in range(3): try: - resp = httpx.get(url, headers=headers) + resp = self.client.get(url, headers=headers) resp.raise_for_status() break except httpx.HTTPError as e: diff --git a/tests/test_videoinfo.py b/tests/test_videoinfo.py index c2a8803..c60abbc 100644 --- a/tests/test_videoinfo.py +++ b/tests/test_videoinfo.py @@ -13,7 +13,7 @@ def _set_test_data(filepath, mocker): response_mock = mocker.Mock() response_mock.status_code = 200 response_mock.text = _text - mocker.patch('httpx.get').return_value = response_mock + mocker.patch('httpx.Client.get').return_value = response_mock def test_archived_page(mocker):