27

Github からすべての Gist を外部の git リポジトリにプルするか、単にそれらの名前のリストを返す API 呼び出しまたは覆すことができなかったスクリプトはありますか? それぞれが個別の git リポジトリであることはわかっているので、私ができる最善の方法は後者を取得してから、それらすべてをローカル ボックスに取得するスクリプトを作成することだと思います。

編集 1 : あるサービスから別のサービスへの git リポジトリのプルとプッシュについては知っています。具体的には、プライベートとパブリックのすべての Gist の信頼できるリストの収集に関する 411 を持っている人を探しています。また、これは他の人にも役立つかもしれないと思いました。移行ではなく、バックアップ戦略です。. . ある種の。

EDIT 2:したがって、これは不可能なようです。更新された Github/Gist API を検索するのに十分なほど Googleを調べていなかったようです。他の API 呼び出しは単純な curl コマンドで機能しますが、Gist の v1 API では機能しません。それでも、API は、すべてのプライベートおよびパブリック Gistsに対して TBD と言っているので、悟りを開いた魂がブロサを接続しない限り、それは全体にカバッシュを置くと思います.

$ 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
$
4

11 に答える 11

22

GitHub API のバージョン 3 では、非常に簡単な方法でこれが可能になります。

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

ユーザーのすべての Gist のリストを提供し、そのリストは、次のような個々の Gist への API URL を含むさまざまな量の URL を提供します

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

Gists API v3 ドキュメントを参照してください。

于 2011-07-17T14:48:40.663 に答える
15

API v1 用に最初に作成されたnicerobot の script のAPI v3に適応があります。

#!/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
于 2013-04-26T09:59:45.993 に答える
5

Github のページネーションを説明する @Fedir のスクリプトのバージョン (要点が数百ある場合):

#!/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])
于 2013-07-10T15:01:59.987 に答える
4

この回答のヒントに基づいて、私はこの簡単な Python スクリプトを作成しました。

これは非常に最小限のコードであり、エラー チェックはほとんど行われず、すべてのユーザーの要点が現在のディレクトリに複製されます。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Clone all gists of GitHub username given on the command line."""

import subprocess
import sys
import requests

if len(sys.argv) > 1:
    gh_user = sys.argv[1]
else:
    print("Usage: clone-gists.py <GitHub username>")
    sys.exit(1)

req = requests.get('https://api.github.com/users/%s/gists' % gh_user)

for gist in req.json():
    ret = subprocess.call(['git', 'clone', gist['git_pull_url']])
    if ret != 0:
        print("ERROR cloning gist %s. Please check output." % gist['id'])

更新も処理するバージョンについては、https://gist.github.com/SpotlightKid/042491a9a2987af04a5aを参照してください。

于 2015-12-02T20:18:48.820 に答える
3

Thomas Traum のいくつかの回答に加えて。ユーザーエージェントは必須のようです: http://developer.github.com/v3/#user-agent-required

そこで、 https ://github.com/sanusart/gists-backup で自分で演習を行いました。ページング、重複した説明、説明の欠落も認識しています。

于 2014-03-13T21:45:42.243 に答える
2

演習として簡単な node.js スクリプトを作成し、すべての Gist をダウンロードして、元の Gist と同じファイル名で「Gist の説明」の名前と一致するフォルダーに保存します。 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 ));
                }
            });
        });

    }

});
于 2013-03-23T22:48:36.947 に答える
1

このルビーの宝石はあなたの問題を解決するようです。まだ試していませんが、有望そうです。

初め

gem install gisty

そして、あなたは置く必要があります

export GISTY_DIR="$HOME/dev/gists"

あなたの .bashrc または .zshrc で、このディレクトリは要旨が保存された場所です。

必要がある

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

上記の設定を .gitconfig に追加します

使用法

  • 気になる投稿 file1 file2 ...

    file1 と file2 を要点に投稿します

  • gisty private_post file1 file2 ...

    file1 と file2 を非公開で投稿する

  • 気まぐれな同期

    すべての Gist と同期する

  • 気まぐれな pull_all

    ローカル リポジトリにプルする

  • 気になるリスト

    クローンされたローカル Gist リポジトリを一覧表示する

于 2011-08-16T00:26:43.903 に答える
0

特定のユーザーからすべての Gist をダウンロードするだけでよい場合は、この単純な Python スクリプトが役に立ちます。

特定のユーザーの要点情報が API 経由で公開されます

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

API によって公開された JSON をループするだけで、Gist のリストを取得したり、クローンを実行したり、指定された生の URL を使用して Gist をダウンロードしたりできます。以下の単純なスクリプトは、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))
于 2015-12-20T13:19:11.263 に答える