Skip to content

Instantly share code, notes, and snippets.

@tobywf
Created April 7, 2020 03:33
Show Gist options
  • Save tobywf/d7b4378417f4a10a75dd7245ec557240 to your computer and use it in GitHub Desktop.
Save tobywf/d7b4378417f4a10a75dd7245ec557240 to your computer and use it in GitHub Desktop.
Save all gists from GitHub, including comments
"""
Save all gists from GitHub, including comments.
Requires the PyGithub library (``pip install PyGithub>=1.47``).
Pass a personal access token with the scope "gist" as the first
argument.
This probably won't work for large files (+10MiB), since they aren't
returned by the GitHub API, and must be cloned instead.
"""
import sys
from github import Github
from pathlib import Path
try:
_prog, token = sys.argv
except ValueError:
print("usage:", sys.argv[0], "<personal access token>")
sys.exit(1)
gh = Github(token)
base = Path.cwd()
user = gh.get_user()
info = [
"# Gists",
"",
]
for gist in user.get_gists():
print("Gist", gist.id)
gist_path = base / gist.id
gist_path.mkdir(exist_ok=True)
# index
visibility = "" if gist.public else "🔒"
gist_link = f"[{gist.id}]({gist.html_url})"
info.extend([
f"## {gist_link} {visibility}",
"",
f"Created: {gist.created_at.isoformat(timespec='seconds')}Z / "
f"Updated: {gist.updated_at.isoformat(timespec='seconds')}Z",
"",
gist.description,
"",
])
info.extend([
f"* `{file.filename}` - {file.language} - {file.last_modified}"
for file in gist.files.values()
])
info.append("")
# comments
if gist.comments:
comments = [
f"# Comments for {gist_link}",
"",
]
for comment in gist.get_comments():
comments.extend([
f"[{comment.id}]({gist.html_url}#gistcomment-{comment.id}) - "
f"[{comment.user.login}]({comment.user.html_url}) - "
f"Created: {comment.created_at.isoformat(timespec='seconds')}Z / ",
f"Updated: {comment.updated_at.isoformat(timespec='seconds')}Z",
"",
comment.body,
"",
])
comment_path = gist_path / "comments.md"
comment_path.write_text("\n".join(comments), encoding="utf-8")
# files
for file in gist.files.values():
print("*", file.filename)
file_path = gist_path / file.filename
file_path.write_text(file.content, encoding="utf-8")
index_path = base / "index.md"
index_path.write_text("\n".join(info), encoding="utf-8")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment