Skip to content

Commit 06006a4

Browse files
committed
add command fetch-articles
1 parent 74e98de commit 06006a4

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

inoreader/main.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,5 +289,48 @@ def get_subscriptions(outfile, folder, out_format):
289289
fout.close()
290290

291291

292+
@main.command("fetch-articles")
293+
@click.option("-i", "--stream-id", required=True, help='Stream ID which you want to fetch')
294+
@click.option("-o", "--outfile", required=True, help="Filename to save results")
295+
@click.option("--out-format",
296+
type=click.Choice(["json", "csv", 'plain', 'markdown', 'org-mode']),
297+
default="json",
298+
help="Format of output, default: json")
299+
def fetch_articles(outfile, stream_id, out_format):
300+
"""Fetch articles by stream id"""
301+
client = get_client()
302+
303+
fout = codecs.open(outfile, mode='w', encoding='utf-8')
304+
writer = None
305+
if out_format == 'csv':
306+
writer = csv.DictWriter(fout, ['title', 'content'], delimiter=',', quoting=csv.QUOTE_ALL)
307+
writer.writeheader()
308+
309+
for idx, article in enumerate(client.get_stream_contents(stream_id)):
310+
if idx > 0 and (idx % 10) == 0:
311+
print("[{}] fetched {} articles".format(datetime.now(), idx))
312+
313+
title = article.title
314+
text = article.text
315+
if out_format == 'json':
316+
print(json.dumps({'title': title, 'content': text}, ensure_ascii=False), file=fout)
317+
elif out_format == 'csv':
318+
writer.writerow({'title': title, 'content': text})
319+
elif out_format == 'plain':
320+
print('TITLE: {}'.format(title), file=fout)
321+
print("CONTENT: {}".format(text), file=fout)
322+
print(file=fout)
323+
elif out_format == 'markdown':
324+
print('# {}\n'.format(title), file=fout)
325+
print(text + '\n', file=fout)
326+
elif out_format == 'org-mode':
327+
print('* {}\n'.format(title), file=fout)
328+
print(text + '\n', file=fout)
329+
330+
print("[{}] fetched {} articles and saved them in {}".format(datetime.now(), idx + 1, outfile))
331+
332+
fout.close()
333+
334+
292335
if __name__ == '__main__':
293336
main()

0 commit comments

Comments
 (0)