Example #1
0
    def __init__(self, cfg_provider):
        plsDecoder = PlsPlaylistDecoder()
        m3uDecoder = M3uPlaylistDecoder()
        asxDecoder = AsxPlaylistDecoder()
        xspfDecoder = XspfPlaylistDecoder()
        asfDecoder = AsfPlaylistDecoder()
        ramDecoder = RamPlaylistDecoder()

        self.log = Logger()

        self.decoders = [
            plsDecoder, asxDecoder, asfDecoder, xspfDecoder, ramDecoder,
            m3uDecoder
        ]

        self.url_timeout = None

        try:
            self.url_timeout = cfg_provider.getConfigValue("url_timeout")
            if (self.url_timeout == None):
                self.log.warn("Couldn't find url_timeout configuration")
                self.url_timeout = 100
                cfg_provider.setConfigValue("url_timeout",
                                            str(self.url_timeout))
        except Exception as e:
            self.log.warn("Couldn't find url_timeout configuration")
            self.url_timeout = 100
            cfg_provider.setConfigValue("url_timeout", str(self.url_timeout))

        self.log.info('Using url timeout = %s' % str(self.url_timeout))
Example #2
0
class PlsPlaylistDecoder:
    def __init__(self):
        self.log = Logger()
        self.log.debug('PLS playlist decoder')

    def isStreamValid(self, contentType, firstBytes):
        if 'audio/x-scpls' in contentType or \
           'application/pls+xml' in contentType or \
           firstBytes.strip().lower().startswith(b'[playlist]'):
            self.log.info('Stream is readable by PLS Playlist Decoder')
            return True
        else:
            return False

    def extractPlaylist(self, url):

        self.log.info('Downloading playlist...')

        req = UrlRequest(url)
        req.add_header('User-Agent', USER_AGENT)
        f = urlUrlopen(req)
        str = f.read()
        f.close()

        self.log.info('Playlist downloaded')
        self.log.info('Decoding playlist...')

        playlist = []
        lines = str.splitlines()
        for line in lines:
            if line.startswith(b"File") == True:
                list = line.split(b"=", 1)
                playlist.append(list[1])

        return playlist
Example #3
0
    def __init__(self,
                 name,
                 description,
                 default_value,
                 template,
                 actual_value=None,
                 mode=None,
                 ui=None):
        # self.id = action_id
        self.name = name
        # self.type = type
        self.mode = mode  # subaction mode
        self.ui = ui
        self.description = description
        self.default_value = default_value
        if actual_value is not None:
            self.actual_value = actual_value
        self.template = template

        self.logger = Logger()
Example #4
0
class XspfPlaylistDecoder:
    def __init__(self):
        self.log = Logger()

    def isStreamValid(self, contentType, firstBytes):
        if 'application/xspf+xml' in contentType:
            self.log.info('Stream is readable by XSPF Playlist Decoder')
            return True
        else:
            return False

    def extractPlaylist(self, url):
        self.log.info('XSPF: downloading playlist...')
        req = UrlRequest(url)
        req.add_header('User-Agent', USER_AGENT)
        f = urlUrlopen(req)
        str = f.read()
        f.close()
        self.log.info('XSPF: playlist downloaded, decoding...')

        root = ET.parse(BytesIO(str))
        ns = {'xspf': 'http://xspf.org/ns/0/'}
        elements = root.findall(".//xspf:track/xspf:location", ns)

        result = []
        for r in elements:
            result.append(r.text)

        return result
Example #5
0
class AsfPlaylistDecoder:
    def __init__(self):
        self.log = Logger()

    def isStreamValid(self, contentType, firstBytes):
        if 'video/x-ms-asf' in contentType and \
           firstBytes.strip().lower().startswith('b[reference]'):
            self.log.info('Stream is readable by ASF Playlist Decoder')
            return True
        else:
            return False

    def extractPlaylist(self, url):
        self.log.info('ASF: downloading playlist..')
        req = UrlRequest(url)
        req.add_header('User-Agent', USER_AGENT)
        f = urlUrlopen(req)
        str = f.read()
        f.close()

        self.log.info('ASF: playlist downloaded, decoding...')

        playlist = []
        lines = str.splitlines()
        for line in lines:
            if line.startswith(b"Ref"):
                list = line.split(b"=", 1)
                tmp = list[1].strip()
                if tmp.endswith(b"?MSWMExt=.asf"):
                    playlist.append(tmp.replace(b"http", b"mms"))
                else:
                    playlist.append(tmp)

        return playlist
Example #6
0
class RamPlaylistDecoder:
    def __init__(self):
        self.log = Logger()


    def isStreamValid(self, contentType, firstBytes):
        if 'audio/x-pn-realaudio' in contentType or \
           'audio/vnd.rn-realaudio' in contentType:
            self.log.info('Stream is readable by RAM Playlist Decoder')
            return True
        else:
            return False


    def extractPlaylist(self,  url):
        self.log.info('RAM: Downloading playlist...')
        req = UrlRequest(url)
        req.add_header('User-Agent', USER_AGENT)
        f = urUrlopen(req)
        str = f.read()
        f.close()

        self.log.info('RAM Playlist downloaded, decoding...')

        lines = str.splitlines()
        playlist = []
        for line in lines:
            if len(line) > 0 and not line.startswith(b"#"):
                tmp = line.strip()
                if len(tmp) > 0:
                    playlist.append(line.strip())

        return playlist
Example #7
0
class M3uPlaylistDecoder:
    def __init__(self):
        self.log = Logger()


    def isStreamValid(self, contentType, firstBytes):
        if 'audio/mpegurl' in contentType or 'audio/x-mpegurl' in contentType:
            self.log.info('Stream is readable by M3U Playlist Decoder')
            return True
        else:
            lines = firstBytes.splitlines()
            for line in lines:
                if line.startswith(b"http://"):
                    return True
        return False


    def extractPlaylist(self,  url):
        self.log.info('M3u: downloading playlist...')
        req = UrlRequest(url)
        req.add_header('User-Agent', USER_AGENT)
        f = urlUrlopen(req)
        str = f.read()
        f.close()

        self.log.info('M3U: playlist downloaded, decoding... ')

        lines = str.splitlines()
        playlist = []

        for line in lines:
            if line.startswith(b"#") == False and len(line) > 0:
                playlist.append(line)

        return playlist
Example #8
0
    def __init__(self,
                 name,
                 description,
                 template,
                 actual_value=None,
                 mode=None,
                 ui=None,
                 evos_possible=False):
        self.name = name
        self.evos_possible = evos_possible
        self.mode = mode  # subaction mode : nCloth, nHair, Fume
        if ui is None:
            self.ui = ["None"]
        else:
            self.ui = ui
        self.description = description
        # old self.default_value = default_value
        if actual_value is not None:
            self.actual_value = actual_value
        self.template = template

        self.logger = Logger()
Example #9
0
class MultiAction:
    """
    Grouped actions class is container for ONE or more SingleAction objects
    this container grouping similar actions, few actions of one type  or different versions of actions
    """
    multi_id = None
    name = ""
    actions = []  # one or more SingleAction objects
    actions_count = 0
    logger = None

    def __init__(self, multi_id, name):
        self.multi_id = multi_id
        self.name = name
        self.actions = []
        self.logger = Logger()

    def __repr__(self):
        return "MultiAction({},{})".format(self.multi_id, self.name)

    def __str__(self):
        return "MultiAction  name:" + self.name

    def print_actions(self):
        self.logger.clear_buffer()
        self.logger.buffering_on()
        logger_raw = self.logger.raw
        logger_raw("\nname:  {}     total_actions:  {} ".format(
            self.name, self.actions_count))
        for i, ac in enumerate(self.actions):
            logger_raw("   action:  {}    desc: {} ".format(
                ac.name, ac.description))

        self.logger.buffering_off()
        return self.logger.get_buffer()

    def add_single_action(self, new_action):
        if isinstance(new_action, SingleAction):
            self.actions.append(new_action)
            self.actions_count += 1
            return True
        else:
            return False

    def get_action_index_by_mode(self, mode):
        for i, a in enumerate(self.actions):
            if a.mode == mode:
                return i
        return None
Example #10
0
class AsxPlaylistDecoder:
    def __init__(self):
        self.log = Logger()

    def isStreamValid(self, contentType, firstBytes):
        if ('audio/x-ms-wax' in contentType or \
            'video/x-ms-wvx' in contentType or \
            'video/x-ms-asf' in contentType or \
            'video/x-ms-wmv' in contentType) and \
            firstBytes.strip().lower().startswith(b'<asx'):
            self.log.info('Stream is readable by ASX Playlist Decoder')
            return True
        else:
            return False

    def extractPlaylist(self, url):
        self.log.info('ASX: Downloading playlist...')

        req = UrlRequest(url)
        req.add_header('User-Agent', USER_AGENT)
        f = urlUrlopen(req)
        str = f.read()
        f.close()

        self.log.info('ASX: playlist downloaded, decoding...')

        try:
            root = ET.parse(BytesIO(str))
        except:
            # Last ditch: try to fix docs with mismatched tag name case
            str = re.sub('''<([A-Za-z0-9/]+)''', \
                         lambda m: "<" + m.group(1).lower(),
                         str)
            root = ET.parse(BytesIO(str))

        #ugly hack to normalize the XML
        for element in root.iter():
            tmp = element.tag
            element.tag = tmp.lower()

            keys = element.attrib.keys()
            for key in keys:
                element.attrib[key.lower()] = element.attrib[key]

        elts = root.findall(".//ref/[@href]")

        result = []
        for elt in elts:
            tmp = elt.attrib['href']
            if (tmp.endswith("?MSWMExt=.asf")):
                tmp = tmp.replace("http", "mms")
            result.append(tmp)

        return result
Example #11
0
class StreamDecoder:
    def __init__(self, cfg_provider):
        plsDecoder = PlsPlaylistDecoder()
        m3uDecoder = M3uPlaylistDecoder()
        asxDecoder = AsxPlaylistDecoder()
        xspfDecoder = XspfPlaylistDecoder()
        asfDecoder = AsfPlaylistDecoder()
        ramDecoder = RamPlaylistDecoder()

        self.log = Logger()

        self.decoders = [
            plsDecoder, asxDecoder, asfDecoder, xspfDecoder, ramDecoder,
            m3uDecoder
        ]

        self.url_timeout = None

        try:
            self.url_timeout = cfg_provider.getConfigValue("url_timeout")
            if (self.url_timeout == None):
                self.log.warn("Couldn't find url_timeout configuration")
                self.url_timeout = 100
                cfg_provider.setConfigValue("url_timeout",
                                            str(self.url_timeout))
        except Exception as e:
            self.log.warn("Couldn't find url_timeout configuration")
            self.url_timeout = 100
            cfg_provider.setConfigValue("url_timeout", str(self.url_timeout))

        self.log.info('Using url timeout = %s' % str(self.url_timeout))

    def getMediaStreamInfo(self, url):
        if type(url) != type(u""):
            url = url.decode('utf-8')
        if url.startswith("http") == False:
            self.log.info('Not an HTTP url. Maybe direct stream...')
            return UrlInfo(url, False, None)

        self.log.info('Requesting stream... %s' % url)
        req = UrlRequest(url)
        req.add_header('User-Agent', USER_AGENT)

        try:
            opener = urlBuild_opener(
                DummyMMSHandler(),
                HTTPSHandler(context=my_ssl_create_unverified_context()))
            f = opener.open(req, timeout=float(self.url_timeout))
        except HTTPError as e:
            self.log.warn('HTTP Error for %s: %s' % (url, e))
            return None
        except URLError as e:
            self.log.info('URLError for %s: %s ' % (url, e))
            if str(e.reason).startswith('MMS REDIRECT'):
                newurl = e.reason.split("MMS REDIRECT:", 1)[1]
                self.log.info('Found mms redirect for: %s' % newurl)
                return UrlInfo(newurl, False, None)
            else:
                return None
        except BadStatusLine as e:
            if str(e).startswith('ICY 200'):
                self.log.info('Found ICY stream')
                return UrlInfo(url, False, None)
            else:
                return None
        except Exception as e:
            print('%s: for %s: %s' % (type(e), url, e), file=sys.stderr)
            self.log.warn('%s: for %s: %s' % (type(e), url, e))
            return None

        metadata = f.info()
        firstbytes = f.read(500)
        f.close()

        try:
            contentType = metadata["content-type"]
            self.log.info('Content-Type: %s' % contentType)
        except Exception as e:
            self.log.info("Couldn't read content-type. Maybe direct stream...")
            return UrlInfo(url, False, None)

        for decoder in self.decoders:

            self.log.info('Checking decoder')
            if decoder.isStreamValid(contentType, firstbytes):
                return UrlInfo(url, True, contentType, decoder)

        # no playlist decoder found. Maybe a direct stream
        self.log.info(
            'No playlist decoder could handle the stream. Maybe direct stream...'
        )
        return UrlInfo(url, False, contentType)

    def getPlaylist(self, urlInfo):
        return urlInfo.getDecoder().extractPlaylist(urlInfo.getUrl())
Example #12
0
class SingleAction:
    """ Single action with template"""
    name = ""
    mode = None
    default_value = ""  # pattern changed when add to queue
    actual_value = ""  # var set by user or default_value, finally used for generate action_script from template
    template = ""  # use template for create absolute ...
    parameters = None  # for nucleus engine it's  BND STR MAS
    description = ""
    json_FIELDS_NAMES = ACTION_DATA_FIELDS_NAMES
    ui = None

    user_value = ""
    logger = None  # TODO  @classmethod

    def __init__(self,
                 name,
                 description,
                 default_value,
                 template,
                 actual_value=None,
                 mode=None,
                 ui=None):
        # self.id = action_id
        self.name = name
        # self.type = type
        self.mode = mode  # subaction mode
        self.ui = ui
        self.description = description
        self.default_value = default_value
        if actual_value is not None:
            self.actual_value = actual_value
        self.template = template

        self.logger = Logger()

    def __repr__(self):
        return 'SingleAction("{}", "{}", "{}", "{}", actual_value="{}", ui="{}")'.format(
            self.name, self.description, self.default_value, self.template,
            self.actual_value, self.ui)

    def __str__(self):
        return "SingleAction"

    def print_minimum(self):
        print "   action: {}   actual_value: {}".format(
            self.name, self.actual_value)

    @staticmethod
    def unicode_arr_to_asci_str(arr):  # TODO std lib !
        ret = ""
        for a in arr:
            ret += a
        return ret

    def print_action(self):
        print "   action: {}   def_val: {}   actual_val: {}   templ: {}   mode: {}".format(
            self.name, self.default_value, self.actual_value, self.template,
            self.mode)

        self.logger.clear_buffer()
        self.logger.buffering_on()
        logger_raw = self.logger.raw
        logger_raw(
            "   action:          {}\n      default_value: {}\n      actual_value: {}\
                   \n      template:       {}".format(
                self.name, self.default_value, self.actual_value,
                self.unicode_arr_to_asci_str(self.template)))

        self.logger.buffering_off()
        return self.logger.get_buffer()

    def get_action(self):
        # TODO
        return self

    def get_action_as_string(self):
        # TODO
        return ""

    def complie_action(self, opions):
        # TODO
        return self.template
Example #13
0
 def __init__(self, multi_id, name):
     self.multi_id = multi_id
     self.name = name
     self.actions = []
     self.logger = Logger()
Example #14
0
class SingleAction:
    """ Single action with template"""
    name = ""
    evos_possible = False  # True for sim engines with evolutions possible BND, DMP, ...
    mode = None  # nCloth, nHair, Fume ... for Maya simulate ; BLAST, RENDER for prev; (defined in definitions)
    # default_value = ""  # template is a def val !!!
    actual_value = ""  # var set by user or default_value, finally used for generate action_script from template
    template = ""  # use template for create absolute script (defined in definitions)
    parameters = None  # for nucleus engine it's  BND STR MAS (defined in definitions)
    description = ""
    json_FIELDS_NAMES = ACTION_DATA_FIELDS_NAMES
    ui = None

    user_value = ""
    logger = None  # TODO  @classmethod

    def __init__(self,
                 name,
                 description,
                 template,
                 actual_value=None,
                 mode=None,
                 ui=None,
                 evos_possible=False):
        self.name = name
        self.evos_possible = evos_possible
        self.mode = mode  # subaction mode : nCloth, nHair, Fume
        if ui is None:
            self.ui = ["None"]
        else:
            self.ui = ui
        self.description = description
        # old self.default_value = default_value
        if actual_value is not None:
            self.actual_value = actual_value
        self.template = template

        self.logger = Logger()

    def __repr__(self):
        return 'SingleAction("{}", "{}", "{}", actual_value="{}", ui="{}")'.format(
            self.name, self.description, self.template, self.actual_value,
            self.ui)

    def __str__(self):
        return "SingleAction {} {} {} ".format(self.name, self.description,
                                               self.template)

    def print_minimum(self):
        print "   action: {}   actual_value: {}".format(
            self.name, self.actual_value)

    def set_evos_possible(self, bool_val):
        self.evos_possible = bool_val

    @staticmethod
    def unicode_arr_to_asci_str(arr):  # TODO std lib !
        ret = ""
        for a in arr:
            ret += a
        return ret

    def print_action(self):
        print "   action: {}   def_val: {}   actual_val: {}   templ: {}   mode: {}".format(
            self.name, self.ui[0], self.actual_value, self.template, self.mode)

        self.logger.clear_buffer()
        self.logger.buffering_on()
        logger_raw = self.logger.raw
        logger_raw(
            "   action:          {}\n      default_value: {}\n      actual_value: {}\
                   \n      template:       {}".format(
                self.name, self.ui[0], self.actual_value,
                self.unicode_arr_to_asci_str(self.template)))

        self.logger.buffering_off()
        return self.logger.get_buffer()

    """
    def get_action(self):
        # TODO
        return self

    def get_action_as_string(self):
        # TODO
        return ""
    """
    """ marker ATQ 235   generate script from temlpate   """

    def generate_script_from_action_template(self,
                                             batch,
                                             option,
                                             with_new_line=False,
                                             evo="",
                                             task_id=""):
        # TODO optimize + mixed var     <dir>\custom_file.bin
        template_with_values = copy.deepcopy(self.template)
        for i, twv in enumerate(template_with_values):
            if twv[0] == "<":
                if twv == "<ui>":
                    if len(option) > 0:
                        if "^" in option:
                            template_with_values[i] = option.split("^")[0]
                        else:
                            template_with_values[i] = option
                    else:
                        template_with_values[i] = "empty_option"
                else:
                    template_with_values[
                        i] = batch.sio.predefined.convert_predefined_variables_to_values(
                            twv, param=evo, option=task_id)
                template_with_values[i] = "\"" + template_with_values[i] + "\""
        scr = "".join(template_with_values)
        if with_new_line:
            scr += "\n"
        return scr
Example #15
0
 def __init__(self):
     self.log = Logger()
Example #16
0
#!/usr/bin/env python
#coding=utf-8
__author__ = 'yinjia'

import os, sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from config import settings
from dbhelper import dbapi
from lib.common import Logger

logger = Logger('user').getlog()
"""
服务端用户信息类
"""


class Users(object):
    def __init__(self, username):
        self.username = username
        self.password = ""
        self.totalspace = 0
        self.userspace = 0
        self.homepath = os.path.join(settings.USER_HOME_FOLDER, self.username)
        self.userpath = self.homepath

    def create_user(self):
        """
        创建用户
        :return: True:创建用户成功; False: 创建用户失败
        """
        args = dict(password=str(self.password),
Example #17
0
 def __init__(self):
     self.log = Logger()
     self.log.debug('PLS playlist decoder')