This repository has been archived by the owner on Feb 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
app.py
203 lines (156 loc) · 5.74 KB
/
app.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
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
from __future__ import division
from future.utils import raise_from
import math
import time
from datetime import datetime
from operator import itemgetter
from pocket import (
Pocket,
PocketException,
PocketAutException
)
from progress.spinner import Spinner
from .config import Configs
from .exceptions import AppException, AppNotConfigured
from .storage import Storage
class PocketApp:
DEFAULT_WORDS_PER_MINUTE = 180
REDIRECT_URL = 'http://www.google.com'
def __init__(self):
self._configs = Configs()
self._storage = Storage()
self._pocket = Pocket(
self._configs.get('consumer_key'),
self._configs.get('access_token')
)
def configure(self, consumer_key, access_token,
words_per_minute, sort_field):
self._configs.set('consumer_key', consumer_key)
self._configs.set('access_token', access_token)
self._configs.set('words_per_minute', words_per_minute)
self._configs.set('sort_field', sort_field)
self._configs.set('last_fetch', 0)
self._configs.write()
self._storage.clear()
self._pocket = Pocket(
consumer_key,
access_token
)
def init_consumer_key(self, consumer_key):
self._pocket = Pocket(consumer_key)
def get_request_token(self):
return self._pocket.get_request_token(
self.REDIRECT_URL
)
def get_access_token(self, request_token):
return self._pocket.get_access_token(
request_token
)
def add_article(self, url, title=None, tags=None):
if isinstance(tags, tuple):
tags = ','.join(list(tags))
try:
return self._pocket.add(url, title, tags)
except PocketException as e:
raise_from(self._check_exception(e), e)
def get_articles(self, limit=None, order=None):
if self._storage.is_empty():
self.fetch_articles(True)
articles = self._storage.read(limit, order)
sort_field = self._configs.get('sort_field')
if not sort_field:
sort_field = 'reading_time'
articles = sorted(articles,
key=itemgetter(sort_field))
return articles
def search(self, search, state, tag, sort):
try:
articles = self._pocket.retrieve(search=search,
state=state,
tag=tag,
sort=sort)
return self._get_articles_index(articles)
except PocketException as e:
raise_from(self._check_exception(e), e)
def archive_article(self, item_id):
try:
self._pocket.archive(int(item_id)).commit()
except PocketException as e:
raise_from(self._check_exception(e), e)
def find_article(self, item_id):
index = self._storage.read()
for article in index:
if str(article['id']) == str(item_id):
return article
return None
def fetch_articles(self, output_progress=False):
spinner = None
if output_progress:
spinner = Spinner('Loading articles ')
articles_index = []
last_fetch = self._configs.get('last_fetch')
offset = 0
count = 20
while(True):
try:
articles = self._pocket.retrieve(
state='unread',
count=count,
offset=offset,
since=last_fetch
)
except PocketException as e:
spinner.finish()
raise_from(self._check_exception(e), e)
if not articles['list']:
break
articles_index.extend(self._get_articles_index(articles))
offset += count
if spinner:
spinner.next()
if spinner:
spinner.finish()
sort_field = self._configs.get('sort_field')
if not sort_field:
sort_field = 'reading_time'
articles_index = sorted(articles_index,
key=itemgetter(sort_field))
self._storage.write(articles_index)
self._configs.set('last_fetch', self._get_timestamp(datetime.now()))
self._configs.write()
def _get_articles_index(self, articles):
wpm = self._configs.get('words_per_minute')
if not wpm:
wpm = self.DEFAULT_WORDS_PER_MINUTE
wpm = int(wpm)
articles_index = []
articles_list = articles['list']
if isinstance(articles_list, list) and len(articles_list) == 0:
return articles_index
for article in articles_list.values():
word_count = int(article.get('word_count', 0))
if word_count == 0:
reading_time = -1
else:
reading_time = int(math.ceil(word_count / wpm))
title = article.get('resolved_title', None)
if not title:
title = article['given_title']
url = article.get('resolved_url', None)
if not url:
url = article['given_url']
index = {
'id': article['item_id'],
'title': title,
'url': url,
'word_count': word_count,
'reading_time': reading_time
}
articles_index.append(index)
return articles_index
def _get_timestamp(self, date):
return int(time.mktime(date.timetuple()))
def _check_exception(self, e):
if isinstance(e, PocketAutException):
raise AppNotConfigured('Application is not configured')
raise AppException(e.message)