gists文章下载工具


=Start=

缘由:

日常使用需要

正文:

参考解答:
#!/usr/bin/env python
# coding=utf-8
# Date: 20170601

import sys, os, time, json
import errno
import argparse
import requests
reload(sys)
sys.setdefaultencoding('utf-8')

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

def make_sure_path_exists(path):
    try:
        os.makedirs(path)
    except OSError as exception:
        if exception.errno != errno.EEXIST:
            raise

cookie_dict = {}
def requests_get(url):
    try:
        if cookie_dict:
            r = requests.get(url, cookies=cookie_dict)
        else:
            r = requests.get(url)
    except requests.exceptions.RequestException as e:
        return ''
    return r.text


# return a list contains all the username's gists
def gists_list(username):
    result = []
    url_user = 'https://api.github.com/users/{0}/gists'.format(username)
    url_text = requests_get(url_user)
    user_gists_list = json.loads(url_text)
    if isinstance(user_gists_list, dict):
        return []
    for item in user_gists_list:
        # print item.get('id'), item.get('description', '')
        result.append({item.get('id'): item.get('description', '')})
    return result

# download the specify gists id into file
# PS: if file is too big(>10M), just print file info and don't download
def gists_download(id):
    url_gists = 'https://api.github.com/gists/{0}'.format(id)
    url_text = requests_get(url_gists)
    gists_info = json.loads(url_text)
    if gists_info.get('message', ''):
        print '{0}{1}{2}\tNot Found'.format(bcolors.FAIL, url_gists, bcolors.ENDC)
        return
    files = gists_info.get('files')
    print 'gists {0} has {1} files in total!'.format(id, len(files))
    for item in files:
        # print files[item].get('filename'), files[item].get('size'), files[item].get('raw_url')
        if int(files[item].get('size')) > 1024 * 1024 * 10:
            print files[item].get('filename'), files[item].get('size'), files[item].get('raw_url')
            continue
        if not os.path.exists(id):
            os.makedirs(id)
        with open('./{0}/{1}'.format(id, files[item].get('filename')), 'wb') as fp:
            fp.write(requests_get(files[item].get('raw_url')))
        print files[item].get('filename'), files[item].get('size'), files[item].get('raw_url')
    return

def main():
    parse = argparse.ArgumentParser()
    parse.add_argument('-i', '--id', required=True, type=str, help='Specify the gists id')
    parse.add_argument('-u', '--user', type=str, help='Specify the GitHub username')
    args = parse.parse_args()

    if len(sys.argv) <= 1:
        parse.print_help()
        sys.exit(0)

    if args.id:
        if args.user is not None:  #因为为args.user设置了「type=str」,所以「args.user的值为None/设定的字符串的值」
            for item in gists_list(args.user):
                if args.id in item:
                    print '{0}{1}{2}'.format(bcolors.OKGREEN, item, bcolors.ENDC)
                else:
                    print item
            gists_download(args.id)
        else:
            gists_download(args.id)

if __name__ == '__main__':
    time_start = time.time()
    try:
        main()
    except KeyboardInterrupt:
        print 'Killed by user'
        sys.exit(0)
    print "\nSpend {0} seconds.\n".format(time.time() - time_start)
参考链接:
更多参考链接:

=END=

, ,

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注