-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ApifyHttpProxyMiddleware for managing proxies
Closes: apify/actor-templates#255
- Loading branch information
Showing
4 changed files
with
49 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
from .middlewares import ApifyRetryMiddleware | ||
from .pipelines import ActorDatasetPushPipeline | ||
from .scheduler import ApifyScheduler | ||
from .utils import get_running_event_loop_id, open_queue_with_custom_client, to_apify_request, to_scrapy_request |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .apify_proxy import ApifyHttpProxyMiddleware | ||
from .apify_retry import ApifyRetryMiddleware |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from __future__ import annotations | ||
|
||
import base64 | ||
from typing import TYPE_CHECKING | ||
from urllib.parse import unquote | ||
|
||
from scrapy.exceptions import NotConfigured | ||
from scrapy.utils.python import to_bytes | ||
|
||
from apify import Actor | ||
|
||
if TYPE_CHECKING: | ||
from scrapy import Request, Spider | ||
from scrapy.crawler import Crawler | ||
|
||
|
||
class ApifyHttpProxyMiddleware: | ||
def __init__(self: ApifyHttpProxyMiddleware, proxy_settings: dict, auth_encoding: str = 'latin-1') -> None: | ||
self.auth_encoding = auth_encoding | ||
self.proxy_settings = proxy_settings | ||
|
||
@classmethod | ||
def from_crawler(cls: type[ApifyHttpProxyMiddleware], crawler: Crawler) -> ApifyHttpProxyMiddleware: | ||
proxy_settings = crawler.settings.get('PROXY_SETTINGS') | ||
|
||
if not proxy_settings: | ||
Actor.log.warning('Field "PROXY_SETTINGS" is missing in the settings. ApifyHttpProxyMiddleware is not NotConfigured.') | ||
raise NotConfigured | ||
|
||
return cls(proxy_settings) | ||
|
||
def process_request(self: ApifyHttpProxyMiddleware, request: Request, spider: Spider) -> None: | ||
if self.proxy_settings: | ||
request.meta['proxy'] = self.proxy_settings['url'] | ||
creds = self._basic_auth_header() | ||
request.headers[b'Proxy-Authorization'] = b'Basic ' + creds | ||
|
||
Actor.log.info(f'ApifyHttpProxyMiddleware: request.meta={request.meta}') | ||
|
||
def _basic_auth_header(self: ApifyHttpProxyMiddleware) -> bytes: | ||
username = self.proxy_settings['username'] | ||
password = self.proxy_settings['password'] | ||
user_pass = to_bytes(f'{unquote(username)}:{unquote(password)}', encoding=self.auth_encoding) | ||
return base64.b64encode(user_pass) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters