Skip to content

Commit

Permalink
BUGS-21491 Twitter bug workaround (#7)
Browse files Browse the repository at this point in the history
* Twitter bug workaround

* Twitter bug workaround
  • Loading branch information
jsadn authored Aug 4, 2023
1 parent 332c96e commit 22a0cea
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
29 changes: 26 additions & 3 deletions chirpy/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import re
import requests
import ssl

from urllib3.util.ssl_ import create_urllib3_context

from functools import partial

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -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',
Expand Down

0 comments on commit 22a0cea

Please sign in to comment.