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.


    Gist file retrieval

    Pythonista
    6
    13
    9110
    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.
    • mrcoxall
      mrcoxall last edited by

      Do you have your script posted somewhere?
      Be interested in seeing how it works.

      Thanks

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

        i updated my savefile script to handle gists, including multi file gists.
        Add to the appex shortcuts, then from safari simply Share-> Run Pythonista3 script->savedfile. For normal files, it will guess the name, and weite the file to your home folder. For gists it will create a gists/gis_id folder and download all files associated with the gist.

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

          https://gist.github.com/fcb3f42932dde9b0ff6c122893d1b230

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

            Ok, that is totally awesome.
            Works flawlessly and the .pyui file just opens up right into the GUI.

            One question.
            I have both versions of Pythonista on my iPad.
            The share sheet from Safari opens up Pythonista not Pythonista 3.
            Any idea how to change That?

            Thanks again.
            I am a much happier camper.
            😁

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

              @JonB this is a slick solution!

              @mrcoxall in the share sheet on the same row where you see Run Pythonista Script, scroll all the way to the right (maybe even under More...) you will find another Run Pythonista Script that has a 3 in the icon.

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

                Got the Pythonista 3 Sharesheet.
                Thanks, i just thought it was a repeat.

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

                  @JonB savefile is a great utility. There seems to be a bug and it gives error If the file contains unicode character. For example I am not able save this file.
                  https://gist.github.com/balachandrana/a0d9a42e1b66e054fe8b5e9a437c71b8

                  I have modified your file as follows to make it work (convert the "content" to bytes). May be you can make appropriate changes.

                              console.hud_alert('writing '+filename)
                              content=f['content']
                              with open(os.path.join(destpath,filename),'wb') as file:
                                  file.write(bytes(content, 'utf8'))
                      else:
                          console.hud_alert('could not download')
                  else:
                      destpath=urlfilename
                      with open(destpath,'wb') as file:
                          file.write(bytes(requests.get(url).content, 'utf8'))
                  
                  
                  idcrook 1 Reply Last reply Reply Quote 0
                  • idcrook
                    idcrook @abcabc last edited by

                    @abcabc @JonB I tested the suggested edit that @abcabc proposed, and it works as needed for gists that contain code points outside ASCII range.

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

                      Thanks for catching this -- I forgot that json was going to return a str..
                      I updated the gist. I actually opted for downloading the raw url, rather than using the content field -- in skimming the api, gists may sometimes be truncated, so redownloading the raw url takes care of both issues. (probably paranoid, but I am always wary of using json to capture truly binary data). Slightly wasteful, but gists are usually small enough.

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

                        I have been trying different versions of Gist and Github downloaders and came up with the following solution after combining several together. It works with both Gists and Github repos. It does work with Unicode and it runs nicely from the sharesheet with some prompts to tell you what it is doing.

                        URL: https://gist.github.com/Mr-Coxall/7fbf6dc0ec3d83525f1944812ccce46f

                        # Created by: jsbain
                        # Created on: Aug 2014
                        # URL : https://github.com/jsbain/GitHubGet/blob/master/GitHubGet.py
                        
                        # download an entire github repo.
                        # either copy the url to clipboard, and run script, or run following bookmarklet.  
                        # will unzip to repo-branch (so be careful if downloading same branch name from multiple users)
                        # 
                        ##   javascript:(function()%7Bif(document.location.href.indexOf('http')===0)document.location.href='pythonista://GitHubGet?action=run&argv='+document.location.href;%7D)();
                        
                        # Altered by: Mr. Coxall
                        # Altered on: Aug 2016
                        # Combined with other code, so that it will only work from sharesheet 
                        #     and gives the user different and better feedback
                        # Also places the files in Downloaded from Github directory
                        # This works not only with Github repos but also Gists.
                        
                        import requests
                        import appex, console, time, os
                        import urllib, zipfile, sys, functools, re, os, tempfile
                        #import urllib,zipfile,sys, clipboard, functools, re, os, tempfile
                        
                        def extract_git_id(git):
                            #print git
                            m = re.match((r'^http(s?)://([\w-]*\.)?github\.com/(?P<user>[\w-]+)/(?P<repo>[\w-]*)'
                                         '((/tree|/blob)/(?P<branch>[\w-]*))?'), git)
                        #    print m.groupdict()
                            return m
                            
                        def git_download_from_args(args):
                            if len(args) == 2:
                                url = args[1]
                            else:
                                url = clipboard.get()
                                #print(url)
                            git_download(url)
                            
                        def git_download_from_sharesheet():
                            if appex.is_running_extension():
                                #unquote=requests.utils.unquote
                                #urlparse=requests.utils.urlparse
                                url = appex.get_url()
                                #print(url)
                                git_download(url)
                            else:  # Error handling...
                                print('''=====
                        * In Safari browser, navigate to a GitHub repo or Gist of interest.
                        * Tap 'Open in...' icon in top right of Safai window.
                        * Tap 'Run Pythonista Script'.
                        * Pick this script and tap the run button.
                        * When you return to Pythonista the files should be in '~/Documents/Downloaded from Github/'.''')
                        
                        
                        def dlProgress(filename, count, blockSize, totalSize):
                            if count*blockSize > totalSize:
                                percent=100
                            else:
                                percent = max(min(int(count*blockSize*100/totalSize),100),0)
                            sys.stdout.write("\r" + filename + "...%d%%" % percent)
                            sys.stdout.flush()
                        
                        def git_download(url):
                            base = 'https://codeload.github.com'
                            archive = 'zip'
                            m = extract_git_id(url)
                            if m:
                                g = m.groupdict()
                                if not g['branch']:
                                    g['branch'] = 'master'
                        
                                u = '/'.join((base,g['user'],g['repo'],archive, g['branch']))
                                #print u
                                #console.hud_alert('Downloading Github repo ...' + u)
                                console.hud_alert('Starting, please wait.')
                                console.show_activity()
                                try:
                                    with tempfile.NamedTemporaryFile(mode='w+b',suffix='.zip') as f:
                                        console.hud_alert('Downloading the zip.')
                                        urllib.urlretrieve(u,f.name,reporthook=functools.partial(dlProgress,u))
                                        
                                        z = zipfile.ZipFile(f)
                                        githubpath = os.path.expanduser('~/Documents/Downloaded from Github/')
                                        if not os.path.exists(githubpath):
                                            os.mkdir(githubpath)
                                        z.extractall(path = githubpath)
                                        console.hud_alert('Extracting zip.')
                                        print z.namelist()
                                except:
                                    print('git url did not return zip file')
                                console.hud_alert('Files saved in "Downloaded from Github" directory.')
                                console.hud_alert('All done.')
                            else:
                                print('could not determine repo url from clipboard or argv')
                                
                        if __name__=='__main__':
                            #git_download_from_args(sys.argv)
                            git_download_from_sharesheet()
                        
                        Phuket2 1 Reply Last reply Reply Quote 0
                        • Phuket2
                          Phuket2 @mrcoxall last edited by

                          @mrcoxall , I just had a look to see if you were in on this discussion-
                          https://forum.omz-software.com/topic/2382/git-or-gist-workflow-for-pythonista/35
                          Maybe you seen it. Not sure. But @ccc approach is pretty nice. Granted you need another app (working-copy) and it's a few steps.

                          Not a one click solution. But It can be 2 way solution (I tested it). Also via working-copy you have a real link back to the source. But the solution is pretty fast and memorable for repeating over and over. I just mention it because it's helped me tremendously. It takes me around 20-30 seconds to the the gist/repo into working-copy then using @ccc apex script to move it into Pythonista.

                          Anyway, just a thought. Personally, I prefer the structure over the speed.

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