Created
January 10, 2022 21:10
-
-
Save stefan2904/d2b6c930cb2f18438dd00986f86b749d to your computer and use it in GitHub Desktop.
Pinboard Orgmode "Importer", adapted from https://github.com/mathcass/pinboard-org
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
import json | |
import sys | |
import requests | |
import codecs | |
from requests.utils import get_netrc_auth | |
TMP_FILE = '/tmp/pinboard.json' # May not exist in all platforms | |
default_params = {'format': 'json'} | |
def get_all(): | |
"""Gets most recent posts from pinboard.in""" | |
endpoint = "https://api.pinboard.in/v1/" | |
user, token = get_netrc_auth(endpoint) | |
auth_token = "{user}:{token}".format(user=user, token=token) | |
params = {'auth_token': auth_token} | |
params.update(default_params) | |
r = requests.get(endpoint + "posts/all", params=params) | |
return r.json() | |
def write_all_to_tmp_file(all_bookmarks): | |
"""Gets all of the bookmarks from pinboard.in and writes to file as json | |
payload""" | |
with open(TMP_FILE, 'w') as f: | |
for bookmark in all_bookmarks: | |
f.write(json.dumps(bookmark) + '\n') | |
def tmpjsonfile_to_orgfile(orgfile): | |
"""Gets the saved json to file""" | |
import datetime | |
import getpass | |
with open(TMP_FILE) as f: | |
json_lines = [json.loads(l) for l in f] | |
cnt = len(json_lines) | |
# Each pinboard item must have: | |
# * description | |
# * time | |
# * href | |
# * tags | |
# * extended (extended text along with it) | |
user = getpass.getuser() | |
org_time_format = '{0:[%Y-%m-%d %a %H:%M]}' | |
now = org_time_format.format(datetime.datetime.now()) | |
header = """#+TITLE: Pinboard.in Export | |
#+AUTHOR: {user} | |
#+EXPORT_TIME: {time} | |
- Exported entries: {cnt} | |
""".format(user=user, | |
time=now, | |
cnt=cnt) | |
org_template = """** [[{href}][{description}]] | |
:PROPERTIES: | |
:ID: pinboard-{id} | |
:TIME_SAVED: {time} | |
:URL: {href} | |
:TAGS: {joined_tags} | |
:END: | |
{extended} | |
""" | |
lastyear = 3000 | |
alltags = {} | |
with codecs.open(orgfile, 'w', encoding='utf-8') as fout: | |
fout.write(header) | |
for l in json_lines: | |
description = l['description'] if l['description'] else l['href'] | |
href = l['href'] | |
idd = l['hash'] | |
server_tags = l['tags'].split(' ') | |
server_tags_fixed = [w.replace('-', '_') for w in server_tags] | |
tags = list(filter(None, server_tags_fixed)) | |
tags = list(filter(lambda t: t not in ['from', '!from'], tags)) | |
for tag in tags: | |
if tag in alltags: | |
alltags[tag] += 1 | |
else: | |
alltags[tag] = 1 | |
if tags: | |
joined_tags = ":".join([""] + tags + [""]) | |
else: | |
joined_tags = "" | |
server_time = datetime.datetime.strptime(l['time'], | |
'%Y-%m-%dT%H:%M:%SZ') | |
if server_time.year < lastyear: | |
fout.write('* {}\n\n'.format(server_time.year)) | |
lastyear = server_time.year | |
org_time = org_time_format.format(server_time) | |
extended = l['extended'] | |
org_line = org_template.format(description=description, | |
href=href, | |
joined_tags=joined_tags, | |
time=org_time, | |
extended=extended, | |
id=idd) | |
# print("Writing: ", org_line.encode('utf-8')) | |
fout.write(org_line) | |
alltags = dict(sorted(alltags.items(), key=lambda item: item[1])) | |
print(alltags) | |
def main(output_file): | |
"""Main method""" | |
all_bookmarks = get_all() | |
write_all_to_tmp_file(all_bookmarks) | |
tmpjsonfile_to_orgfile(output_file) | |
if __name__ == '__main__': | |
if len(sys.argv) != 2: | |
print("Usage: python3 pinboard.py <outputfile>") | |
exit(1) | |
output_file = sys.argv[1] | |
main(output_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment