Skip to content

Commit

Permalink
Add isort, fix chipers error
Browse files Browse the repository at this point in the history
  • Loading branch information
markowanga committed Feb 6, 2023
1 parent 1e0dc6e commit c49a89f
Show file tree
Hide file tree
Showing 21 changed files with 266 additions and 129 deletions.
267 changes: 184 additions & 83 deletions poetry.lock

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "stweet"
version = "2.1.0"
version = "2.1.1"
description = "Package to scrap tweets"
authors = ["Marcin Wątroba <[email protected]>"]
license = "MIT"
Expand All @@ -19,6 +19,9 @@ tor-python-easy = "^0.1.2"
[tool.poetry.dev-dependencies]
pytest = "^6.2.5"

[tool.poetry.group.dev.dependencies]
isort = "^5.12.0"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
16 changes: 10 additions & 6 deletions stweet/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from .get_user_runner import GetUsersTask, GetUsersRunner, GetUsersResult
from .http_request import WebClient, RequestsWebClient, RequestsWebClientProxyConfig
from .get_user_runner import GetUsersResult, GetUsersRunner, GetUsersTask
from .http_request import (RequestsWebClient, RequestsWebClientProxyConfig,
WebClient)
from .model import Language, UserTweetRaw
from .raw_output import PrintRawOutput, CollectorRawOutput, PrintEveryNRawOutput, \
PrintFirstInBatchRawOutput, JsonLineFileRawOutput
from .search_runner import SearchTweetsResult, TweetSearchRunner, SearchTweetsTask, RepliesFilter
from .tweets_by_ids_runner import TweetsByIdResult, TweetsByIdTask, TweetsByIdRunner
from .raw_output import (CollectorRawOutput, JsonLineFileRawOutput,
PrintEveryNRawOutput, PrintFirstInBatchRawOutput,
PrintRawOutput)
from .search_runner import (RepliesFilter, SearchTweetsResult,
SearchTweetsTask, TweetSearchRunner)
from .tweets_by_ids_runner import (TweetsByIdResult, TweetsByIdRunner,
TweetsByIdTask)
from .twitter_api import DefaultTwitterWebClientProvider
6 changes: 3 additions & 3 deletions stweet/auth/simple_auth_token_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
from json import JSONDecodeError
from typing import Callable, Optional

from .auth_token_provider import AuthTokenProvider
from .fail_strategy.auth_fail_strategy import AuthFailStrategy
from .fail_strategy.wait_auth_fail_strategy import WaitAuthFailStrategy
from ..exceptions import RefreshTokenException
from ..exceptions.too_many_requests_exception import TooManyRequestsException
from ..http_request import WebClient
from ..twitter_api.twitter_api_requests import TwitterApiRequests
from .auth_token_provider import AuthTokenProvider
from .fail_strategy.auth_fail_strategy import AuthFailStrategy
from .fail_strategy.wait_auth_fail_strategy import WaitAuthFailStrategy

_TIMEOUT = 20
_URL = 'https://api.twitter.com/1.1/guest/activate.json'
Expand Down
13 changes: 7 additions & 6 deletions stweet/get_user_runner/get_users_runner.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from dataclasses import dataclass
from typing import Optional, List
from typing import List, Optional

from .get_users_context import GetUsersContext
from .get_users_result import GetUsersResult
from .get_users_task import GetUsersTask
from .user_parser import parse_user
from ..http_request import WebClient
from ..model.user_raw import UserRaw
from ..raw_output.raw_data_output import RawDataOutput
from ..twitter_api.default_twitter_web_client_provider import DefaultTwitterWebClientProvider
from ..twitter_api.default_twitter_web_client_provider import \
DefaultTwitterWebClientProvider
from ..twitter_api.twitter_api_requests import TwitterApiRequests
from .get_users_context import GetUsersContext
from .get_users_result import GetUsersResult
from .get_users_task import GetUsersTask
from .user_parser import parse_user


@dataclass
Expand Down
3 changes: 2 additions & 1 deletion stweet/http_request/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .http_method import HttpMethod
from .request_details import RequestDetails
from .request_response import RequestResponse
from .requests.requests_web_client import RequestsWebClient, RequestsWebClientProxyConfig
from .requests.requests_web_client import (RequestsWebClient,
RequestsWebClientProxyConfig)
from .web_client import WebClient
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from http.client import HTTPConnection
from typing import List

from .. import RequestsWebClient, WebClient, RequestDetails, RequestResponse
from .. import RequestDetails, RequestResponse, RequestsWebClient, WebClient


class LoggingRequestsWebClientInterceptor(WebClient.WebClientInterceptor):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import threading
from typing import List

from .. import RequestsWebClient, WebClient, RequestDetails, RequestResponse
from .. import RequestDetails, RequestResponse, RequestsWebClient, WebClient


class ParamsResponseLogWebClientInterceptor(WebClient.WebClientInterceptor):
Expand Down
23 changes: 21 additions & 2 deletions stweet/http_request/requests/requests_web_client.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
"""Request search_runner class."""
from __future__ import annotations

from typing import Optional, Dict, List
from typing import Dict, List, Optional

import requests
import requests.adapters
import urllib3
import urllib3.util.ssl_

from .requests_web_client_proxy_config import RequestsWebClientProxyConfig
from ..request_details import RequestDetails
from ..request_response import RequestResponse
from ..web_client import WebClient
from .requests_web_client_proxy_config import RequestsWebClientProxyConfig

_CIPHERS = 'TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:ECDHE-ECDSA-AES128-' \
'GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:' \
'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-RSA-AES128-SHA' \
':ECDHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA:AES256-SHA'


class _TwitterTLSAdapter(requests.adapters.HTTPAdapter):
def init_poolmanager(self, *args, **kwargs):
# FIXME: When urllib3 2.0.0 is out and can be required,
# this should use urllib3.util.create_urllib3_context instead of the private, undocumented ssl_ module.
kwargs['ssl_context'] = urllib3.util.ssl_.create_urllib3_context(ciphers=_CIPHERS)
return super().init_poolmanager(*args, **kwargs)


class RequestsWebClient(WebClient):
Expand All @@ -34,6 +50,9 @@ def __init__(
def run_clear_request(self, params: RequestDetails) -> RequestResponse:
"""Main method to run request using requests package."""
session = requests.Session()
adapter = _TwitterTLSAdapter()
session.mount('https://twitter.com', adapter)
session.mount('https://api.twitter.com', adapter)
response = session.request(
method=params.http_method.name,
url=params.url,
Expand Down
2 changes: 1 addition & 1 deletion stweet/http_request/web_client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Web client abstract class."""
from __future__ import annotations

from abc import abstractmethod, ABC
from abc import ABC, abstractmethod
from typing import List, Optional

from .request_details import RequestDetails
Expand Down
2 changes: 1 addition & 1 deletion stweet/raw_output/collector_raw_output.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import List

from .raw_data_output import RawDataOutput
from ..model.raw_data import RawData
from .raw_data_output import RawDataOutput


class CollectorRawOutput(RawDataOutput):
Expand Down
2 changes: 1 addition & 1 deletion stweet/raw_output/json_line_file_raw_output.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import List

from .raw_data_output import RawDataOutput
from ..model.raw_data import RawData
from .raw_data_output import RawDataOutput


class JsonLineFileRawOutput(RawDataOutput):
Expand Down
2 changes: 1 addition & 1 deletion stweet/raw_output/print_every_n_raw_output.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import List

from .raw_data_output import RawDataOutput
from ..model.raw_data import RawData
from .raw_data_output import RawDataOutput


class PrintEveryNRawOutput(RawDataOutput):
Expand Down
2 changes: 1 addition & 1 deletion stweet/raw_output/print_first_in_batch_raw_output.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import List

from .raw_data_output import RawDataOutput
from ..model.raw_data import RawData
from .raw_data_output import RawDataOutput


class PrintFirstInBatchRawOutput(RawDataOutput):
Expand Down
2 changes: 1 addition & 1 deletion stweet/raw_output/print_raw_output.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import List

from .raw_data_output import RawDataOutput
from ..model.raw_data import RawData
from .raw_data_output import RawDataOutput


class PrintRawOutput(RawDataOutput):
Expand Down
11 changes: 6 additions & 5 deletions stweet/search_runner/search_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
import json
from typing import List, Optional

from .search_run_context import SearchRunContext
from .search_tweets_result import SearchTweetsResult
from .search_tweets_task import SearchTweetsTask
from .tweet_raw_parser import parse_users, get_scroll_cursor, parse_tweets
from ..exceptions.scrap_batch_bad_response import ScrapBatchBadResponse
from ..http_request.request_details import RequestDetails
from ..http_request.web_client import WebClient
from ..model.tweet_raw import TweetRaw
from ..model.user_raw import UserRaw
from ..raw_output.raw_data_output import RawDataOutput
from ..twitter_api.default_twitter_web_client_provider import DefaultTwitterWebClientProvider
from ..twitter_api.default_twitter_web_client_provider import \
DefaultTwitterWebClientProvider
from ..twitter_api.twitter_api_requests import TwitterApiRequests
from .search_run_context import SearchRunContext
from .search_tweets_result import SearchTweetsResult
from .search_tweets_task import SearchTweetsTask
from .tweet_raw_parser import get_scroll_cursor, parse_tweets, parse_users


class TweetSearchRunner:
Expand Down
2 changes: 1 addition & 1 deletion stweet/search_runner/search_tweets_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

from arrow import Arrow

from .replies_filter import RepliesFilter
from ..model.language import Language
from .replies_filter import RepliesFilter


def _format_date(arrow_time: Arrow) -> str:
Expand Down
13 changes: 7 additions & 6 deletions stweet/tweets_by_ids_runner/tweets_by_id_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
import json
from typing import List, Optional

from .tweet_raw_parser import get_all_tweets_from_json
from .tweets_by_id_context import TweetsByIdContext
from .tweets_by_id_result import TweetsByIdResult
from .tweets_by_id_task import TweetsByIdTask
from ..exceptions import ScrapBatchBadResponse
from ..http_request import WebClient, RequestDetails, RequestResponse
from ..http_request import RequestDetails, RequestResponse, WebClient
from ..model import UserTweetRaw
from ..model.cursor import Cursor
from ..raw_output.raw_data_output import RawDataOutput
from ..twitter_api.default_twitter_web_client_provider import DefaultTwitterWebClientProvider
from ..twitter_api.default_twitter_web_client_provider import \
DefaultTwitterWebClientProvider
from ..twitter_api.twitter_api_requests import TwitterApiRequests
from .tweet_raw_parser import get_all_tweets_from_json
from .tweets_by_id_context import TweetsByIdContext
from .tweets_by_id_result import TweetsByIdResult
from .tweets_by_id_task import TweetsByIdTask

_NOT_FOUND_MESSAGE = '_Missing: No status found with that ID.'

Expand Down
3 changes: 2 additions & 1 deletion stweet/twitter_api/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .default_twitter_web_client_provider import DefaultTwitterWebClientProvider
from .default_twitter_web_client_provider import \
DefaultTwitterWebClientProvider
9 changes: 6 additions & 3 deletions stweet/twitter_api/default_twitter_web_client_provider.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""DefaultTwitterWebClientProvider class."""
from tor_python_easy.tor_control_port_client import TorControlPortClient

from .twitter_auth_web_client_interceptor import TwitterAuthWebClientInterceptor
from ..auth import SimpleAuthTokenProvider
from ..auth.fail_strategy.tor_ip_change_auth_fail_strategy import TorIpChangeAuthFailStrategy
from ..http_request import WebClient, RequestsWebClient, RequestsWebClientProxyConfig
from ..auth.fail_strategy.tor_ip_change_auth_fail_strategy import \
TorIpChangeAuthFailStrategy
from ..http_request import (RequestsWebClient, RequestsWebClientProxyConfig,
WebClient)
from .twitter_auth_web_client_interceptor import \
TwitterAuthWebClientInterceptor


class DefaultTwitterWebClientProvider:
Expand Down
8 changes: 5 additions & 3 deletions stweet/twitter_api/twitter_auth_web_client_interceptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
from threading import Lock
from typing import List, Optional

from stweet.auth import SimpleAuthTokenProvider, AuthTokenProvider
from stweet.exceptions.too_many_requests_exception import TooManyRequestsException
from stweet.http_request import RequestsWebClient, WebClient, RequestDetails, RequestResponse
from stweet.auth import AuthTokenProvider, SimpleAuthTokenProvider
from stweet.exceptions.too_many_requests_exception import \
TooManyRequestsException
from stweet.http_request import (RequestDetails, RequestResponse,
RequestsWebClient, WebClient)

_AUTH_TOKEN = 'Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4p' \
'uTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA'
Expand Down

0 comments on commit c49a89f

Please sign in to comment.