Skip to content

Commit 104f800

Browse files
committed
add new command fetch-starred
1 parent 2bd7f52 commit 104f800

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

inoreader/main.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,5 +459,60 @@ def dedupe(folder, thresh):
459459
apply_action(matched_articles, client, 'mark_as_read', None)
460460

461461

462+
@main.command()
463+
@click.option("-f", "--folder", help='Folder which articles belong to')
464+
@click.option("-t", "--tags", help="Tag(s) for filtering, seprate with comma")
465+
@click.option("-o", "--outfile", required=True, help="Filename to save articles")
466+
@click.option("--out-format",
467+
type=click.Choice(['json', 'csv', 'plain', 'markdown', 'org-mode']),
468+
default='json',
469+
help='Format of output file, default: json')
470+
@catch_error
471+
def fetch_starred(folder, tags, outfile, out_format):
472+
"""Fetch starred articles"""
473+
client = get_client()
474+
475+
tag_list = [] if not tags else tags.split(',')
476+
fout = codecs.open(outfile, mode='w', encoding='utf-8')
477+
writer = csv.writer(fout, delimiter=',') if out_format == 'csv' else None
478+
for idx, article in enumerate(client.fetch_starred(folder=folder, tags=tag_list)):
479+
if idx > 0 and (idx % 10) == 0:
480+
LOGGER.info("fetched %d articles", idx)
481+
482+
title = article.title
483+
text = article.text
484+
link = article.link
485+
if out_format == 'json':
486+
print(json.dumps({'title': title, 'content': text, 'url': link}, ensure_ascii=False),
487+
file=fout)
488+
elif out_format == 'csv':
489+
writer.writerow([link, title, text])
490+
elif out_format == 'plain':
491+
print('TITLE: {}'.format(title), file=fout)
492+
print("LINK: {}".format(link), file=fout)
493+
print("CONTENT: {}".format(text), file=fout)
494+
print(file=fout)
495+
elif out_format == 'markdown':
496+
if link:
497+
print('# [{}]({})\n'.format(title, link), file=fout)
498+
else:
499+
print('# {}\n'.format(title), file=fout)
500+
501+
print(text + '\n', file=fout)
502+
elif out_format == 'org-mode':
503+
if link:
504+
title = title.replace('[', '_').replace(']', '_')
505+
print('* [[{}][{}]]\n'.format(link, title),
506+
file=fout)
507+
else:
508+
print('* {}\n'.format(title), file=fout)
509+
510+
print(text + '\n', file=fout)
511+
512+
LOGGER.info("fetched %d articles and saved them in %s", idx + 1, outfile)
513+
514+
fout.close()
515+
516+
462517
if __name__ == '__main__':
463518
main()

0 commit comments

Comments
 (0)