Snippets
Created by
7shi n
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | import base64, json, os, time, urllib.request
authinfo = "USER:PASS"
user = authinfo.split(":")[0]
apiurl = "https://api.bitbucket.org/2.0"
auth = {
"Authorization":
"Basic " + base64.encodebytes(authinfo.encode("utf-8")).decode("ascii").strip()
}
os.makedirs("cache", exist_ok=True)
def read_or_down(fn, url):
if os.path.isfile(fn):
with open(fn, "rb") as f:
return f.read()
print("GET:", url)
time.sleep(1)
req = urllib.request.Request(url, headers=auth)
with urllib.request.urlopen(req) as f:
data = f.read()
with open(fn, "wb") as f:
f.write(data)
return data
def getvalues(category):
url = "{}/{}/{}".format(apiurl, category, user)
values = []
p = 1
while url:
fn = "cache/{}-{}.json".format(category, p)
data = json.loads(read_or_down(fn, url).decode("utf-8"))
values += data["values"]
url = data["next"] if "next" in data else None
p += 1
return values
def getlinks(value, dir):
if not "links" in value: return
for name, link in value["links"].items():
if not "href" in link: continue
href = link["href"]
if href.startswith(apiurl):
ext = "diff" if name in ["patch", "diff"] else "json"
read_or_down("{}/{}.{}".format(dir, name, ext), href)
def getvalue(data, key, value):
for v in data:
if v[key] == value: return v
return None
def getclone(value):
return getvalue(value["links"]["clone"], "name", "https")["href"]
def getrepodir(clone):
ret = clone.split("/")[-1]
if ret.endswith(".git"): return ("git", ret[:-4])
return ("hg", ret)
def writehdr(f):
f.write("""
mkdir -p tmp
clone() {
dir=$1
repodir=$2
scm=$3
url=$4
if [ ! -d $dir/$repodir ]; then
cd tmp
rm -rf $repodir
$scm clone $url && mv $repodir ../$dir
cd ..
fi
}
""".lstrip())
repositories = {}
with open("repositories-clone.sh", "w", encoding="utf-8") as f:
writehdr(f)
values = getvalues("repositories")
for i, value in enumerate(values):
clone = getclone(value)
_, repodir = getrepodir(clone)
echo = "{}/{}: {}".format(i + 1, len(values), value["name"])
print(echo)
repositories[repodir] = value
dir = "repositories/" + repodir
os.makedirs(dir, exist_ok=True)
getlinks(value, dir)
scm = value["scm"]
f.write(f"echo '{echo}'; clone {dir} {repodir} {scm} {clone}\n")
if value["has_wiki"]:
f.write(f"clone {dir} wiki {scm} {clone}/wiki\n")
with open("repositories.json", "w", encoding="utf-8") as f:
json.dump(repositories, f, indent=4)
getvalues("pullrequests")
snippets = {}
with open("snippets-clone.sh", "w", encoding="utf-8") as f:
writehdr(f)
values = getvalues("snippets")
for i, value in enumerate(values):
created = value["created_on"][:19].replace("-", "").replace(":", "").replace("T", "-")
clone = getclone(value)
scm, repodir = getrepodir(clone)
name = created + "_" + repodir
echo = "{}/{}: {}".format(i + 1, len(values), name)
print(echo)
snippets[name] = value
dir = "snippets/" + name
os.makedirs(dir, exist_ok=True)
getlinks(value, dir)
f.write(f"echo '{echo}'; clone {dir} {repodir} {scm} {clone}\n")
with open("snippets.json", "w", encoding="utf-8") as f:
json.dump(snippets, f, indent=4)
|
Comments (1)
You can clone a snippet to your computer for local editing. Learn more.
使い方は以下の記事を参照してください。