<commit_before>from livestreamer.plugins import Plugin, PluginError, NoStreamsError
from livestreamer.stream import RTMPStream
from livestreamer.utils import swfverify, urlget
from livestreamer.compat import urllib, str
from livestreamer.options import Options

import xml.dom.minidom, re, sys, random

class JustinTV(Plugin):
    options = Options({
        "cookie": None
    })

    StreamInfoURL = "http://usher.justin.tv/find/{0}.xml?type=any&p={1}&b_id=true&chansub_guid={2}&private_code=null&group=&channel_subscription={2}"
    MetadataURL = "http://www.justin.tv/meta/{0}.xml?on_site=true"
    SWFURL = "http://www.justin.tv/widgets/live_embed_player.swf"

    @classmethod
    def can_handle_url(self, url):
        return ("justin.tv" in url) or ("twitch.tv" in url)

    def _get_channel_name(self, url):
        return url.rstrip("/").rpartition("/")[2]

    def _get_metadata(self, channel):
        cookie = self.options.get("cookie")

        if cookie:
            headers = {"Cookie": cookie}
            req = urllib.Request(self.MetadataURL.format(channel), headers=headers)
        else:
            req = urllib.Request(self.MetadataURL.format(channel))

        data = urlget(req)

        try:
            dom = xml.dom.minidom.parseString(data)
        except Exception as err:
            raise PluginError(("Unable to parse config XML: {0})").format(err))

        meta = dom.getElementsByTagName("meta")[0]
        metadata = {}

        metadata["title"] = self._get_node_if_exists(dom, "title")
        metadata["access_guid"] = self._get_node_if_exists(dom, "access_guid")
        metadata["login"] = self._get_node_if_exists(dom, "login")

        return metadata

    def _get_node_if_exists(self, dom, name):
        elements = dom.getElementsByTagName(name)
        if elements and len(elements) > 0:
            return self._get_node_text(elements[0])

    def _get_node_text(self, element):
        res = []
        for node in element.childNodes:
            if node.nodeType == node.TEXT_NODE:
                res.append(node.data)
        return "".join(res)

    def _get_streaminfo(self, channelname):
        def clean_tag(tag):
            if tag[0] == "_":
                return tag[1:]
            else:
                return tag


        chansub = None
        if self.options.get("cookie") is not None:
            self.logger.debug("Attempting to authenticate using cookie")

            metadata = self._get_metadata(channelname)
            chansub = metadata["access_guid"]

            if "login" in metadata:
                self.logger.debug("Successfully logged in as {0}", metadata["login"])


        randomp = int(random.random() * 999999)
        url = self.StreamInfoURL.format(channelname, randomp, chansub)

        self.logger.debug("Fetching stream info")
        data = urlget(url)

        # fix invalid xml
        data = re.sub(b"<(\d+)", b"<_\g<1>", data)
        data = re.sub(b"</(\d+)", b"</_\g<1>", data)

        streams = {}

        try:
            dom = xml.dom.minidom.parseString(data)
        except Exception as err:
            raise PluginError(("Unable to parse config XML: {0})").format(err))<commit_msg>livestreamer.plugins.justintv: Small tweak to login check.<commit_after>from livestreamer.plugins import Plugin, PluginError, NoStreamsError
from livestreamer.stream import RTMPStream
from livestreamer.utils import swfverify, urlget
from livestreamer.compat import urllib, str
from livestreamer.options import Options

import xml.dom.minidom, re, sys, random

class JustinTV(Plugin):
    options = Options({
        "cookie": None
    })

    StreamInfoURL = "http://usher.justin.tv/find/{0}.xml?type=any&p={1}&b_id=true&chansub_guid={2}&private_code=null&group=&channel_subscription={2}"
    MetadataURL = "http://www.justin.tv/meta/{0}.xml?on_site=true"
    SWFURL = "http://www.justin.tv/widgets/live_embed_player.swf"

    @classmethod
    def can_handle_url(self, url):
        return ("justin.tv" in url) or ("twitch.tv" in url)

    def _get_channel_name(self, url):
        return url.rstrip("/").rpartition("/")[2]

    def _get_metadata(self, channel):
        cookie = self.options.get("cookie")

        if cookie:
            headers = {"Cookie": cookie}
            req = urllib.Request(self.MetadataURL.format(channel), headers=headers)
        else:
            req = urllib.Request(self.MetadataURL.format(channel))

        data = urlget(req)

        try:
            dom = xml.dom.minidom.parseString(data)
        except Exception as err:
            raise PluginError(("Unable to parse config XML: {0})").format(err))

        meta = dom.getElementsByTagName("meta")[0]
        metadata = {}

        metadata["title"] = self._get_node_if_exists(dom, "title")
        metadata["access_guid"] = self._get_node_if_exists(dom, "access_guid")
        metadata["login"] = self._get_node_if_exists(dom, "login")

        return metadata

    def _get_node_if_exists(self, dom, name):
        elements = dom.getElementsByTagName(name)
        if elements and len(elements) > 0:
            return self._get_node_text(elements[0])

    def _get_node_text(self, element):
        res = []
        for node in element.childNodes:
            if node.nodeType == node.TEXT_NODE:
                res.append(node.data)

        if len(res) == 0:
            return None
        else:
            return "".join(res)

    def _get_streaminfo(self, channelname):
        def clean_tag(tag):
            if tag[0] == "_":
                return tag[1:]
            else:
                return tag


        chansub = None
        if self.options.get("cookie") is not None:
            self.logger.debug("Attempting to authenticate using cookie")

            metadata = self._get_metadata(channelname)
            chansub = metadata["access_guid"]

            if "login" in metadata and metadata["login"] is not None:
                self.logger.debug("Successfully logged in as {0}", metadata["login"])


        randomp = int(random.random() * 999999)
        url = self.StreamInfoURL.format(channelname, randomp, chansub)

        self.logger.debug("Fetching stream info")
        data = urlget(url)

        # fix invalid xml
        data = re.sub(b"<(\d+)", b"<_\g<1>", data)
        data = re.sub(b"</(\d+)", b"</_\g<1>", data)

        streams = {}

        try:
            dom = xml.dom.minidom.parseString(data)
        except Exception as err:
            raise PluginError(("Unable to parse config XML: {0})").format(err))
{'fraction_non_alphanumeric': 0.12790697674418605, 'fraction_numerical': 0.0068849449204406365, 'mean_word_length': 2.5032154340836015, 'pattern_counts': {'":': 6, '<': 15, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2475358', 'n_tokens_mistral': 2061, 'n_tokens_neox': 1977, 'n_words': 429}