Skip to content

Commit

Permalink
plugins.afreecatv: Fix weird connection issue.
Browse files Browse the repository at this point in the history
Resolves #412.
Resolves #413.
  • Loading branch information
chrippa committed Jun 11, 2014
1 parent 6fe70da commit 5caad7d
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/livestreamer/plugins/afreecatv.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import re

from requests.adapters import HTTPAdapter

from livestreamer.plugin import Plugin
from livestreamer.plugin.api import http
from livestreamer.stream import HLSStream
Expand All @@ -16,6 +18,23 @@
_url_re = re.compile("http(s)?://(\w+\.)?afreeca.com/(?P<username>\w+)")


class RedirectAdapter(HTTPAdapter):
"""This adapter redirects connections on the socket level.
This is done to keep the Host header the same but replace the
actual server IP address used for the connection.
"""

def __init__(self, *args, **kwargs):
self.redirect_source = kwargs.pop("source")
self.redirect_target = kwargs.pop("target")
HTTPAdapter.__init__(self, *args, **kwargs)

def get_connection(self, url, proxies=None):
url = url.replace(self.redirect_source, self.redirect_target)
return HTTPAdapter.get_connection(self, url, proxies=None)


class AfreecaTV(Plugin):
@classmethod
def can_handle_url(self, url):
Expand Down Expand Up @@ -46,4 +65,9 @@ def _get_streams(self):
return HLSStream.parse_variant_playlist(self.session, playlist_url,
headers=HEADERS)

# Redirect 123.111.232.13 to 123.111.232.16
http.mount("http://123.111.232.13", RedirectAdapter(source="123.111.232.13",
target="123.111.232.16"))


__plugin__ = AfreecaTV

0 comments on commit 5caad7d

Please sign in to comment.