Added search
This commit is contained in:
parent
b439150973
commit
8a0d7ad080
2 changed files with 92 additions and 42 deletions
|
@ -72,6 +72,16 @@ class PocketApp:
|
|||
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 self._check_exception(e) from e
|
||||
|
||||
def archive_article(self, item_id):
|
||||
try:
|
||||
self._pocket.archive(int(item_id)).commit()
|
||||
|
@ -94,11 +104,6 @@ class PocketApp:
|
|||
|
||||
articles_index = []
|
||||
|
||||
wpm = self._configs.get('words_per_minute')
|
||||
if not wpm:
|
||||
wpm = self.DEFAULT_WORDS_PER_MINUTE
|
||||
wpm = int(wpm)
|
||||
|
||||
last_fetch = self._configs.get('last_fetch')
|
||||
|
||||
offset = 0
|
||||
|
@ -118,30 +123,7 @@ class PocketApp:
|
|||
if not articles['list']:
|
||||
break
|
||||
|
||||
for article in articles['list'].values():
|
||||
word_count = int(article['word_count'])
|
||||
if word_count == 0:
|
||||
reading_time = -1
|
||||
else:
|
||||
reading_time = math.ceil(word_count / wpm)
|
||||
|
||||
title = article['resolved_title']
|
||||
if not title:
|
||||
title = article['given_title']
|
||||
|
||||
url = article['resolved_url']
|
||||
if not url:
|
||||
url = article['given_url']
|
||||
|
||||
index = {
|
||||
'id': article['item_id'],
|
||||
'title': title,
|
||||
'url': url,
|
||||
'word_count': article['word_count'],
|
||||
'reading_time': reading_time
|
||||
}
|
||||
|
||||
articles_index.append(index)
|
||||
articles_index.extend(self._get_articles_index(articles))
|
||||
|
||||
offset += count
|
||||
if spinner:
|
||||
|
@ -161,6 +143,45 @@ class PocketApp:
|
|||
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['word_count'])
|
||||
if word_count == 0:
|
||||
reading_time = -1
|
||||
else:
|
||||
reading_time = math.ceil(word_count / wpm)
|
||||
|
||||
title = article['resolved_title']
|
||||
if not title:
|
||||
title = article['given_title']
|
||||
|
||||
url = article['resolved_url']
|
||||
if not url:
|
||||
url = article['given_url']
|
||||
|
||||
index = {
|
||||
'id': article['item_id'],
|
||||
'title': title,
|
||||
'url': url,
|
||||
'word_count': article['word_count'],
|
||||
'reading_time': reading_time
|
||||
}
|
||||
|
||||
articles_index.append(index)
|
||||
|
||||
return articles_index
|
||||
|
||||
def _get_timestamp(self, date):
|
||||
return int(time.mktime(date.timetuple()))
|
||||
|
||||
|
|
|
@ -92,20 +92,27 @@ def list_articles(limit, order):
|
|||
'run pocket-cli fetch to index your articles')
|
||||
return
|
||||
|
||||
try:
|
||||
pager = subprocess.Popen(['less'],
|
||||
stdin=subprocess.PIPE,
|
||||
stdout=sys.stdout)
|
||||
for article in articles:
|
||||
if int(article['reading_time']) <= 0:
|
||||
article['reading_time'] = 'Unknown'
|
||||
pager.stdin.write(
|
||||
bytearray(format_article(article, line=True), 'utf-8'))
|
||||
output_articles(articles)
|
||||
|
||||
pager.stdin.close()
|
||||
pager.wait()
|
||||
except (KeyboardInterrupt, ValueError):
|
||||
pass
|
||||
|
||||
@click.command()
|
||||
@click.argument('search')
|
||||
@click.option('--state', '-s',
|
||||
type=click.Choice(['unread', 'archive', 'all']),
|
||||
default='unread')
|
||||
@click.option('--tag', '-t')
|
||||
@click.option('--sort', '-o',
|
||||
type=click.Choice(['newest', 'oldest', 'title', 'site']),
|
||||
default='newest')
|
||||
def search(search, state, tag, sort):
|
||||
try:
|
||||
articles = pocket_app.search(search, state, tag, sort)
|
||||
except AppNotConfigured:
|
||||
app_not_configured()
|
||||
except AppException as e:
|
||||
exception_occured(e)
|
||||
|
||||
output_articles(articles)
|
||||
|
||||
|
||||
@click.command()
|
||||
|
@ -176,6 +183,27 @@ def archive_article(article_id):
|
|||
exception_occured(e)
|
||||
|
||||
|
||||
def output_articles(articles):
|
||||
if len(articles) == 0:
|
||||
print('No articles found')
|
||||
return
|
||||
|
||||
try:
|
||||
pager = subprocess.Popen(['less'],
|
||||
stdin=subprocess.PIPE,
|
||||
stdout=sys.stdout)
|
||||
for article in articles:
|
||||
if int(article['reading_time']) <= 0:
|
||||
article['reading_time'] = 'Unknown'
|
||||
pager.stdin.write(
|
||||
bytearray(format_article(article, line=True), 'utf-8'))
|
||||
|
||||
pager.stdin.close()
|
||||
pager.wait()
|
||||
except (KeyboardInterrupt, ValueError):
|
||||
pass
|
||||
|
||||
|
||||
def app_not_configured():
|
||||
print('App is not configured')
|
||||
print('Run `pocket-cli configure` to be able to use the app')
|
||||
|
@ -191,6 +219,7 @@ def exception_occured(exception):
|
|||
main.add_command(configure)
|
||||
main.add_command(add_article)
|
||||
main.add_command(list_articles)
|
||||
main.add_command(search)
|
||||
main.add_command(random_article)
|
||||
main.add_command(fetch)
|
||||
main.add_command(read)
|
||||
|
|
Loading…
Reference in a new issue