Welcome to our forums! Please take a few moments to read through our Community Guidelines (also conveniently linked in the header at the top of each page). There, you'll find guidelines on conduct, tips on getting the help you may be searching for, and more!

Issue with slash symbol in @route declaration

CigarasCigaras Members Posts: 122 ✭✭
edited March 7 in Channel Development

I have an issue with dynamic @route declaration:

@handler('test')
def MainMenu():
    oc = ObjectContainer()
    group = Prefs['group'] # for example aaa/bbb
    oc.add(DirectoryObject(key = Callback(ListItems, group = unicode(group)), title = group))
    return oc

@route('test/listitems/{group}', page = int)
def ListItems(group, page = 1):
    items = Dict[group]
    for item in items:
        ....

I get an error stating that url test/listitems/aaa/bbb is not found, how can I escape it to something like test/listitems/aaa&#47bbb or similar? aaa/bbb value is being red from preferences, provided by user.

If I drop the {group} from @route I see this in log file:

DEBUG (runtime:717) - Handling request GET /video/iptv/listitems?group=aaa%2Fbbb

So Plex is able to change / to %2F in some situations, I want to force Plex to do so in @route, so I could get this:

DEBUG (runtime:717) - Handling request GET /video/iptv/listitems/aaa%2Fbbb

P. S. On one hand omitting the {group} might be a solution, but if group text has unicode symbols the "unicodeness" is lost when passing group as a parameter to a procedure and group is not a part of a @route, but this is a different question.

Best Answer

Answers

  • TwoureTwoure Members Posts: 574 ✭✭✭

    Maybe:

    # Encode with Plex's custom base64 Encode/Decode
    group = E(Prefs['group'])
    
    # then when you need it later
    items = Dict[D(group)]
    

    E() and D() are aliases for Encode() and Decode() respectively.

    Info taken from utilkit.py:

    def Encode(s):
        """
            Encodes the given string using the framework's standard encoding (a slight variant on
            Base64 which ensures that the string can be safely used as part of a URL).
        """
        ...
    
    def Decode(self, s):
        """
            Decodes a string previously encoded using the above function.
        """
        ...
    
  • CigarasCigaras Members Posts: 122 ✭✭
    edited March 7

    If I drop the {group} from @route I see this in log file:

    DEBUG (runtime:717) - Handling request GET /video/iptv/listitems?group=aaa%2Fbbb
    

    So Plex is able to change / to %2F in some situations, I want to force Plex to do so in @route, so I could get this:

    DEBUG (runtime:717) - Handling request GET /video/iptv/listitems/aaa%2Fbbb
    

    P. S. On one hand omitting the {group} might be a solution, but if group text has unicode symbols the "unicodeness" is lost when passing group as a parameter to a procedure and group is not a part of a @route, but this is a different question.

  • sander1sander1 Channel Developer/Admin Members, Plex Pass, Plex Ninja Posts: 3,590 Plex Ninja

    @Cigaras said:
    If I drop the {group} from @route I see this in log file:

    DEBUG (runtime:717) - Handling request GET /video/iptv/listitems?group=aaa%2Fbbb
    

    GET parameters are automatically encoded into a safe string. When you create a route, you have to do that yourself. The solution is mentioned by dane22 above.

  • dein0sdein0s Members Posts: 8 ✭✭

    Speaking of the latest public server (1.4.4.3495) with corresponding framework - String.Quote() from utilskit.py wouldn't help with this issue.
    Why? Because you can't pass any **kwargs to it and urllib.quote() by default does not encode / symbol as it's marked as safe (ref to https://docs.python.org/2/library/urllib.html#urllib.quote).
    But, you can import it directly and use like this:

    from urllib import quote
    some_unsafe_string = '/this/is/unsafe/string'
    safe_string = quote(some_unsafe_string, safe='')
    
Sign In or Register to comment.