Skip to content

Instantly share code, notes, and snippets.

@tgray
Created January 2, 2015 16:43
Show Gist options
  • Save tgray/43b0d512213f4bb53a4f to your computer and use it in GitHub Desktop.
Save tgray/43b0d512213f4bb53a4f to your computer and use it in GitHub Desktop.
Python script for bulk import of links into inboard. Input is two files, one with urls and one with titles.
#!/usr/bin/env python
# https://github.com/tgray
import urllib, sys, os
import argparse
def importFile(filename):
filename = os.path.expanduser(filename)
try:
f = open(filename)
except:
print("file {} does not exist".format(filename))
sys.exit(1)
lines = f.readlines()
# strip white space
lines = [l.strip() for l in lines]
# remove empty lines
lines = filter(None, lines)
f.close()
return lines
try:
f = open(os.path.join(os.environ['HOME'], '.pinboard-token'))
token = f.readline().strip()
f.close()
except:
sys.exit(1)
argv = sys.argv
parser = argparse.ArgumentParser(
description = 'Import links to pinboard.')
# parser.add_argument('-s', '--star',
# dest = 'starred',
# action = 'store_true',
# help = 'Star the links')
parser.add_argument('-s', '--shared',
dest = 'shared',
action = 'store_true',
help = 'Make bookmark public')
parser.add_argument('-r', '--to-read',
dest = 'toread',
action = 'store_true',
help = 'Mark bookmark to read')
parser.add_argument('-n', '--dry-run',
dest = 'dryrun',
action = 'store_true',
help = 'Dry run.')
parser.add_argument('-t', '--tag',
dest = 'tags',
action = 'store',
help = 'Tag links with argument')
parser.add_argument('urlfile', type = str,
help = 'The file containing the URLS')
parser.add_argument('titlefile', type = str,
help = 'The file containing the titles')
args = parser.parse_args()
urls = importFile(args.urlfile)
titles = importFile(args.titlefile)
u = zip(urls, titles)
qtags = ''
qshared = '&shared=no'
qread = '&toread=no'
tags = ['.pinimport-py',]
if args.tags:
if args.tags.count(',') > 0:
ntags = args.tags.split(',')
else:
ntags = args.tags.split()
tags.extend(ntags)
tags = [t.strip() for t in tags]
jtags = ','.join(tags)
qtags = '&tags={}'.format(jtags)
if args.shared:
qshared = '&shared=yes'
if args.toread:
qread = '&toread=yes'
entries = []
for i in u:
e = {}
e['link'] = i[0]
e['title'] = i[1]
entries.append(e)
queries = []
for entry in entries:
query = ('https://api.pinboard.in/v1/posts/add?auth_token=' + token +
'&url=' + urllib.quote(entry['link']) + '&description=' +
urllib.quote(entry['title'].encode('utf-8')) + qtags + qshared + qread)
if not args.dryrun:
urllib.urlopen(query)
else:
print('(dry run)'),
print("imported '{}'".format(entry['title']))
print("")
print("All bookmarks imported with:")
print(" shared: {}, toread: {}".format(args.shared, args.toread))
print(" tags: {}".format(', '.join(tags)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment