2011-07-10 42 views
16

是否有API調用或任何我未能推翻的腳本,將Github的所有Gist從Github中移出到外部git倉庫,或者只是給我返回一個他們的名字列表?我知道每個人都是一個單獨的git回購,所以我認爲我能做的最好的就是獲得後者,然後腳本將其全部放到我的本地盒子中。從Github上拉動所有Gist?

編輯1:我知道從一個服務推和拉的git回購協議到另一個,我專門找誰在收集權威列表中的所有要旨我有,私人和公共有411人。我也認爲這可能對其他人有用。這不是關於遷移,而是備份戰略。 。 。各種各樣的。

編輯2:所以,看來這可能是不可能的。我顯然沒有Google hard enough to search the updated Github/Gist API。其他API調用使用簡單的curl命令,但不適用於Gist的v1 API。儘管如此,API還是說TBD代表所有的私人和公共Gists,所以我認爲除非一個開明的靈魂把brotha掛起來,否則就會把這個cabash放在整個事情上。

$ curl http://github.com/api/v2/json/repos/show/alharaka 
{"repositories":[{"url":"https://github.com/alharaka/babushka","has_wiki":true,"homepage":"http: 
... # tons of more output 
echo $? 
0 
$ 

這一個不工作這麼熱。

$ curl https://gist.github.com/api/v1/:format/gists/:alharaka 
$ echo $? 
0 
$ 

編輯3:之前有人問我,我注意到在API版本的差異;這個「精彩的黑客」也沒有幫助。儘管如此,仍然很酷。

$ curl https://gist.github.com/api/v2/:format/gists/:alharaka # Notice v2 instead of v1 
$ echo $? 
0 
$ 
+0

我認爲這個問題需要更清楚地描述你正在做什麼。 – Soren

+0

我曾預料到過。往上看。如果還不清楚,不知道還有什麼要說清楚的:API允許我使用JSON(無論是否已驗證)來獲取有關repos *或*寫入的數據。很酷,我必須說。儘管如此,功能還不是全部。 – songei2f

+0

https://gist.github.com/1622504 – endolith

回答

18

GitHub的API的版本3允許這種在一個非常簡單的方法:

https://api.github.com/users/koraktor/gists 

給你的用戶的所有要旨的列表,該列表提供了各種量的URL包括API個別Gist的網址如

https://api.github.com/gists/921286 

查看Gists API v3 documentation

+0

忘了評論一段時間了。這看起來有希望。當我有一個系統來處理這個問題時,我會盡快回復你。謝謝。 – songei2f

+1

我測試了這個,它沒有列出用戶的所有要點 – Brad

+1

請注意,上面的鏈接將只獲得第一頁上的所有要點。如果您需要訪問您的所有要點,請在URL的末尾添加「?page = 」,默認情況下會打開第一個「1」頁面。 –

1

This ruby gem似乎有助於你的問題。 我還沒有嘗試過,但看起來很有前途。

首先

gem install gisty 

而且你需要把

export GISTY_DIR="$HOME/dev/gists" 

在您的.bashrc或者.zshrc 這dir是你的要旨保存。

需要

git config --global github.user your_id 
git config --global github.token your_token 

添加上述配置您的.gitconfig

使用

  • gisty後文件1文件2 ...

    帖子file1和文件2到您的要點

  • gisty private_post文件1文件2 ...

    帖子file1和file2的私人

  • gisty同步

    同步到所有學家

  • gisty pull_all

    拉至本地回購

  • gisty列表

    名單克隆當地要點回購

+0

由於api更改,github.token現在不起作用。 – weakish

+0

它現在使用OAuth。與Gist API的v3一起撰寫的文章非常棒! –

2

我寫了一個快速的Node.js腳本作爲一個練習,下載所有學家,並與他們保存與「gist description」名稱匹配的文件夾中的原始要點相同的文件名。 https://gist.github.com/thomastraum/5227541

var request = require('request') 
    , path = require('path') 
    , fs = require('fs') 
    , url = "https://api.github.com/users/thomastraum/gists" 
    , savepath = './gists'; 

request(url, function (error, response, body) { 

    if (!error && response.statusCode == 200) { 

     gists = JSON.parse(body); 
     gists.forEach(function(gist) { 

      console.log("description: ", gist.description); 
      var dir = savepath + '/' + gist.description; 

      fs.mkdir(dir, function(err){ 
       for(var file in gist.files){ 

        var raw_url = gist.files[file].raw_url; 
        var filename = gist.files[file].filename; 

        console.log("downloading... " + filename); 
        request(raw_url).pipe(fs.createWriteStream(dir + '/' + filename)); 
       } 
      }); 
     }); 

    } 

}); 
13

還有就是nicerobot's scriptan adaptation in API v3,這是最初書面API V1:

#!/usr/bin/env python 
# Clone or update all a user's gists 
# curl -ks https://raw.github.com/gist/5466075/gist-backup.py | USER=fedir python 
# USER=fedir python gist-backup.py 

import json 
import urllib 
from subprocess import call 
from urllib import urlopen 
import os 
import math 
USER = os.environ['USER'] 

perpage=30.0 
userurl = urlopen('https://api.github.com/users/' + USER) 
public_gists = json.load(userurl) 
gistcount = public_gists['public_gists'] 
print "Found gists : " + str(gistcount) 
pages = int(math.ceil(float(gistcount)/perpage)) 
print "Found pages : " + str(pages) 

f=open('./contents.txt', 'w+') 

for page in range(pages): 
    pageNumber = str(page + 1) 
    print "Processing page number " + pageNumber 
    pageUrl = 'https://api.github.com/users/' + USER + '/gists?page=' + pageNumber + '&per_page=' + str(int(perpage)) 
    u = urlopen (pageUrl) 
    gists = json.load(u) 
    startd = os.getcwd() 
    for gist in gists: 
     gistd = gist['id'] 
     gistUrl = 'git://gist.github.com/' + gistd + '.git' 
     if os.path.isdir(gistd): 
      os.chdir(gistd) 
      call(['git', 'pull', gistUrl]) 
      os.chdir(startd) 
     else: 
      call(['git', 'clone', gistUrl]) 
     if gist['description'] == None: 
      description = '' 
     else: 
      description = gist['description'].encode('utf8').replace("\r",' ').replace("\n",' ') 
     print >> f, gist['id'], gistUrl, description 
+0

curl聲明現在似乎是:curl -ks https://gist.githubusercontent.com/fedir/5466075/raw/gist-backup.py | USER = fedir python – philshem

+0

@philshem非常感謝,github改變了URL,我更新了答案。 –

+0

作品非常感謝你! :) – Atlas7

5

@Fedir的腳本,佔Github的分頁(如果你有幾百的一個版本要點):

#!/usr/bin/env python 
# Clone or update all a user's gists 
# curl -ks https://raw.github.com/gist/5466075/gist-backup.py | USER=fedir python 
# USER=fedir python gist-backup.py 

import json 
import urllib 
from subprocess import call 
from urllib import urlopen 
import os 
import math 
USER = os.environ['USER'] 

perpage=30.0 
userurl = urlopen('https://api.github.com/users/' + USER) 
public_gists = json.load(userurl) 
gistcount = public_gists['public_gists'] 
print "Found gists : " + str(gistcount) 
pages = int(math.ceil(float(gistcount)/perpage)) 
print "Found pages : " + str(pages) 

f=open('./contents.txt', 'w+') 

for page in range(pages): 
    pageNumber = str(page + 1) 
    print "Processing page number " + pageNumber 
    pageUrl = 'https://api.github.com/users/' + USER + '/gists?page=' + pageNumber + '&per_page=' + str(int(perpage)) 
    u = urlopen (pageUrl) 
    gists = json.load(u) 
    startd = os.getcwd() 
    for gist in gists: 
     gistd = gist['id'] 
     gistUrl = 'git://gist.github.com/' + gistd + '.git' 
     if os.path.isdir(gistd): 
      os.chdir(gistd) 
      call(['git', 'pull', gistUrl]) 
      os.chdir(startd) 
     else: 
      call(['git', 'clone', gistUrl]) 
+0

不錯的改進。它似乎是,頁面迭代應該從1開始,而不是0,否則您將不會獲取最後一頁。 –

+0

良好的腳本,很容易採用,以便它也拉動私人要求。 –

+0

如何使用這個腳本來拉我的祕訣? – yeedle

2

根據this answer中的提示,我編寫了這個簡單的Python腳本,它爲我做了竅門。

這是非常小巧的代碼,幾乎沒有任何錯誤檢查,並將所有用戶的要點克隆到當前目錄中。對於處理更新的版本,請參閱https://gist.github.com/SpotlightKid/042491a9a2987af04a5a

+0

這一款適合我! –

0

如果您只需從特定用戶下載所有要點,那麼這個簡單的python腳本將有所幫助。

特定用戶要旨信息通過API

"https://api.github.com/users/" + username + "/gists" 

你可以簡單地遍歷JSON通過API暴露,得到學家的名單,進行克隆,或者乾脆使用原始URL下載要旨曝光指定。下面的簡單腳本通過JSON循環,提取文件名和原始URL並下載所有要點並將其保存在本地文件夾中。

import requests 

# Replace username with correct username 
url = "https://api.github.com/users/" + username + "/gists" 

resp = requests.get(url) 
gists = resp.json() 

for gist in gists: 
    for file in gist["files"]: 
     fname = gist["files"][file]["filename"] 
     furl = gist["files"][file]["raw_url"] 
     print("{}:{}".format(fname, furl)) # This lists out all gists 

     Use this to download all gists 
     pyresp = requests.get(furl) 

     with open("../folder/" + fname, "wb") as pyfile: 
      for chunk in pyresp.iter_content(chunk_size=1024): 
       if chunk: 
        pyfile.write(chunk) 
     print("{} downloaded successfully".format(fname))