Skip to content

Instantly share code, notes, and snippets.

@thusoy
Created December 25, 2016 10:10
Show Gist options
  • Save thusoy/e76bd817f9092dac08f0d7ab90f21834 to your computer and use it in GitHub Desktop.
Save thusoy/e76bd817f9092dac08f0d7ab90f21834 to your computer and use it in GitHub Desktop.
Import to Pinboard from Pocket while preserving read status and multi-word tags.
import sys
import time
import os
import re
import requests
def main():
to_read = 'yes'
LINE_RE = re.compile(r'href="(.*?)" time_added="(.*?)" tags="(.*?)">(.*?)</a>')
session = requests.Session()
api_token = os.environ['PINBOARD_TOKEN']
api_endpoint = 'https://api.pinboard.in/v1/posts/add'
for line in sys.stdin:
if '<h1>Read Archive</h1>' in line:
to_read = 'no'
line = line.strip()
match = LINE_RE.search(line)
if match:
link, time_added, raw_tags, title = match.groups()
tags = parse_tags(raw_tags)
params = {
'auth_token': api_token,
'url': link,
'description': title,
'tags': ','.join(tags),
'dt': time_added,
'replace': 'yes',
'shared': 'no',
'toread': to_read,
}
response = session.get(api_endpoint, params=params)
timeout = 3.5
while response.status_code != 200:
if response.status_code == 429:
timeout *= 2
time.sleep(timeout)
response = session.get(api_endpoint, params=params)
print('Submitted %s' % title)
time.sleep(timeout)
def parse_tags(tag_string):
tags = []
raw_tags = tag_string.split(',')
for tag in raw_tags:
if not tag:
continue
tags.append(tag.lower().replace(' ', '-'))
return tags
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment