Skip to content

Instantly share code, notes, and snippets.

@jaanus
Created July 30, 2018 19:03
Show Gist options
  • Save jaanus/3f5fc340c0f8a92f3ecc06dabcf38970 to your computer and use it in GitHub Desktop.
Save jaanus/3f5fc340c0f8a92f3ecc06dabcf38970 to your computer and use it in GitHub Desktop.
Upload Instapaper bookmarks to Pinboard. For input, uses the CSV file as exported from Instapaper, or received from their support.
#!/usr/bin/env python3
"""Upload Instapaper bookmarks to Pinboard. For input, uses the CSV file as
exported from Instapaper, or received from their support.
"""
import sys
import os
import argparse
import csv
# https://github.com/lionheart/pinboard.py
import pinboard
def main(arguments):
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('-i', '--infile', help="Input file in CSV format as exported from Instapaper", required=True, type=argparse.FileType('r'))
args = parser.parse_args(arguments)
try:
api_token = os.environ["PINBOARD_TOKEN"]
except KeyError:
print("You must specify Pinboard API token in PINBOARD_TOKEN environment variable.")
return 1
pb = pinboard.Pinboard(api_token)
reader = csv.reader(args.infile, delimiter=',', quotechar="\"")
next(reader) # skip 1st line (header)
for row in reader:
# sometimes title is blank. Pinboard API will barf and abort on this. I prefer to just fix the CSV manually in that case
url, title, selection, folder = row
print(folder, " – ", title)
toread = False
# Pinboard doesn’t like multi-word folders and converts them to multiple tags. I had few enough of these
# that I could just fix that manually.
tags = [folder]
if folder == "Unread":
toread = True
tags = []
pb.posts.add(url=url, description=title, extended=selection, tags=tags, toread = toread)
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment