2011-07-10 31 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))