diff --git a/importers/pinboard/pinboard.py b/importers/pinboard/pinboard.py new file mode 100644 index 0000000..91157b6 --- /dev/null +++ b/importers/pinboard/pinboard.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python3 + +""" +Pinboard to bookmarks.public importer + +Requires: +* Python 3.5 +* Requests: +""" + +import json +from os.path import join, dirname, realpath + +import requests + +PINBOARD_API_ENDPOING = ('https://api.pinboard.in/v1/{method}' + '?auth_token={api_token}&format=json') +BOOKMARK_PUBLIC_TPL = ('
  • ' + '{description}
  • \n') + + +def get_bookmarks(api_token): + resp = requests.get(PINBOARD_API_ENDPOING.format(method='posts/all', + api_token=api_token)) + + if not resp.status_code == 200: + raise Exception + + return resp.json() + + +def write_data_file(bookmarks, filename): + with open(filename, 'w') as fobj: + for bookmark in bookmarks: + if not bookmark['shared'] == 'yes': + continue + + if bookmark['description'].startswith('(404) '): + continue + + if bookmark['description'] == '': + bookmark['description'] = bookmark['href'] + + bookmark['tags'] = ', '.join(bookmark['tags'].split(' ')) + fobj.write(BOOKMARK_PUBLIC_TPL.format(**bookmark)) + +def main(api_token): + bookmarks = get_bookmarks(api_token) + + bookmarks_data_file = join(dirname(realpath(__file__)), + '../../bookmarks.data') + + write_data_file(bookmarks, bookmarks_data_file) + + +if __name__ == '__main__': + print('Copy your API Token from .') + print('And paste it below.') + api_token = input('API Token: ') + + main(api_token)