python Github文件查看器/ Pythonista的一般URL下载脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Github文件查看器/ Pythonista的一般URL下载脚本相关的知识,希望对你有一定的参考价值。

"""Prompt user for URL and filename and download the remote resource to a file.

If the clipboard contains a HTTP(S) or FTP(S) URL, the URL input dialog is
pre-filled with it.

The suggested local filename is extracted from the URL, if possible.

If a github file viewer URL is given, it is transformed into the matching
raw file access URL, which makes it easier to download files on github you
are viewing in your browser. Just copy the URL to the clipboard, change to
Pythonista and run this script.

""" 
import os
import re
import tempfile
import urllib2
import urlparse

from os.path import basename, exists
from urllib import unquote

import console
import editor

CHUNKSIZE = 4096

def download(args):
    if args:
        url = args[0]
    else:
        try:
            import clipboard
            cb = clipboard.get().strip()
            if not re.search('^(ht|f)tps?://', cb):
                cb = ''
            url = console.input_alert('Enter URL', 'Download file from URL:',
                cb, 'Download')
        except KeyboardInterrupt:
            return

    # convert github file viewer URLs into raw file URLs
    urlparts = urlparse.urlparse(url)
    try:
        auth, server = urlparts.netloc.split('@')
    except:
        server = urlparts.netloc

    server = server.split(':')[0]
    if server == 'github.com' and '/blob' in urlparts.path:
        urlparts = list(urlparts)
        urlparts[1] = urlparts[1].replace('github.com', 'raw.githubusercontent.com')
        urlparts[2] = urlparts[2].replace('/blob', '')
        url = urlparse.urlunparse(urlparts)

    try:
        r = urllib2.urlopen(url)
    except urllib2.URLError as exc:
        print(exc)
        console.hud_alert("Download error (see console)", 'error')
        return

    content_type = r.info().get('content-type')
    urlparts = urlparse.urlparse(r.geturl())
    fn = basename(unquote(urlparts.path))

    while True:
        try:
            fn = console.input_alert('Enter filename', "Save download as:", fn)
            if exists(fn):
                choice = console.alert('Overwrite file?',
                    'File exists. Enter new filename, overwrite or cancel?',
                    'New filename', 'Overwrite')
                if choice == 2:
                    break
            else:
                break
        except KeyboardInterrupt:
            return

    try:
        # trigger error early in case there are permission problems 
        if not exists(fn):
            open(fn, 'wb').close()

        with tempfile.NamedTemporaryFile(delete=False) as temp:
            while True:
                chunk = r.read(CHUNKSIZE)
                if not chunk:
                    break
                temp.write(chunk)
    except Exception as exc:
        print(exc)
        console.hud_alert("Download error (see console)", 'error')
        try:
            os.unlink(temp.name)
        except:
            pass
        return        
    else:
        os.rename(temp.name, fn)
    
    if content_type.startswith('text/'):
        editor.open_file(fn)

if __name__ == '__main__':
    import sys
    download(sys.argv[1:])

以上是关于python Github文件查看器/ Pythonista的一般URL下载脚本的主要内容,如果未能解决你的问题,请参考以下文章

caffe pytho接口

一个使用three.js的网页DXF文件查看器dxf viewer

python生态工具

如何下载安装Python

python装饰器

第33天pytho学习装饰器:高级函数部分演示