Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Twitch-Livestream] Use proxy settings for chat IRC connection #203

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions chat_downloader/sites/twitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,24 @@ class TwitchError(SiteError):

class TwitchChatIRC():

def __init__(self):
def __init__(self, proxy=None):

if proxy is not None:
# use pysocks when needed
import socks
from urllib.parse import urlparse
proxy_url = urlparse(proxy)
self.socket = socks.socksocket()
scheme = socks.HTTP
if proxy_url.scheme == "socks4" or proxy_url.scheme == "socks":
scheme = socks.SOCKS4
if proxy_url.scheme == "socks5":
scheme = socks.SOCKS5
self.socket.set_proxy(scheme, proxy_url.hostname, proxy_url.port)

# create new socket
self.socket = socket.socket()
if proxy is None:
self.socket = socket.socket()

# start connection
self.socket.connect(('irc.chat.twitch.tv', 6667))
Expand Down Expand Up @@ -94,6 +109,10 @@ class TwitchChatDownloader(BaseChatDownloader):

_NAME = 'twitch.tv'

def __init__(self, **kwargs):
super().__init__(**kwargs)
self.proxy = kwargs["proxy"]

_TESTS = [
# Live
{
Expand Down Expand Up @@ -1516,7 +1535,7 @@ def _get_chat_messages_by_stream_id(self, stream_id, params):
def create_connection():
for attempt_number in attempts(max_attempts):
try:
irc = TwitchChatIRC()
irc = TwitchChatIRC(self.proxy)
irc.set_timeout(message_receive_timeout)
irc.join_channel(stream_id)
return irc
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
'isodate',
'docstring-parser',
'colorlog',
'websocket-client'
'websocket-client',
'pysocks'
]

setup(
Expand Down