Last active
April 20, 2025 18:11
-
-
Save n8henrie/890839d99493fb63bd0395a6425d3e11 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*.json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python3 | |
"""Download a backup of my bitly links.""" | |
import datetime as dt | |
import json | |
import subprocess | |
import sys | |
from pathlib import Path | |
from urllib.request import Request, urlopen | |
def get_keychain_credential(account, service): | |
"""Get credentials from MacOS keychain.""" | |
cmd = f"security find-generic-password -a {account} -s {service} -w" | |
process = subprocess.run(cmd.split(), stdout=subprocess.PIPE) | |
data = process.stdout | |
return data.decode("utf8").strip() | |
def request(url: str, access_token: str) -> dict: | |
req = Request(url) | |
req.add_header("Authorization", "Bearer {}".format(access_token)) | |
with urlopen(req) as r: | |
resp = json.loads(r.read()) | |
return resp | |
def main(access_token: str): | |
"""Run the main code logic.""" | |
output = Path(".") / f"{dt.datetime.today():%Y%m%d}_bitly_export.json" | |
url = "https://api-ssl.bitly.com/v4/groups/Ba9b2FIuUtv/bitlinks?archived=both&size=50" | |
all_data = [] | |
while True: | |
resp = request(url, access_token) | |
all_data.append(resp) | |
url = resp.get("pagination", {}).get("next") | |
if not url: | |
break | |
output.write_text(json.dumps(all_data, indent=4)) | |
def cli() -> dict: | |
config = dict(access_token=sys.argv[1]) | |
return config | |
if __name__ == "__main__": | |
main(**cli()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment