Skip to content

Instantly share code, notes, and snippets.

@rachtsingh
Last active June 21, 2022 19:07
Show Gist options
  • Save rachtsingh/9f7b2861a8cb5ab6701e762326b82b01 to your computer and use it in GitHub Desktop.
Save rachtsingh/9f7b2861a8cb5ab6701e762326b82b01 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/python3
"""
This script should idempotently grab the stuff that's newly added to Pocket to pocket.md
You'll need to install https://airtable-python-wrapper.readthedocs.io, i.e. via:
$ pip3 install airtable-python-wrapper
and, of course, sign up for an Airtable account (free seems to work fine).
"""
from airtable import Airtable
import parse
from collections import OrderedDict
fmt_string = "- [{}]({})"
API_KEY = "API_KEY_GOES_HERE"
BASE_KEY = "BASE_KEY_GOES_HERE"
pocket_fn = "/PATH/TO/MD/FILE.md"
def main():
# get data from Airtable
links_tbl = Airtable(api_key=API_KEY,
base_key=BASE_KEY,
table_name='Links')
archive_tbl = Airtable(api_key=API_KEY,
base_key=BASE_KEY,
table_name='Archive')
try:
records = links_tbl.get_all()
except e:
print("Error retrieving records from Airtable")
raise e
old_rows = 0
header = []
# load the pocket.md file, and parse it
links = OrderedDict()
with open(pocket_fn, "r") as f:
parser = parse.compile(fmt_string)
for line in f:
if line[0] == "#":
header.append(line)
continue
res = parser.parse(line.strip())
if not res:
print("[warning]: line `{}` was not parsed".format(line.strip()))
else:
links[res[1]] = res[0]
old_rows += 1
# add in the *new* Airtable rows
records_to_remove = []
new_rows = 0
for rec in records:
if rec['fields']['URL'] not in links:
url, title = rec['fields']['URL'], rec['fields']['Title']
print("grabbed: [{}]({})".format(title, url))
links[rec['fields']['URL']] = rec['fields']['Title']
new_rows += 1
# archive and then delete the old record
records_to_remove.append(rec)
# archive then delete - this'll follow the API rate limit
print("backing up links before deleting...")
archive_tbl.batch_insert([rec["fields"] for rec in records_to_remove])
print("backed up {}".format(len(records_to_remove)))
links_tbl.batch_delete([rec["id"] for rec in records_to_remove])
print("deleted Airtable entries")
# could lose data here...
with open(pocket_fn, "w") as f:
for line in header:
f.write(line.strip() + '\n')
for entry in links:
f.write(fmt_string.format(links[entry], entry) + '\n')
print("grabbed {} new links from Pocket, added to {} existing links".format(new_rows, old_rows))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment