Skip to content

Commit c703d49

Browse files
authored
Merge pull request #10 from Linusp/dev
add new command `fetch-starred`
2 parents d7bbb3b + 104f800 commit c703d49

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

inoreader/client.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def __get_stream_contents(self, stream_id, continuation=''):
163163
else:
164164
return response['items'], None
165165

166-
def fetch_unread(self, folder=None, tags=None):
166+
def fetch_articles(self, folder=None, tags=None, unread=True, starred=False):
167167
self.check_token()
168168

169169
url = urljoin(BASE_URL, self.STREAM_CONTENTS_PATH)
@@ -172,7 +172,13 @@ def fetch_unread(self, folder=None, tags=None):
172172
url,
173173
quote_plus(self.GENERAL_TAG_TEMPLATE.format(folder))
174174
)
175-
params = {'xt': self.READ_TAG, 'c': str(uuid4())}
175+
176+
params = {'c': str(uuid4())}
177+
if unread:
178+
params['xt'] = self.READ_TAG
179+
180+
if starred:
181+
params['it'] = self.STARRED_TAG
176182

177183
response = self.parse_response(self.session.post(url, params=params, proxies=self.proxies))
178184
for data in response['items']:
@@ -182,6 +188,7 @@ def fetch_unread(self, folder=None, tags=None):
182188
])
183189
if tags and not categories.issuperset(set(tags)):
184190
continue
191+
185192
yield Article.from_json(data)
186193

187194
continuation = response.get('continuation')
@@ -198,8 +205,17 @@ def fetch_unread(self, folder=None, tags=None):
198205
if tags and not categories.issuperset(set(tags)):
199206
continue
200207
yield Article.from_json(data)
208+
201209
continuation = response.get('continuation')
202210

211+
def fetch_unread(self, folder=None, tags=None):
212+
for article in self.fetch_articles(folder=folder, tags=tags, unread=True):
213+
yield article
214+
215+
def fetch_starred(self, folder=None, tags=None):
216+
for article in self.fetch_articles(folder=folder, tags=tags, unread=False, starred=True):
217+
yield article
218+
203219
def add_general_label(self, articles, label):
204220
self.check_token()
205221

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)