Skip to content

Commit 765b996

Browse files
committed
optimize command filter: more general articles selection method
1 parent f3f15fd commit 765b996

File tree

1 file changed

+25
-36
lines changed

1 file changed

+25
-36
lines changed

inoreader/main.py

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -287,65 +287,54 @@ def apply_action(articles, client, action, tags):
287287
def filter_articles(rules_file):
288288
"""Select articles and do something"""
289289
client = get_client()
290-
filters = []
291-
for rule in yaml.load(open(rules_file)):
292-
name = rule.get('name')
293-
folders = rule['folders']
294-
295-
fields = []
296-
# only 'title' or 'content' is supported now
297-
for field in rule.get('fields', ['title', 'content']):
298-
if field not in ('title', 'content'):
299-
continue
300-
fields.append(field)
290+
matched_articles = defaultdict(list)
291+
for rule in yaml.load(open(rules_file), Loader=yaml.Loader):
292+
fields = [
293+
field
294+
for field in rule.get('fields', ['title', 'content'])
295+
if field in ('title', 'content')
296+
]
301297
cur_filter = get_filter(rule['filter'])
302-
303298
actions = []
304299
# only 'mark_as_read', 'like', 'star', 'broadcast', 'tag' is supported now
305300
for action in rule.get('actions', [{'type': 'mark_as_read'}]):
306-
if action['type'] not in ('mark_as_read', 'like', 'star', 'broadcast', 'tag'):
301+
if action['type'] not in (
302+
'mark_as_read',
303+
'like',
304+
'star',
305+
'broadcast',
306+
'tag',
307+
'unstar',
308+
):
307309
continue
308310
actions.append(action)
309311

310-
filters.append(
311-
{
312-
'name': name,
313-
'folders': folders,
314-
'fields': fields,
315-
'filter': cur_filter,
316-
'actions': actions,
317-
}
318-
)
319-
320-
articles_by_foler = {} # folder -> articles
321-
matched_articles = defaultdict(list) # action -> articles
322-
for rule in filters:
323312
articles = []
324-
for folder in rule['folders']:
325-
if folder not in articles_by_foler:
326-
articles_by_foler[folder] = list(client.fetch_unread(folder=folder))
327-
328-
articles.extend(articles_by_foler[folder])
313+
if 'folders' in rule:
314+
for folder in rule['folders']:
315+
articles.extend(client.fetch_unread(folder=folder))
316+
else:
317+
for articles_info in rule.get('articles', []):
318+
articles.extend(client.fetch_articles(**articles_info))
329319

330320
# FIXME: deduplicate
331321
count = 0
332322
for article in articles:
333323
matched = False
334-
if 'title' in rule['fields'] and rule['filter'].validate(article.title):
324+
if 'title' in fields and cur_filter.validate(article.title):
335325
matched = True
336-
if 'content' in rule['fields'] and rule['filter'].validate(article.text):
326+
if 'content' in fields and cur_filter.validate(article.text):
337327
matched = True
338328

339329
if matched:
340-
for action in rule['actions']:
330+
for action in actions:
341331
matched_articles[action['type']].append((article, action))
342332

343333
count += 1
344334

345335
LOGGER.info(
346-
"matched %d articles in folder(s) %s with filter named '%s'",
336+
"matched %d articles with filter named '%s'",
347337
count,
348-
rule['folders'],
349338
rule['name'],
350339
)
351340

0 commit comments

Comments
 (0)