logo

Github에서 요점을 다운로드하는 것이 간단해졌습니다.

GithubGist 비공개 또는 공개 요점을 만들 수 있는 곳입니다. 즉, 파일을 비공개 또는 공개적으로 저장할 수 있습니다. 프로젝트에 대한 많은 요점을 작성했고 그 중 일부를 다운로드하려는 시나리오를 가정해 보겠습니다. 당신이 그것을 사용할 수있는 유일한 방법은 GithubGist HTTP 또는 SSH를 통해 ZIP을 다운로드하거나 복제하기 위해 각각의 개별 요점을 여는 것입니다. 이 글은 위의 작업을 더 간단하게 만드는 것에 관한 것입니다. 아래 명령을 사용하면 비밀번호를 알 때까지 개인 사용자를 제외한 다른 github 사용자로부터 요점을 다운로드할 수도 있습니다. 우리는 사용할 것입니다 요청 이 제안에 대한 패키지. 최소한의 코드로 HTTP 요청을 보내는 멋진 패키지입니다. 설치 1. 다음에서 패키지를 다운로드합니다. PyPI pip3을 사용하는 터미널을 통해 통사론:
pip3 install requests
메모: To become a root user run the following command:
sudo pip3 install requests
Python3 스크립트 스크립트는 온라인 IDE에서 실행할 수 없으므로 다음을 클릭할 수 있습니다. 여기 to see how it works. Python
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(1len(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는 https://api.github.com/users/username/gists에 각 사용자에 대한 정보를 저장합니다.
  • 사용자에 대한 정보를 검색하려면 위 URL로 HTTP 요청을 보냅니다.
  • 다음을 검색하세요. raw_url 사용 가능한 모든 요점을 파악하고 이에 대한 정보를 검색하기 위해 HTTP 요청을 보냈습니다.
  • 귀하의 필요에 맞게 정보를 조작했습니다.