summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2018-08-15 21:47:47 -0700
committerBryan Newbold <bnewbold@robocracy.org>2018-08-15 21:47:47 -0700
commit68e847aadc728d63057be5b8d547c851b02a0008 (patch)
tree1aef1c1b9d56e246cb6556f85387b13d6b822786
parent4c11f65f202ef8f71bfd640232ed30ccd6f4c3a4 (diff)
downloadfatcat-68e847aadc728d63057be5b8d547c851b02a0008.tar.gz
fatcat-68e847aadc728d63057be5b8d547c851b02a0008.zip
WIP on fatcat_export
-rw-r--r--python/fatcat/fcid.py17
-rwxr-xr-xpython/fatcat_export.py72
2 files changed, 89 insertions, 0 deletions
diff --git a/python/fatcat/fcid.py b/python/fatcat/fcid.py
new file mode 100644
index 00000000..dd72b242
--- /dev/null
+++ b/python/fatcat/fcid.py
@@ -0,0 +1,17 @@
+
+import base64
+import uuid
+
+def fcid2uuid(s):
+ s = s.split('_')[-1].upper().encode('utf-8')
+ assert len(s) == 26
+ raw = base64.b32decode(s + b"======")
+ return str(uuid.UUID(bytes=raw)).lower()
+
+def uuid2fcid(s):
+ raw = uuid.UUID(s).bytes
+ return base64.b32encode(raw)[:26].lower().decode('utf-8')
+
+def test_fcid():
+ test_uuid = '00000000-0000-0000-3333-000000000001'
+ assert test_uuid == fcid2uuid(uuid2fcid(test_uuid))
diff --git a/python/fatcat_export.py b/python/fatcat_export.py
new file mode 100755
index 00000000..6c4502af
--- /dev/null
+++ b/python/fatcat_export.py
@@ -0,0 +1,72 @@
+#!/usr/bin/env python3
+
+import sys
+import json
+import argparse
+import fatcat_client
+from fatcat_client.rest import ApiException
+from fatcat.fcid import uuid2fcid
+
+def run_export_releases(args):
+ conf = fatcat_client.Configuration()
+ conf.host = args.host_url
+ api = fatcat_client.DefaultApi(fatcat_client.ApiClient(conf))
+
+ for line in args.ident_file:
+ ident = uuid2fcid(line.split()[0])
+ release = api.get_release(id=ident, expand="all")
+ args.json_output.write(json.dumps(release.to_dict()) + "\n")
+
+def run_export_changelog(args):
+ conf = fatcat_client.Configuration()
+ conf.host = args.host_url
+ api = fatcat_client.DefaultApi(fatcat_client.ApiClient(conf))
+
+ end = args.end
+ if end is None:
+ latest = api.get_changelog(limit=1)[0]
+ end = latest.index
+
+ for i in range(args.start, end):
+ entry = api.get_changelog_entry(id=i)
+ args.json_output.write(json.dumps(entry.to_dict()) + "\n")
+
+def main():
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--debug',
+ action='store_true',
+ help="enable debugging interface")
+ parser.add_argument('--host-url',
+ default="http://localhost:9411/v0",
+ help="connect to this host/port")
+ subparsers = parser.add_subparsers()
+
+ sub_releases = subparsers.add_parser('releases')
+ sub_releases.set_defaults(func=run_export_releases)
+ sub_releases.add_argument('ident_file',
+ help="TSV list of fatcat release idents to dump",
+ default=sys.stdin, type=argparse.FileType('r'))
+ sub_releases.add_argument('json_output',
+ help="where to send output",
+ default=sys.stdout, type=argparse.FileType('w'))
+
+ sub_changelog = subparsers.add_parser('changelog')
+ sub_changelog.set_defaults(func=run_export_changelog)
+ sub_changelog.add_argument('--start',
+ help="index to start dumping at",
+ default=1, type=int)
+ sub_changelog.add_argument('--end',
+ help="index to stop dumping at (else detect most recent)",
+ default=None, type=int)
+ sub_changelog.add_argument('json_output',
+ help="where to send output",
+ default=sys.stdout, type=argparse.FileType('w'))
+
+ args = parser.parse_args()
+ if not args.__dict__.get("func"):
+ print("tell me what to do!")
+ sys.exit(-1)
+ args.func(args)
+
+if __name__ == '__main__':
+ main()