Skip to content

Commit

Permalink
Switch HTTPX support
Browse files Browse the repository at this point in the history
  • Loading branch information
Egsago-n committed Aug 11, 2024
1 parent 6929b30 commit e38985e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
requests>=2.28.1
httpx[brotli,socks]>=0.27.0
click>=8.0.4
ffmpeg-progress-yield>=0.7.8
17 changes: 9 additions & 8 deletions src/phub/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import logging
import random

import requests
import httpx
from typing import Iterable, Union
from functools import cached_property

Expand Down Expand Up @@ -94,12 +94,13 @@ def reset(self) -> None:
'''

# Initialise session
self.session = requests.Session()
self._clear_granted_token()
self.session = httpx.Client(
headers = consts.HEADERS,
cookies = consts.COOKIES,
follow_redirects = True
)

# Insert cookies & headers
self.session.headers = consts.HEADERS
self.session.cookies.update(consts.COOKIES)
self._clear_granted_token()

if self.bypass_geo_blocking:
ip = random.choice(consts.GEO_BYPASS_IPs)
Expand All @@ -120,7 +121,7 @@ def call(self,
headers: dict = None,
timeout: float = consts.CALL_TIMEOUT,
throw: bool = True,
silent: bool = False) -> requests.Response:
silent: bool = False) -> httpx.Response:
'''
Used internally to send a request or an API call.
Expand Down Expand Up @@ -167,7 +168,7 @@ def call(self,
url = url,
headers = headers,
data = data,
timeout = timeout,
timeout = timeout
)

# Silent 429 errors
Expand Down
10 changes: 2 additions & 8 deletions src/phub/modules/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import time
import logging
import requests.adapters
from pathlib import Path
from ffmpeg_progress_yield import FfmpegProgress
from typing import TYPE_CHECKING, Callable, Union
Expand Down Expand Up @@ -122,7 +121,7 @@ def _thread(client: Client, url: str, timeout: int) -> bytes:
'''
try:
response = client.call(url, timeout=timeout, silent=True)
response.raise_for_status() # Assuming client.call returns an object with a similar interface to requests.Response
response.raise_for_status()
return (url, response.content, True)

except Exception as e:
Expand All @@ -138,11 +137,7 @@ def _base_threaded(client: Client, segments: list[str], callback: CallbackType,
logging.info('Threaded download initiated')
buffer = {}
length = len(segments)
logger.info('Mounting download adapter')
old_adapter = client.session.adapters.get('https://')
adapter = requests.adapters.HTTPAdapter(pool_maxsize = max_workers)
client.session.mount('https://', adapter)


with Pool(max_workers=max_workers) as executor:
future_to_url = {executor.submit(_thread, client, url, timeout): url for url in segments}

Expand All @@ -157,7 +152,6 @@ def _base_threaded(client: Client, segments: list[str], callback: CallbackType,
except Exception as e:
logging.warning(f"Error processing segment {url}: {e}")

client.session.mount('https://', old_adapter)
return buffer

def threaded(max_workers: int = 20,
Expand Down

0 comments on commit e38985e

Please sign in to comment.