📜  从 Github 下载要点变得简单

📅  最后修改于: 2021-10-21 05:29:24             🧑  作者: Mango

GithubGist 是一个您可以创建私人或公共 gist 的地方,即私人或公开存储您的文件。让我们假设一个场景,您已经为您的项目编写了大量的要点,并且您想下载其中的一组要点。使用 GithubGist 执行此操作的唯一方法是打开每个单独的 gist 以通过 HTTP 或 SSH 下载 ZIP 或克隆。
这篇文章是关于使上述任务更简单。使用下面的命令,你甚至可以从其他 github 用户下载 gist,不包括私人用户,直到你知道他们的密码。

我们将为此提议使用请求包。这是一个用最少的代码发送 HTTP 请求的很棒的包。

安装

1.通过终端使用pip3从PyPI下载包
句法:

pip3 install requests

注意:要成为 root 用户,请运行以下命令:

sudo pip3 install requests

Python3 脚本
该脚本无法在在线 IDE 上运行,因此您可以单击此处查看其工作原理。

import requests
import os
  
def create_directory(dirname):
    #Creates a new directory if a directory with dirname does not exist
  
    try:
        os.stat(dirname)
    except:
        os.mkdir(dirname)
  
def show(obj):
    #Displays the items in the obj
  
    for i in range(len(obj)):
        print(str(i)+': '+str(obj[i]))
  
def auth():
    #Asks for the user details
  
    ask_auth = input("Do you want to download gists from your account
                               ? Type 'yes' or 'no': ")
    if(ask_auth=="yes"):
        user = input("Enter your username: ")
        password = input("Enter your password: ")
        request = requests.get('https://api.github.com/users/'+user+'/gists'
                                             , auth=(user, password))
    elif(ask_auth=="no"):
        user = input("Enter username: ")
        request = requests.get('https://api.github.com/users/'
                                                       +user+'/gists')
    return [ask_auth, user, request]
  
def load(request):
   #Loads the files and the gist urls
  
   output = request.text.split(",")
   gist_urls = []
   files = []
   for item in output:
       if "raw_url" in item:
           gist_urls.append(str(item[11:-1]))
       if "filename" in item:
           files.append(str(item.split(":")[1][2:-1]))
   return [gist_urls, files]
  
def write_gist(filename, text):
    #Writes text(gist) to filename
  
    fp = open(filename, 'w')
    fp.write(text)
    fp.close()
  
def download(permission, user, request, fileno):
    #Loads and writes all the gists to dirname
  
    if(permission == "yes" or permission == "no"):
        gist_urls, files = load(request)
        dirname = user+"'s_gists/"
        create_directory(dirname)
        if(fileno[1] == "all"):
            for i in range(len(gist_urls)):
                gist = requests.get(gist_urls[i])
                write_gist(dirname+files[i], gist.text)
        else:
            for i in range(1,len(fileno)):
                gist = requests.get(gist_urls[int(fileno[i])])
                write_gist(dirname+files[int(fileno[i])], gist.text)
  
def detailed(urls, pos):
    #Prints out the contents of a file
  
    gist = requests.get(urls[int(pos)])
    print(gist.text)
  
def main():
    #Authenticates and downloads gists according to user's choice
    #Commands:
    #show: To show all the available gists with their assigned gistno
    #download all: To download all the available gists
    #download gistno(s): To download gist(s) assigned to gistno(s)
    #detailed gistno: To print content of gist assigned to gistno
    #exit: To exit the script
  
    ask_auth, user, request = auth()
    urls, files = load(request)
    try:
        while(1):
            command = input("Enter your command: ")
            if("download" in command):
                download(ask_auth, user, request, command.split(" "))
            elif("detailed" in command):
                detailed(urls, command.split(" ")[1])
            elif(command == "show"):
                show(files)
            elif(command == "exit"):
                return
    except:
        pass
  
if(__name__ == '__main__'):
    main()

解释
GithubGist API 将每个用户的信息存储在 http://api.github.com/users/username/gists。

  • 向上述 url 发送 HTTP 请求以检索有关用户的信息。
  • 搜索每个可用要点的 raw_url并发送 HTTP 请求以检索有关它们的信息。
  • 根据您的需要处理信息。