Snippets

7shi n [py] Get repository informations at Bitbucket

Created by 7shi n last modified
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)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.