This repository has been archived by the owner on Nov 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Pocket.recipe
255 lines (225 loc) · 8.84 KB
/
Pocket.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/usr/bin/env python2
# vim:ft=python tabstop=8 expandtab shiftwidth=4 softtabstop=4
from calibre.constants import config_dir
from calibre.utils.config import JSONConfig
from calibre.web.feeds.news import BasicNewsRecipe
from collections import namedtuple
from os import path
from time import localtime, strftime, time
import errno
import json
import mechanize
import operator
try:
from urllib.error import HTTPError
except ImportError:
from urllib2 import HTTPError
__license__ = 'GPL v3'
__copyright__ = '2019, David Orchard'
class PocketConfig:
__file_path = path.join(config_dir, 'custom_recipes', 'Pocket.json')
class AuthState:
FirstRun = 1
Authorizing = 2
Authorized = 3
def __init__(self, state = AuthState.FirstRun, token = None, user = None):
# Default values
self.state = state
self.token = token
self.user = user
@staticmethod
def from_file():
config = PocketConfig()
config.load()
return config
def load(self):
try:
with open(self.__file_path) as config:
config = json.load(config)
if isinstance(config, dict):
for key in self.__dict__.keys():
if config[key]:
setattr(self, key, config[key])
except IOError as e:
# File not found
if e.errno != errno.ENOENT:
raise e
def save(self):
with open(self.__file_path, 'w') as config:
json.dump(self.__dict__, config)
class Pocket(BasicNewsRecipe):
config = PocketConfig.from_file()
title = strftime('Pocket [%a, %d %b %Y]', localtime())
__author__ = 'David Orchard'
description = '''
Fetches articles saved with <a href="https://getpocket.com/">Pocket</a> and archives them.<br>
''' + ('''
Click <a href="https://getpocket.com/connected_applications">here</a>
to disconnect Calibre from the Pocket account "{}".
'''.format(config.user) if config.user else '''
Run 'Fetch News' with this source scheduled to initiate authentication with Pocket.
''')
publisher = 'Pocket.com'
category = 'info, custom, Pocket'
# User-configurable settings
oldest_article = 7
max_articles_per_feed = 100
archive_downloaded = True
series_name = 'Pocket'
# Inherited developer settings
auto_cleanup = True
no_stylesheets = True
use_embedded_content = False
ignore_duplicate_articles = {'url'}
# Custom developer settings
consumer_key = '87006-2ecad30a91903f54baf0ee05'
redirect_uri = 'https://calibre-ebook.com/'
base_url = 'https://app.getpocket.com'
to_archive = []
def first_run(self):
request = mechanize.Request("https://getpocket.com/v3/oauth/request",
(u'{{'
'"consumer_key":"{0}",'
'"redirect_uri":"{1}"'
'}}').format(
self.consumer_key,
self.redirect_uri
),
headers = {
'Content-Type': 'application/json; charset=UTF8',
'X-Accept': 'application/json'
}
)
response = self.browser.open(request)
response = json.load(response)
self.config = PocketConfig(
state = PocketConfig.AuthState.Authorizing,
token = response['code']
)
def authorize(self):
assert self.config.state == PocketConfig.AuthState.Authorizing, "Authorization process not yet begun"
assert self.config.token, "No request token"
request = mechanize.Request("https://getpocket.com/v3/oauth/authorize",
(u'{{'
'"consumer_key":"{0}",'
'"code":"{1}"'
'}}').format(
self.consumer_key,
self.config.token
),
headers = {
'Content-Type': 'application/json; charset=UTF8',
'X-Accept': 'application/json'
}
)
try:
response = self.browser.open(request)
response = json.load(response)
self.config = PocketConfig(
state = PocketConfig.AuthState.Authorized,
token = response["access_token"],
user = response["username"],
)
except HTTPError as e:
if e.code == 403:
# The code has already been used, or the user denied access
self.reauthorize()
raise e
def parse_index(self):
assert self.config.state == PocketConfig.AuthState.Authorized, "Not yet authorized"
assert self.config.token, "No access token"
request = mechanize.Request("https://getpocket.com/v3/get",
(u'{{'
'"consumer_key":"{0}",'
'"access_token":"{1}",'
'"count":"{2}",'
'"since":"{3}",'
'"state":"unread",'
'"detailType":"complete",'
'"sort":"newest"'
'}}').format(
self.consumer_key,
self.config.token,
self.max_articles_per_feed,
int(time()) - 86400 * self.oldest_article
),
headers = {
'Content-Type': 'application/json; charset=UTF8',
'X-Accept': 'application/json'
}
)
try:
response = self.browser.open(request)
response = json.load(response)
except HTTPError as e:
if e.code == 401:
# Calibre access has been removed
self.reauthorize()
raise e
if not response['list']:
self.abort_recipe_processing('No unread articles in the Pocket account "{}"'.format(self.config.user))
if self.archive_downloaded and response['list']:
self.to_archive = [item['item_id'] for item in response['list'].values()]
return [('Unread', [
{
'title': item['resolved_title'],
'url': item['resolved_url'],
'date': item['time_added'],
'description': item['excerpt'],
}
for item in sorted(response['list'].values(), key = lambda x: x['sort_id'])
] if response['list'] else [])]
def reauthorize(self):
self.config = PocketConfig();
self.ensure_authorization()
def ensure_authorization(self):
if self.config.state is PocketConfig.AuthState.FirstRun:
self.first_run()
self.config.save()
self.abort_recipe_processing('''
Calibre must be granted access to your Pocket account. Please click
<a href="https://getpocket.com/auth/authorize?request_token={0}&redirect_uri={1}">here</a>
to authenticate via a browser, and then re-fetch the news.
'''.format(self.config.token, self.redirect_uri))
elif self.config.state is PocketConfig.AuthState.Authorizing:
self.authorize()
self.config.save()
def get_browser(self, *args, **kwargs):
self.browser = BasicNewsRecipe.get_browser(self)
self.ensure_authorization()
return self.browser
def archive(self):
assert self.config.state == PocketConfig.AuthState.Authorized, "Not yet authorized"
assert self.config.token, "No access token"
if not self.to_archive:
return
archived_time = int(time())
request = mechanize.Request("https://getpocket.com/v3/send",
(u'{{'
'"consumer_key":"{0}",'
'"access_token":"{1}",'
'"actions":{2}'
'}}').format(
self.consumer_key,
self.config.token,
json.dumps([{
'action': 'archive',
'item_id': item_id,
'time': archived_time,
} for item_id in self.to_archive])
),
headers = {
'Content-Type': 'application/json; charset=UTF8',
'X-Accept': 'application/json'
}
)
response = self.browser.open(request)
def cleanup(self):
# If we're in another state, then downloading didn't complete
# (e.g. reauthorization needed) so there is no archiving to do
if self.config.state == PocketConfig.AuthState.Authorized:
self.archive()
# TODO: This works with EPUB, but not mobi/azw3
# BUG: https://bugs.launchpad.net/calibre/+bug/1838486
def postprocess_book(self, oeb, opts, log):
oeb.metadata.add('series', self.series_name)