Connect to a HTTPS Url from Glyphs App Python

I’m trying to update my Get Random Featured Wikipedia Article of the Day script to work with Python 3 in Glyphs 3 but I’m having problems where the Python installed can’t “Can’t connect to HTTPS URL because the SSL module is not available.”, does anyone know anything about this and how to make it work?

The solutions I find in general are about fixing or changing the system wide python (for example) – which I don’t really want to change from using Glyphs App’s python to using my other python installs. In my experience all my scripts and plugins break.

What python do you use?

One easy way to avoid the problem is to use cocoa functions to load the data.
This is a very simplistic way of doing it:

URL = NSURL.URLWithString_("https://some.com/file.txt")
fileString = NSString.stringWithContentsOfURL_(URL)
print(fileString)

This synchron so it blocks until it is finished downloading. So you need to make sure to run this on a background thread.

Here is a blog post that explains the proper way in much detail. You can implement that in python, too: Tutorial: How To Use iOS NSURLConnection By Example

1 Like

Thanks Georg! I won’t be able to try it until tomorrow but can this pull an atom xml feed? I’m using Glyph’s python – the only Glyphs one there.

It can pull anything. You need to parse the xml yourself after you downloaded it.

Thanks Georg, that worked! I’m having trouble now with the encoding, I think the content is UTF-8 but it’s getting displayed like such due to being interpreted as Mac OS Roman

(endash) becomes –

I can see documentation on setting the encoding here Apple Developer Documentation but can’t tell how to write it in Python?

I figured I can take the fileString and do
fileString = fileString.encode("mac-roman").decode("utf-8")
But this doesn’t seem like the best way to do it?

from Foundation import NSUTF8StringEncoding 
fileString = NSString.stringWithContentsOfURL_encoding_error_(URL, NSUTF8StringEncoding, None)
1 Like