Skip to content

Commit 9ad1a05

Browse files
committed
use logging instead of print in inoreader.main
1 parent fcb6f7d commit 9ad1a05

File tree

1 file changed

+56
-17
lines changed

1 file changed

+56
-17
lines changed

inoreader/main.py

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import sys
77
import json
88
import codecs
9-
from datetime import datetime
9+
import logging
10+
from logging.config import dictConfig
1011
from collections import defaultdict, Counter
1112
from configparser import ConfigParser
1213

@@ -23,6 +24,37 @@
2324
ENV_NAMES = [APPID_ENV_NAME, APPKEY_ENV_NAME, TOKEN_ENV_NAME]
2425

2526
CONFIG_FILE = os.path.join(os.environ.get('HOME'), '.inoreader')
27+
LOGGER = logging.getLogger(__name__)
28+
29+
30+
dictConfig({
31+
'version': 1,
32+
'formatters': {
33+
'simple': {
34+
'format': '%(asctime)s - %(filename)s:%(lineno)s: %(message)s',
35+
}
36+
},
37+
'handlers': {
38+
'default': {
39+
'level': 'DEBUG',
40+
'class': 'logging.StreamHandler',
41+
'formatter': 'simple',
42+
"stream": "ext://sys.stdout",
43+
},
44+
},
45+
'loggers': {
46+
'__main__': {
47+
'handlers': ['default'],
48+
'level': 'DEBUG',
49+
'propagate': False
50+
},
51+
'inoreader': {
52+
'handlers': ['default'],
53+
'level': 'DEBUG',
54+
'propagate': True
55+
},
56+
}
57+
})
2658

2759

2860
def read_config():
@@ -49,15 +81,15 @@ def get_client():
4981
config = read_config()
5082
appid, appkey = get_appid_key(config)
5183
if not appid or not appkey:
52-
print("'appid' or 'appkey' is missing")
84+
LOGGER.error("'appid' or 'appkey' is missing")
5385
sys.exit(1)
5486

5587
token = None
5688
if config.has_section('auth'):
5789
token = config.get('auth', 'token')
5890
token = token or os.environ.get(TOKEN_ENV_NAME)
5991
if not token:
60-
print("Please login first")
92+
LOGGER.error("Please login first")
6193
sys.exit(1)
6294

6395
userid = None
@@ -82,7 +114,7 @@ def login():
82114
password = input("PASSWORD: ").strip()
83115
status = client.login(username, password)
84116
if status:
85-
print("Login as '{}'".format(username))
117+
LOGGER.info("Login as '%s'", username)
86118
auth_token = client.auth_token
87119
config = read_config()
88120
if 'auth' in config:
@@ -95,9 +127,9 @@ def login():
95127
config['user'] = {'email': username, 'id': client.userinfo()['userId']}
96128
with codecs.open(CONFIG_FILE, mode='w', encoding='utf-8') as fconfig:
97129
config.write(fconfig)
98-
print("save token in {}, ".format(username, CONFIG_FILE))
130+
LOGGER.info("save token in config file '%s'", CONFIG_FILE)
99131
else:
100-
print("Login failed: Wrong username or password")
132+
LOGGER.info("Login failed: Wrong username or password")
101133
sys.exit(1)
102134

103135

@@ -137,7 +169,7 @@ def fetch_unread(folder, tags, outfile, out_format):
137169
writer = csv.writer(fout, delimiter=',') if out_format == 'csv' else None
138170
for idx, article in enumerate(client.fetch_unread(folder=folder, tags=tag_list)):
139171
if idx > 0 and (idx % 10) == 0:
140-
print("[{}] fetched {} articles".format(datetime.now(), idx))
172+
LOGGER.info("fetched %d articles", idx)
141173
title = article.title
142174
text = article.text
143175
if out_format == 'json':
@@ -155,7 +187,7 @@ def fetch_unread(folder, tags, outfile, out_format):
155187
print('* {}\n'.format(title), file=fout)
156188
print(text + '\n', file=fout)
157189

158-
print("[{}] fetched {} articles and saved them in {}".format(datetime.now(), idx + 1, outfile))
190+
LOGGER.info("fetched %d articles and saved them in %s", idx + 1, outfile)
159191

160192
fout.close()
161193

@@ -166,23 +198,23 @@ def apply_action(articles, client, action, tags):
166198
client.add_tag(articles, tag)
167199

168200
for article in articles:
169-
print("Add tags [{}] on article: {}".format(tags, article.title))
201+
LOGGER.info("Add tags [%s] on article: %s", tags, article.title)
170202
elif action == 'mark_as_read':
171203
client.mark_as_read(articles)
172204
for article in articles:
173-
print("Mark article as read: {}".format(article.title))
205+
LOGGER.info("Mark article as read: %s", article.title)
174206
elif action == 'like':
175207
client.mark_as_liked(articles)
176208
for article in articles:
177-
print("Mark article as liked: {}".format(article.title))
209+
LOGGER.info("Mark article as liked: %s", article.title)
178210
elif action == 'broadcast':
179211
client.broadcast(articles)
180212
for article in articles:
181-
print("Boradcast article: {}".format(article.title))
213+
LOGGER.info("Boradcast article: {}", article.title)
182214
elif action == 'star':
183215
client.mark_as_starred(articles)
184216
for article in articles:
185-
print("Starred article: {}".format(article.title))
217+
LOGGER.info("Starred article: {}", article.title)
186218

187219

188220
@main.command("filter")
@@ -242,8 +274,11 @@ def filter_articles(rules_file):
242274
matched_articles[action['type']].append((article, action))
243275

244276
count += 1
245-
print("[{}] matched {} articles with filter: {}".format(
246-
datetime.now(), count, rule['name']))
277+
278+
LOGGER.info(
279+
"matched %d articles in folder(s) %s with filter named '%s'",
280+
count, rule['folders'], rule['name']
281+
)
247282

248283
for action_name in matched_articles:
249284
articles, actions = zip(*matched_articles[action_name])
@@ -309,7 +344,7 @@ def fetch_articles(outfile, stream_id, out_format):
309344

310345
for idx, article in enumerate(client.get_stream_contents(stream_id)):
311346
if idx > 0 and (idx % 10) == 0:
312-
print("[{}] fetched {} articles".format(datetime.now(), idx))
347+
LOGGER.info("fetched %d articles", idx)
313348

314349
title = article.title
315350
text = article.text
@@ -328,7 +363,7 @@ def fetch_articles(outfile, stream_id, out_format):
328363
print('* {}\n'.format(title), file=fout)
329364
print(text + '\n', file=fout)
330365

331-
print("[{}] fetched {} articles and saved them in {}".format(datetime.now(), idx + 1, outfile))
366+
LOGGER.info("fetched %d articles and saved them in %s", idx + 1, outfile)
332367

333368
fout.close()
334369

@@ -342,6 +377,9 @@ def dedupe(folder, thresh):
342377
client = get_client()
343378
matched_articles, index = [], InvIndex()
344379
for idx, article in enumerate(client.fetch_unread(folder=folder)):
380+
if idx > 0 and (idx % 10) == 0:
381+
LOGGER.info("fetched %d articles and found %d duplicate", idx, len(matched_articles))
382+
345383
related = index.retrieve(article.title, k=10)
346384
sims = Counter()
347385
for docid, doc, _ in related:
@@ -359,6 +397,7 @@ def dedupe(folder, thresh):
359397

360398
index.add_doc(article)
361399

400+
LOGGER.info("fetched %d articles and found %d duplicate", idx + 1, len(matched_articles))
362401
apply_action(matched_articles, client, 'mark_as_read', None)
363402

364403

0 commit comments

Comments
 (0)