omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Download pdf on Safari and open in Pythonista script?

    Pythonista
    3
    5
    3025
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • cvp
      cvp last edited by

      I download a pdf in Safari, then, on this pdf, I "open in" a Pythonista script that uses appex and receives a "get_url" as "file://..........pdf".
      How can I read/copy this file locally in Pythonista dirs?

      1 Reply Last reply Reply Quote 0
      • dgelessus
        dgelessus last edited by

        To convert the file: URL to a normal path, you can simply remove the file:// prefix using path = url[len("file://"):]. (You can of course just write path = url[7:], but it's not very obvious what the 7 means.)

        Pythonista's "Script Library" is located at ~/Documents. ~ is the "home" directory, to convert that to a normal path use os.path.expanduser("~/Documents").

        So if you want to save the file into Pythonista, you can use something like this:

        import appex
        import os
        import shutil
        
        path = appex.get_url()[len("file://"):]
        name = os.path.basename(path)
        dest = os.path.join(os.path.expanduser("~/Documents"), name)
        shutil.copy(path, dest)
        

        This could be condensed into less lines, I've split it up a little for clarity.

        Though if you just want to work with the PDF file and don't need to keep it permanently, you can open the path normally:

        with open(path, "rb"):
            # do whatever you need to
        
        1 Reply Last reply Reply Quote 1
        • cvp
          cvp last edited by

          Resolved: just found "urlretrieve" in a post (jonB) of last week...

          1 Reply Last reply Reply Quote 0
          • omz
            omz last edited by

            @dgelessus That won't work correctly if the URL contains percent escapes. A better method would be to use urlparse and urllib.url2pathname, like this:

            from urlparse import urlparse
            from urllib import url2pathname
            
            p = urlparse(file_url)
            file_path = url2pathname(p.path)
            
            1 Reply Last reply Reply Quote 2
            • cvp
              cvp last edited by

              Tanks a lot for your both clarifications...

              1 Reply Last reply Reply Quote 0
              • First post
                Last post
              Powered by NodeBB Forums | Contributors