-
Notifications
You must be signed in to change notification settings - Fork 5
/
Pinboard.recipe
executable file
·92 lines (70 loc) · 3.18 KB
/
Pinboard.recipe
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
#!/usr/bin/env python
__license__ = 'GPL v3'
__copyright__ = '2012, Jim DeVona'
__website__ = 'https://github.com/anoved/Pinboard-Recipe'
#
# Find your Pinboard API Token at https://pinboard.in/settings/password
# It will look something like apitoken = 'username:5CABE73682AAA9856010'
#
apitoken = ''
#
# If delete_bookmarks is True, bookmarks will be deleted once retrieved.
# If delete_bookmarks is False, they will just be marked as read instead.
#
delete_bookmarks = False
#
# How many articles to pull at most? Set to False for no checks.
#
max_articles = 15
#
# Should processed articles receive a tag? If so, set the tag:
#
processed_tag = 'calibre-recipe'
from calibre.web.feeds.news import BasicNewsRecipe
import urllib
import json
class PinboardRecipe(BasicNewsRecipe):
title = 'Pinboard Bookmarks'
description = 'Generate an ebook of unread Pinboard bookmarks.'
__author__ = 'Jim DeVona'
auto_cleanup = True
no_stylesheets = True
remove_javascript = True
def parse_index(self):
if len(apitoken) == 0:
self.abort_recipe_processing('Pinboard recipe must be configured with your Pinboard API token.')
# Request all bookmarks marked as unread. Try adding 'tags' to params...
params = urllib.urlencode({'toread': 'yes', 'format': 'json', 'auth_token': apitoken})
pbfile = urllib.urlopen('https://api.pinboard.in/v1/posts/all?' + params)
bookmarks = json.loads(pbfile.read())
if len(bookmarks) == 0:
self.abort_recipe_processing('No unread Pinboard bookmarks.')
successful_bookmark_reads = 0
articles = []
for bookmark in bookmarks:
try:
urllib.urlopen(bookmark['href'])
articles.append({
'title': bookmark['description'],
'url': bookmark['href'],
'description': bookmark['extended'],
'date': bookmark['time']})
if delete_bookmarks:
params = urllib.urlencode({'url': bookmark['href'], 'auth_token': apitoken})
urllib.request.urlopen('https://api.pinboard.in/v1/posts/delete?' + params)
else:
if processed_tag:
bookmark['tags'] += ' {}'.format(processed_tag)
# Mark bookmark as read by re-posting with 'toread' set to 'no'.
params = urllib.urlencode({
'url': bookmark['href'], 'description': bookmark['description'],
'extended': bookmark['extended'], 'tags': bookmark['tags'],
'dt': bookmark['time'], 'shared': bookmark['shared'],
'replace': 'yes', 'toread': 'no', 'auth_token': apitoken})
urllib.urlopen('https://api.pinboard.in/v1/posts/add?' + params)
successful_bookmark_reads += 1
if max_articles and successful_bookmark_reads >= max_articles:
break
except IOError:
continue
return [('Pinboard Bookmarks', articles)]