<commit_before>'''
Torrent-tv.ru Playlist Downloader Plugin
http://ip:port/ttvplaylist
'''
import re
import logging
import urllib2
import time
from PluginInterface import AceProxyPlugin
import ttvplaylist_config


class Ttvplaylist(AceProxyPlugin):
    handlers = ('ttvplaylist', )

    logger = logging.getLogger('plugin_ttvplaylist')
    url = ttvplaylist_config.url
    host = ttvplaylist_config.host
    playlist = None
    playlisttime = None

    def downloadPlaylist(self):
        try:
            Ttvplaylist.logger.debug('Trying to download playlist')
            Ttvplaylist.playlist = urllib2.urlopen(
                Ttvplaylist.url, timeout=10).read()
            Ttvplaylist.playlisttime = int(time.time())
        except:
            Ttvplaylist.logger.error("Can't download playlist!")
            return False

        try:
            Ttvplaylist.playlist = re.sub(r',(\S.+) \((.+)\)', r' group-title="\2",\1', Ttvplaylist.playlist)
        except Exception as e:
            Ttvplaylist.logger.warning("Can't parse playlist groups! " + repr(e))

        try:
            # Add JTV
            Ttvplaylist.playlist = re.sub('#EXTM3U', r'#EXTM3U url-tvg="http://www.teleguide.info/download/new3/jtv.zip"',
                                          Ttvplaylist.playlist)
        except Exception as e:
            Ttvplaylist.logger.warning("Can't add JTV! " + repr(e))

        try:
            Ttvplaylist.playlist = re.sub(r',(\S.+)', lambda match: ' tvg-name="' + match.group(1).replace(' ', '_') + '",' \
                + match.group(1), Ttvplaylist.playlist)
        except Exception as e:
            Ttvplaylist.logger.warning("Can't add channel JTV name! " + repr(e))

        return True

    def handle(self, connection):
        if not Ttvplaylist.playlist or (int(time.time()) - Ttvplaylist.playlisttime > 60 * 60):
            if not self.downloadPlaylist():
                connection.dieWithError()
                return

        if Ttvplaylist.host:
            hostport = Ttvplaylist.host + ':' + str(connection.request.getsockname()[1])
        else:
            hostport = connection.request.getsockname()[0] + ':' + str(connection.request.getsockname()[1])

        try:
            if connection.splittedpath[2].lower() == 'ts':
                # Adding ts:// after http:// for some players
                hostport = 'ts://' + hostport
        except:
            pass

        connection.send_response(200)
        connection.send_header('Content-type', 'application/x-mpegurl')
        connection.end_headers()
        # For .acelive URLs
        playlist = re.sub('^(http.+)$', lambda match: 'http://' + hostport + '/torrent/' + \
            urllib2.quote(match.group(0), '') + '/stream.mp4', Ttvplaylist.playlist, flags=re.MULTILINE)
        # For PIDs
        playlist = re.sub('^([0-9a-f]{40})$', 'http://' + hostport + '/pid/\\1/stream.mp4', playlist, flags=re.MULTILINE)
        connection.wfile.write(playlist)
<commit_msg>Change the way the URL is formed in ttvplaylist_plugin taking the "Host" header into account.<commit_after>'''
Torrent-tv.ru Playlist Downloader Plugin
http://ip:port/ttvplaylist
'''
import re
import logging
import urllib2
import time
from PluginInterface import AceProxyPlugin
import ttvplaylist_config


class Ttvplaylist(AceProxyPlugin):
    handlers = ('ttvplaylist', )

    logger = logging.getLogger('plugin_ttvplaylist')
    url = ttvplaylist_config.url
    host = ttvplaylist_config.host
    playlist = None
    playlisttime = None

    def downloadPlaylist(self):
        try:
            Ttvplaylist.logger.debug('Trying to download playlist')
            Ttvplaylist.playlist = urllib2.urlopen(
                Ttvplaylist.url, timeout=10).read()
            Ttvplaylist.playlisttime = int(time.time())
        except:
            Ttvplaylist.logger.error("Can't download playlist!")
            return False

        try:
            Ttvplaylist.playlist = re.sub(r',(\S.+) \((.+)\)', r' group-title="\2",\1', Ttvplaylist.playlist)
        except Exception as e:
            Ttvplaylist.logger.warning("Can't parse playlist groups! " + repr(e))

        try:
            # Add JTV
            Ttvplaylist.playlist = re.sub('#EXTM3U', r'#EXTM3U url-tvg="http://www.teleguide.info/download/new3/jtv.zip"',
                                          Ttvplaylist.playlist)
        except Exception as e:
            Ttvplaylist.logger.warning("Can't add JTV! " + repr(e))

        try:
            Ttvplaylist.playlist = re.sub(r',(\S.+)', lambda match: ' tvg-name="' + match.group(1).replace(' ', '_') + '",' \
                + match.group(1), Ttvplaylist.playlist)
        except Exception as e:
            Ttvplaylist.logger.warning("Can't add channel JTV name! " + repr(e))

        return True

    def handle(self, connection):
        if not Ttvplaylist.playlist or (int(time.time()) - Ttvplaylist.playlisttime > 60 * 60):
            if not self.downloadPlaylist():
                connection.dieWithError()
                return
            
        hostport = connection.headers['Host']

        try:
            if connection.splittedpath[2].lower() == 'ts':
                # Adding ts:// after http:// for some players
                hostport = 'ts://' + hostport
        except:
            pass

        connection.send_response(200)
        connection.send_header('Content-type', 'application/x-mpegurl')
        connection.end_headers()
        # For .acelive URLs
        playlist = re.sub('^(http.+)$', lambda match: 'http://' + hostport + '/torrent/' + \
            urllib2.quote(match.group(0), '') + '/stream.mp4', Ttvplaylist.playlist, flags=re.MULTILINE)
        # For PIDs
        playlist = re.sub('^([0-9a-f]{40})$', 'http://' + hostport + '/pid/\\1/stream.mp4', playlist, flags=re.MULTILINE)
        connection.wfile.write(playlist)

{'fraction_non_alphanumeric': 0.1362859097127223, 'fraction_numerical': 0.01008891928864569, 'mean_word_length': 2.2332780541735766, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4866238', 'n_tokens_mistral': 1872, 'n_tokens_neox': 1782, 'n_words': 355}