diff --git a/chirpy/api.py b/chirpy/api.py index 25d2f43..8c68df6 100644 --- a/chirpy/api.py +++ b/chirpy/api.py @@ -1,5 +1,8 @@ import re import requests +import ssl + +from urllib3.util.ssl_ import create_urllib3_context from functools import partial @@ -21,6 +24,21 @@ class _DEFAULT(object): pass +# fix for a sudden twitter outage on 8/4/2023 +class TwitterNoSSLSessionTicketExtension(requests.adapters.HTTPAdapter): + def __init__(self): + self.ssl_context = create_urllib3_context() + self.ssl_context.options &= ~ssl.OP_NO_TICKET + super().__init__() + + def init_poolmanager(self, *args, **kwargs): + kwargs["ssl_context"] = self.ssl_context + super().init_poolmanager(*args, **kwargs) + + def proxy_manager_for(self, *args, **kwargs): + kwargs["ssl_context"] = self.ssl_context + return super().proxy_manager_for(*args, **kwargs) + class TwitterError(Exception): """ Base Exception thrown by the Twitter object when there is a @@ -187,17 +205,22 @@ def __call__(self, **kwargs): headers = {'Accept-Encoding': 'gzip'} headers.update(self.headers) + adapter = TwitterNoSSLSessionTicketExtension() + req_session = requests.Session() + req_session.mount('https://api.twitter.com', adapter) + req_session.mount('http://api.twitter.com', adapter) + if json_body is not None: headers['Content-Type'] = 'application/json; charset=utf-8' if method == 'GET' or method == 'DELETE': - request = partial(requests.request, params=kwargs) + request = partial(req_session.request, params=kwargs) elif method == 'POST': post_data = json.dumps(json_body) if json_body is not None else kwargs - request = partial(requests.request, data=post_data) + request = partial(req_session.request, data=post_data) elif method == 'PUT': put_data = json.dumps(json_body) if json_body is not None else kwargs - request = partial(requests.request, params=kwargs, data=put_data) + request = partial(req_session.request, params=kwargs, data=put_data) resp = request(method, uriBase, headers=headers, timeout=_timeout, proxies=self.proxies, auth=self.auth) diff --git a/setup.py b/setup.py index 03910a6..d5d26f6 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ from setuptools import setup, find_packages import sys, os -version = '2.0.2' +version = '2.1.0' install_requires = [ 'requests>=1.2.3',