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.


    Simple file download.

    Pythonista
    file download
    6
    19
    17977
    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.
    • NickAtNight
      NickAtNight last edited by

      Ok, so if I want to keep the file name, I need something like this.

      filepath = 'https://raw.githubusercontent.com/grrrr/py/741ba0500bc49e8f6268f02d23e461649e8d457b/scripts/'
      filename = 'buffer.py'

      filein = filepath + filename
      fileout = filename

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

        You can use urllib.urlretrieve:

        import urllib
        urllib.urlretrieve("http://www.example.com/songs/mp3.mp3", "mp3.mp3")
        

        This will download the file and return a File object which points to the downloaded file on disk

        NickAtNight 1 Reply Last reply Reply Quote 1
        • NickAtNight
          NickAtNight last edited by

          And I can replace the hard code with a input()

          filein = input("Enter the URL address of file to get:")
          fileout = input("Enter the file name:")

          filein = filepath + filename
          fileout = filename

          1 Reply Last reply Reply Quote 0
          • NickAtNight
            NickAtNight @lukaskollmer last edited by

            Ok, I will have to try that. @lukaskollmer

            How about this:
            If I navigate to the file in Safari.
            I can run a script.
            Using APPEX will give me the URL

            import appex
            
            r = appex.get_url()
            
            print(r)
            

            Now I just need to put that url into the command.

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

              Latest version,

              • Uses APPEX.
                -- If running as a script, gets the file name from APPEX.
                -- So navigate to file and invoke script.
                -- No checking for the appropriate type of file.

              • If not run as a script
                -- Allow manual entry of entire file path.
                -- Alterbative is to use the dummy test url

              • I suppose I should make a 'download' directory and put the files in there, just below the root.

              import console
              import dialogs
              import appex
              import os.path
              import urllib.request, urllib

              test = appex.is_running_extension()

              if test == True:
              filein = appex.get_url()
              else:
              myrun = dialogs.alert('Manual entry?', '','Enter manual URL',"Dummy")
              if myrun == 1:
              filein = input("Enter the URL address of file to get:")

              else:
              	filein = "https://raw.githubusercontent.com/grrrr/py/741ba0500bc49e8f6268f02d23e461649e8d457b/scripts/buffer.py"
              

              fileparse = urllib.parse.urlparse(filein)
              filepath,filename = os.path.split(fileparse.path)

              fin = urllib.request.urlopen(filein)

              fout = open(filename,'w')

              fout.truncate()

              bytemyfile = fin.read()
              myfile = bytemyfile.decode("utf-8")
              fout.write(myfile)
              print (myfile)

              fin.close()
              fout.close()

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

                Revised version.

                Makes a Downliad file in Documents.
                Puts download file there

                import console
                import dialogs
                import appex
                import os.path
                import urllib.request, urllib

                #Make a downloads directory
                os.chdir(os.path.expanduser('~'))
                os.chdir(os.path.join(os.getcwd(),'Documents'))

                test = os.path.isdir('Downloads')
                print(test)

                if test == False:
                print('Downloads created')
                os.makedirs('Downloads')
                else:
                print('Downloads exists')

                print('Change dir to Downloads')

                os.chdir(os.path.join(os.getcwd(),'Downloads'))

                #Test if running as extenstion
                test = appex.is_running_extension()

                if test == True:
                filein = appex.get_url()
                else:
                myrun = dialogs.alert('Manual entry?', '','Enter manual URL',"Dummy")

                if myrun == 1:
                filein = input("Enter the URL address of file to get:")

                else:
                filein = "https://raw.githubusercontent.com/grrrr/py/741ba0500bc49e8f6268f02d23e461649e8d457b/scripts/buffer.py"

                fileparse = urllib.parse.urlparse(filein)
                filepath,filename = os.path.split(fileparse.path)

                fin = urllib.request.urlopen(filein)

                fout = open(filename,'w')

                fout.truncate()

                bytemyfile = fin.read()
                myfile = bytemyfile.decode("utf-8")
                fout.write(myfile)
                print (myfile)

                fin.close()
                fout.close()

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

                  Well, that works modestly well. It downloaded both sample files for this Google code jam problem.

                  link text

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

                    Well, not working on github.com

                    Getting coding error

                    Traceback (most recent call last):
                    File "/private/var/mobile/Containers/Shared/AppGroup/78B1F70D-7DE8-4840-87C8-F5C1D01E9EF5/Pythonista3/Documents/LPTHW/FileDownload.py", line 49, in <module>
                    fout.write(myfile)
                    UnicodeEncodeError: 'ascii' codec can't encode character '\xb7' in position 1367: ordinal not in range(128)

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

                      You are better off using requests than urllib. It is easier to work with, and it handles decoding of the charset for you (otherwise, you need to check what encoding the website uses, and decode based in that). Also, you are better off using 'wb' to open your file, and write the bytes directly, rather than trying to mess with encodings at all.

                      Here is a 2 liner to save a file:

                      with open(destpath,'wb') as file:
                      		file.write(requests.get(url).content)
                      

                      See https://gist.github.com/jsbain/fcb3f42932dde9b0ff6c122893d1b230 for how this is used in an app extension that can be run from safari, to save a file to pythonista.

                      By the way, to post code in the forums, please type three backticks before you paste, on a separate line, and theee backticks after:

                      ```
                      your code here
                      ```
                      

                      Backticks on ios are a pain, you have to long press the single quote, and select the option on the left. I like to create a keyboard shortcut in settings, which replaces ,,, with ``` for this purpose.

                      NickAtNight 1 Reply Last reply Reply Quote 1
                      • NickAtNight
                        NickAtNight last edited by

                        Backticks?

                        Ok, let's give it a try.

                        Here is a simple working version of your suggestion.

                        I like the 'wb' switch.

                        import requests
                        
                        print()
                        
                        destpath = 'ex24file.txt'
                        url = "https://raw.githubusercontent.com/grrrr/py/741ba0500bc49e8f6268f02d23e461649e8d457b/scripts/buffer.py"
                        
                        with open(destpath,'wb') as file:
                        	file.write(requests.get(url).content)
                        
                        1 Reply Last reply Reply Quote 1
                        • NickAtNight
                          NickAtNight @JonB last edited by

                          Well that works pretty good @JonB.

                          I just learned how to transfer up a GIST.

                          First test:
                          https://gist.github.com/NickAtNight500

                          How do we download a ZIP file?

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

                            Tagging this discussion for future reference. link text

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

                              Found this bit of code on github inside of a game made with Pythonista.

                              urllib.request.urlretrieve('https://drive.google.com/uc?export=download&id=0B3zm9_2zdxHnQUJweTVtMU1GdDQ', 'notsonice.m4a')

                              It downloads a file from their Google Drive account into the directory the script that runs this code is.

                              lachlantula 1 Reply Last reply Reply Quote 0
                              • lukaskollmer
                                lukaskollmer last edited by

                                @TutorialDoctor Isn't that basically what I already proposed?

                                1 Reply Last reply Reply Quote 2
                                • TutorialDoctor
                                  TutorialDoctor last edited by

                                  Eeek. Yup.

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

                                    So what is the difference between request and urllib.request?

                                    Same library, just different collections?

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

                                      http://stackoverflow.com/questions/2018026/what-are-the-differences-between-the-urllib-urllib2-and-requests-module

                                      1 Reply Last reply Reply Quote 0
                                      • lachlantula
                                        lachlantula @TutorialDoctor last edited by

                                        @TutorialDoctor Hahaha, looks like you found my game I made as a joke between me and my friend:P

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