forked from mathcass/pinboard-org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinboard.py
103 lines (83 loc) · 2.98 KB
/
pinboard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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]
# 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}
""".format(user=user,
time=now)
org_template = """* [[{href}][{description}]] {joined_tags}
:PROPERTIES:
:TIME_SAVED: {time}
:URL: {href}
:END:
{extended}
"""
with codecs.open(orgfile, 'w', encoding='utf-8') as fout:
fout.write(header)
for l in json_lines:
description = l['description']
href = l['href']
server_tags = l['tags'].split(' ')
server_tags_fixed = [w.replace('-', '_') for w in server_tags]
tags = list(filter(None, server_tags_fixed))
if tags:
joined_tags = ":".join([""] + tags + [""])
else:
joined_tags = ""
server_time = datetime.datetime.strptime(l['time'],
'%Y-%m-%dT%H:%M:%SZ')
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)
# print("Writing: ", org_line.encode('utf-8'))
fout.write(org_line)
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: pinboard.py <outputfile>")
exit(1)
output_file = sys.argv[1]
main(output_file)