从Github下载GIST变得很简单

吉卜赛人 是一个可以创建私人或公共gist的地方,即私人或公开存储文件。让我们假设一个场景,您已经为您的项目编写了大量GIST,并且希望下载一组GIST。只有这样你才能使用 吉卜赛人 就是打开每个gist,通过HTTP或SSH下载ZIP或克隆。 本文旨在简化上述任务。使用下面的命令,您甚至可以从其他github用户(私人用户除外)下载GIST,直到您知道他们的密码。

null

我们将使用 请求 这项提议的一揽子方案。用最少的代码发送HTTP请求是一个很棒的包。

安装

1.从 皮皮 使用pip3的via终端 语法:

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 <em>dirname</em>
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请求以检索有关用户的信息。
  • 搜索 原始网址 并发送HTTP请求以检索有关它们的信息。
  • 根据你的需要操纵信息。

本文由 斯里·桑凯斯·乌帕拉帕蒂 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享