def test_s2n():
    s1 = u'élà'
    s2 = b'\xc3\xa9l\xc3\xa0'
    if PY2:
        assert s2n(s1) == b'\xc3\xa9l\xc3\xa0'
        assert s2n(s1, validate=True) == b'\xc3\xa9l\xc3\xa0'
        assert isinstance(s2n(s1), binary_type)
        assert s2n(s2) == b'\xc3\xa9l\xc3\xa0'
        assert isinstance(s2n(s2), binary_type)
    else:
        assert s2n(s1) == u'élà'
        assert s2n(s1, validate=True) == u'élà'
        assert isinstance(s2n(s1), text_type)
        assert s2n(s2) == u'élà'
        assert isinstance(s2n(s2), text_type)
Beispiel #2
0
def _configure_server(restarting=False):
    global websocket_plugin

    # Configure server error log
    cherrypy.config.update({'log.error_file': 'cherrypy.error.log'})

    # Configure server url
    cherrypy.config.update({
        'server.socket_host':
        s2n(autosubliminal.WEBSERVERIP),
        'server.socket_port':
        int(autosubliminal.WEBSERVERPORT)
    })

    # Disable engine plugins (no need for autoreload plugin)
    cherrypy.config.update({'engine.autoreload.on': False})

    # Read and store cherrypy server version (if not set, it returns CherryPy/Unknown because it's not installed)
    server_header = 'CherryPy/%s' % get_library_version('cherrypy')
    cherrypy.config.update({'response.headers.server': server_header})

    # Configure authentication in if a username and password is set by the user
    if autosubliminal.USERNAME and autosubliminal.PASSWORD:
        users = {s2n(autosubliminal.USERNAME): s2n(autosubliminal.PASSWORD)}
        cherrypy.config.update({
            'tools.auth_digest.on':
            True,
            'tools.auth_digest.realm':
            'Auto-Subliminal website',
            'tools.auth_digest.get_ha1':
            auth_digest.get_ha1_dict_plain(users),
            'tools.auth_digest.key':
            'yek.tsegid_htua.lanimilbuS-otuA'  # Can be any random string
        })

    # Configure our custom json_out_handler (Uncomment if it should be used for any @cherrypy.tools.json_out())
    # cherrypy.config.update({'tools.json_out.handler': json_out_handler})

    if not restarting:
        # Enable websocket plugin
        websocket_plugin = WebSocketPlugin(cherrypy.engine)
        websocket_plugin.subscribe()
        cherrypy.tools.websocket = WebSocketTool()
    else:
        # When restarting we need to create a new websocket manager thread (you cannot start the same thread twice!)
        websocket_plugin.manager = WebSocketManager()
        # When restarting we need to clear the httpserver to force the creation of a new one (needed for ip/port change)
        cherrypy.server.httpserver = None
Beispiel #3
0
    def _search(self, title, year=None, language='en'):
        """ Search the api for a show.

        :param title: the title to search for
        :type title: str
        :param year: the year (optional)
        :type year: int or None
        :param language: the language to search for
        :type language: str
        :return: the search result or None if not found
        :rtype : tvdb_api_v2.models.series_search_result.SeriesSearchResult or None
        """
        name = title
        if year:
            name += ' (' + text_type(year) + ')'
        log.info('Searching tvdb api for %s', name)
        # Make sure to convert it to native string before searching (to prevent unicode encoding error)
        search_results = self._client.search_series_by_name(s2n(name), language=language)

        # Only return 1 result or None
        for search_result in search_results.data:
            if sanitize(search_result.series_name) == sanitize(name):
                return search_result
            elif search_result.aliases:
                # If no match, fallback to aliases (if aliases are available)
                for alias in search_result.aliases:
                    if sanitize(alias) == sanitize(name):
                        return search_result
            else:
                continue

        return None
Beispiel #4
0
    def when(self, matches, context):
        """Evaluate the rule.

        :param matches:
        :type matches: rebulk.match.Matches
        :param context:
        :type context: dict
        :return:
        """
        if not matches.named('type', lambda m: m.value == 'movie'):
            return

        to_remove = []
        to_append = []
        others = matches.named('other')
        if others:
            other = others[0]
            if other.initiator.value == 'XXX':
                next = matches.next(other, predicate=lambda match: match.name == 'title')
                if next:
                    # Prepend to title
                    title = next[0]
                    title.value = cleanup(other.initiator.raw + s2n(' ') + title.initiator.raw)
                    to_remove.extend(others)
                    to_append.append(title)

        return to_remove, to_append
Beispiel #5
0
    def when(self, matches, context):
        """Evaluate the rule.

        :param matches:
        :type matches: rebulk.match.Matches
        :param context:
        :type context: dict
        :return:
        """
        if not matches.named('type', lambda m: m.value == 'movie'):
            return

        to_remove = []
        to_append = []
        countries = matches.named('country')
        if countries:
            country = countries[0]
            if country.initiator.value == 'US':
                previous = matches.previous(country, predicate=lambda match: match.name == 'title')
                if previous:
                    # Append to title
                    title = previous[0]
                    title.value = cleanup(title.initiator.raw + s2n(' ') + country.initiator.raw)
                    to_remove.extend(countries)
                    to_append.append(title)

        return to_remove, to_append
Beispiel #6
0
    def when(self, matches, context):
        """Evaluate the rule.

        :param matches:
        :type matches: rebulk.match.Matches
        :param context:
        :type context: dict
        :return:
        """
        if not matches.named('type', lambda m: m.value == 'movie'):
            return

        to_remove = []
        to_append = []
        parts = matches.named('part')
        if parts:
            part = parts[0]
            previous = matches.previous(part, predicate=lambda match: match.name == 'title')
            if previous:
                # Append to title
                title = previous[0]
                title.value = cleanup(title.initiator.raw + s2n(' ') + part.initiator.raw)
                to_remove.extend(parts)
                to_append.append(title)

        return to_remove, to_append
Beispiel #7
0
    def _search(self, title, year=None, language='en'):
        """ Search the api for a show.

        :param title: the title to search for
        :type title: str
        :param year: the year (optional)
        :type year: int or None
        :param language: the language to search for
        :type language: str
        :return: the search result or None if not found
        :rtype : tvdb_api_v2.models.series_search_result.SeriesSearchResult or None
        """
        name = title
        if year:
            name += ' (' + text_type(year) + ')'
        log.info('Searching tvdb api for %s', name)
        # Make sure to convert it to native string before searching (to prevent unicode encoding error)
        search_results = self._client.search_series_by_name(s2n(name),
                                                            language=language)

        # Only return 1 result or None
        for search_result in search_results.data:
            if sanitize(search_result.series_name) == sanitize(name):
                return search_result
            elif search_result.aliases:
                # If no match, fallback to aliases (if aliases are available)
                for alias in search_result.aliases:
                    if sanitize(alias) == sanitize(name):
                        return search_result
            else:
                continue

        return None
Beispiel #8
0
def start_server(restarting=False):
    # stop server when restarting
    if restarting:
        # Stop server
        log.info('Stopping CherryPy webserver')
        cherrypy.engine.stop()

    # Configure server
    _configure_server(restarting)

    # (Re-)Mount application
    if restarting:
        # Remove previous mount (in case webroot should change)
        del cherrypy.tree.apps[list(cherrypy.tree.apps)[0]]
    cherrypy.tree.mount(WebServerRoot(),
                        s2n(autosubliminal.WEBROOT),
                        config=_get_application_configuration())

    # Start cherrypy server
    log.info('Starting CherryPy webserver')
    try:
        cherrypy.engine.start()
    except Exception:
        log.exception('Could not start webserver, exiting')
        _shutdown()
        _exit(1)
def fill_addic7ed_show_id_cache():
    """
    Fill the Addic7ed show_id cache used by the Addic7ed provider with our Addic7ed show name mappings.

    If the show name of a parsed file is not found on Addic7ed, no subtitles can be search on Addic7ed.
    That's why we need to force the id in the subliminal cache, in order to find the subtitles.
    Subtitles on Addic7ed can only be listed by their own internal Addic7ed id.

    Filling of the cache will make sure that this method of addic7ed.py always returns our mapping for a show_name.
    Since we are filling it before each run, the expiration_time will never be met and we'll always use mapping id.
    If there is no custom mapping, a search will be done on Addic7ed with the show_name.
    >>>@region.cache_on_arguments(expiration_time=SHOW_EXPIRATION_TIME)
    >>>def _search_show_id(self, series, year=None):
    """
    for x in autosubliminal.ADDIC7EDSHOWNAMEMAPPING:
        # Dogpile cache expects native strings as keys!
        cache_value = int(autosubliminal.ADDIC7EDSHOWNAMEMAPPING[x])
        cache_key = s2n(ADDIC7ED_SEARCH_SHOW_ID_CACHE_PREFIX + '|' + x)
        region.set(cache_key, cache_value)
        cache_key = s2n(CUSTOM_ADDIC7ED_SEARCH_SHOW_ID_CACHE_PREFIX + '|' + x)
        region.set(cache_key, cache_value)
def _configure_server(restarting=False):
    global websocket_plugin

    # Configure server error log
    cherrypy.config.update({'log.error_file': 'cherrypy.error.log'})

    # Configure server url
    cherrypy.config.update({'server.socket_host': s2n(autosubliminal.WEBSERVERIP),
                            'server.socket_port': int(autosubliminal.WEBSERVERPORT)
                            })

    # Disable engine plugins (no need for autoreload plugin)
    cherrypy.config.update({'engine.autoreload.on': False})

    # Read and store cherrypy server version (if not set, it returns CherryPy/Unknown because it's not installed)
    server_header = 'CherryPy/%s' % get_library_version('cherrypy')
    cherrypy.config.update({'response.headers.server': server_header})

    # Configure authentication in if a username and password is set by the user
    if autosubliminal.USERNAME and autosubliminal.PASSWORD:
        users = {s2n(autosubliminal.USERNAME): s2n(autosubliminal.PASSWORD)}
        cherrypy.config.update({'tools.auth_digest.on': True,
                                'tools.auth_digest.realm': 'Auto-Subliminal website',
                                'tools.auth_digest.get_ha1': auth_digest.get_ha1_dict_plain(users),
                                'tools.auth_digest.key': 'yek.tsegid_htua.lanimilbuS-otuA'  # Can be any random string
                                })

    # Configure our custom json_out_handler (Uncomment if it should be used for any @cherrypy.tools.json_out())
    # cherrypy.config.update({'tools.json_out.handler': json_out_handler})

    if not restarting:
        # Enable websocket plugin
        websocket_plugin = WebSocketPlugin(cherrypy.engine)
        websocket_plugin.subscribe()
        cherrypy.tools.websocket = WebSocketTool()
    else:
        # When restarting we need to create a new websocket manager thread (you cannot start the same thread twice!)
        websocket_plugin.manager = WebSocketManager()
        # When restarting we need to clear the httpserver to force the creation of a new one (needed for ip/port change)
        cherrypy.server.httpserver = None
def start_server(restarting=False):
    # stop server when restarting
    if restarting:
        # Stop server
        log.info('Stopping CherryPy webserver')
        cherrypy.engine.stop()

    # Configure server
    _configure_server(restarting)

    # (Re-)Mount application
    if restarting:
        # Remove previous mount (in case webroot should change)
        del cherrypy.tree.apps[list(cherrypy.tree.apps)[0]]
    cherrypy.tree.mount(WebServerRoot(), s2n(autosubliminal.WEBROOT), config=_get_application_configuration())

    # Start cherrypy server
    log.info('Starting CherryPy webserver')
    try:
        cherrypy.engine.start()
    except Exception:
        log.exception('Could not start webserver, exiting')
        _shutdown()
        _exit(1)
 def _convert_arg(self, arg):
     # Arguments should be sent in native strings (validate if the args can be sent in the specified encoding)
     return s2n(arg, encoding=self._encoding, validate=True)