What encoding when using the "Add by file path" API?

Get help with all aspects of SABnzbd
Forum rules
Help us help you:
  • Are you using the latest stable version of SABnzbd? Downloads page.
  • Tell us what system you run SABnzbd on.
  • Adhere to the forum rules.
  • Do you experience problems during downloading?
    Check your connection in Status and Interface settings window.
    Use Test Server in Config > Servers.
    We will probably ask you to do a test using only basic settings.
  • Do you experience problems during repair or unpacking?
    Enable +Debug logging in the Status and Interface settings window and share the relevant parts of the log here using [ code ] sections.
Post Reply
Usenet
Jr. Member
Jr. Member
Posts: 87
Joined: February 12th, 2008, 6:04 pm

What encoding when using the "Add by file path" API?

Post by Usenet »

I have some issues when trying to use the "Add by file path" api.
Then documentation states:

Code: Select all

api?mode=addlocalfile&name=full/local/path/to/file.ext
Adding the path, "C:\home\users\Örn\the.nzb" in utf-8 and then urlencoded

Code: Select all

api?mode=addlocalfile&name=C%3A%5Chome%5Cusers%5C%C3%96rn%5Cthe.nzb

Fails with a "no file exists" error.
However if I use the urlencoded unicode character for Ö it works.

Code: Select all

api?mode=addlocalfile&name=C%3A%5Chome%5Cusers%5C%D6rn%5Cthe.nzb
.
Is this the intended behavior?
Any python snippet is appreciated :D
User avatar
shypike
Administrator
Administrator
Posts: 19774
Joined: January 18th, 2008, 12:49 pm

Re: What encoding when using the "Add by file path" API?

Post by shypike »

The calling program is supposed to add headers specifying the encoding.
What does your Python snippet look like?
Usenet
Jr. Member
Jr. Member
Posts: 87
Joined: February 12th, 2008, 6:04 pm

Re: What encoding when using the "Add by file path" API?

Post by Usenet »

shypike wrote:The calling program is supposed to add headers specifying the encoding.
Aha. I use this:

Code: Select all

def _sabResponse(self, url):
        try:
            req = urllib2.Request(url)
            response = urllib2.urlopen(req)
        except:
            responseMessage = "unable to load url: " + url
        else:
            log = response.read()
            response.close()
            if "ok" in log:
                responseMessage = 'ok'
            else:
                responseMessage = log
        return responseMessage
User avatar
shypike
Administrator
Administrator
Posts: 19774
Joined: January 18th, 2008, 12:49 pm

Re: What encoding when using the "Add by file path" API?

Post by shypike »

If you send UTF-8 to urllib2, it won't know that it is UTF-8 (it might consider it to be Latin-1).
When you send it Unicode, it will know and convert that to UTF-8 and send the proper headers along.
Usenet
Jr. Member
Jr. Member
Posts: 87
Joined: February 12th, 2008, 6:04 pm

Re: What encoding when using the "Add by file path" API?

Post by Usenet »

In this case I build the url by

Code: Select all

url = self.baseurl + "mode=addlocalfile&name=" + urllib.quote_plus(local_file_name.encode('utf-8'))
since urllib.qoute_plus doesnt like unicode characters.
How would I url encode the url before sending it to urllib2?

BTW, thanks for the help!
User avatar
shypike
Administrator
Administrator
Posts: 19774
Joined: January 18th, 2008, 12:49 pm

Re: What encoding when using the "Add by file path" API?

Post by shypike »

The problem is that quote_plus doesn't understand UTF-8 very well.
You should encode in Latin-1, this maps sufficiently to Unicode to work for this case.
At least when Latin-1 covers your needs.
When you get byte string values from system calls, assume that they are Latin-1.
Usenet
Jr. Member
Jr. Member
Posts: 87
Joined: February 12th, 2008, 6:04 pm

Re: What encoding when using the "Add by file path" API?

Post by Usenet »

Thanks! Something like you do in https://github.com/sabnzbd/sabnzbd/blob ... ing.py#L45 would work then..
User avatar
shypike
Administrator
Administrator
Posts: 19774
Joined: January 18th, 2008, 12:49 pm

Re: What encoding when using the "Add by file path" API?

Post by shypike »

Not exactly.
The Python function will only accept 8bit ASCII with the implicit
assumption that it's Latin-1.
Usenet
Jr. Member
Jr. Member
Posts: 87
Joined: February 12th, 2008, 6:04 pm

Re: What encoding when using the "Add by file path" API?

Post by Usenet »

Aha, thanks, *sigh* I really hate these different encodings. I'll dig further.
Usenet
Jr. Member
Jr. Member
Posts: 87
Joined: February 12th, 2008, 6:04 pm

Re: What encoding when using the "Add by file path" API?

Post by Usenet »

When reading e bit more it seems as if the standard for a GET request is somewhat vague. The encoding specified in the urllib2 request is only for the response.
Anyway, encoding to latin-1 does the trick.
Post Reply