Skip to content

Commit befa122

Browse files
authored
Merge pull request #19 from LittleYe233/main
Fix some logging code failed to work and refine log format
2 parents f8b6c17 + c870d72 commit befa122

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,6 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130+
131+
# user config
132+
config.user.yml

README.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,20 @@ cd jd_AutoComment
3636
pip install -r requirements.txt
3737
```
3838

39-
获取电脑版ck后填入 `config.yml` 文件:
39+
获取电脑版ck后填入配置文件。可以选择填入默认配置文件 `config.yml` ;也可以填入用户配置文件 `config.user.yml` (需要新建后将 `config.yml` 中的内容复制到该文件中),避免后续的更新覆盖 `config.yml` 中的内容。
40+
41+
需要填入如下内容:
42+
43+
```yml
44+
user:
45+
cookie: '<Cookie>'
46+
```
47+
48+
例如,若获取得到的ck为 `a=1; b=2; c=3` ,则配置文件中填入:
4049

4150
```yml
4251
user:
43-
cookie: ''
52+
cookie: 'a=1; b=2; c=3'
4453
```
4554

4655
最后运行 `auto_comment_plus.py` :
@@ -56,11 +65,15 @@ python3 auto_comment_plus.py
5665
本程序支持命令行参数:
5766

5867
```text
59-
usage: auto_comment_plus.py [-h] [--dry-run]
68+
usage: auto_comment_plus.py [-h] [--dry-run] [--log-level LOG_LEVEL] [-o LOG_FILE]
6069
6170
optional arguments:
62-
-h, --help show this help message and exit
63-
--dry-run have a full run without comment submission
71+
-h, --help show this help message and exit
72+
--dry-run have a full run without comment submission
73+
--log-level LOG_LEVEL
74+
specify logging level (default: info)
75+
-o LOG_FILE, --log-file LOG_FILE
76+
specify logging file
6477
```
6578

6679
**`-h`, `--help`:**
@@ -71,6 +84,16 @@ optional arguments:
7184

7285
完整地运行程序,但不实际提交评论。
7386

87+
**`--log-level LOG_LEVEL`:**
88+
89+
设置输出日志的等级。默认为 `INFO` 。可选等级为 `DEBUG`、`INFO`、`WARNING`、`ERROR` ,输出内容量依次递减。
90+
91+
**注意:** 若你需要提交 issue 来报告一个 bug ,请将该选项设置为 `DEBUG` 。
92+
93+
**`-o LOG_FILE`:**
94+
95+
设置输出日志文件的路径。若无此选项,则不输出到文件。
96+
7497
## 声明
7598

7699
本项目为Python学习交流的开源非营利项目,仅作为程序员之间相互学习交流之用。

auto_comment_plus.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import argparse
77
import copy
88
import logging
9+
import os
910
import random
1011
import sys
1112
import time
@@ -20,6 +21,7 @@
2021

2122
# constants
2223
CONFIG_PATH = './config.yml'
24+
USER_CONFIG_PATH = './config.user.yml'
2325
ORDINARY_SLEEP_SEC = 10
2426
SUNBW_SLEEP_SEC = 5
2527
REVIEW_SLEEP_SEC = 10
@@ -246,7 +248,7 @@ def ordinary(N, opts=None):
246248
url2 = "https://club.jd.com/myJdcomments/saveProductComment.action"
247249
opts['logger'].debug('URL: %s', url2)
248250
xing, Str = generation(oname, opts=opts)
249-
opts['logger'].info(f'\t\t评价内容,星级{xing}:', Str)
251+
opts['logger'].info(f'\t\t评价内容,星级{xing}:' + Str)
250252
data2 = {
251253
'orderId': oid,
252254
'productId': pid, # 商品id
@@ -260,7 +262,8 @@ def ordinary(N, opts=None):
260262
opts['logger'].debug('Sending comment request')
261263
pj2 = requests.post(url2, headers=headers, data=data2)
262264
else:
263-
opts['logger'].debug('Skipped sending comment request in dry run')
265+
opts['logger'].debug(
266+
'Skipped sending comment request in dry run')
264267
opts['logger'].debug('Sleep time (s): %.1f', ORDINARY_SLEEP_SEC)
265268
time.sleep(ORDINARY_SLEEP_SEC)
266269
idx += 1
@@ -519,7 +522,7 @@ def Service_rating(N, opts=None):
519522
pj1 = requests.post(url1, headers=headers, data=data1)
520523
else:
521524
opts['logger'].debug('Skipped sending comment request in dry run')
522-
opts['logger'].info("\t\t", pj1.text)
525+
opts['logger'].info("\t\t " + pj1.text)
523526
opts['logger'].debug('Sleep time (s): %.1f', SERVICE_RATING_SLEEP_SEC)
524527
time.sleep(SERVICE_RATING_SLEEP_SEC)
525528
N['服务评价'] -= 1
@@ -530,8 +533,8 @@ def No(opts=None):
530533
opts = opts or {}
531534
opts['logger'].info('')
532535
N = all_evaluate(opts)
533-
for i in N:
534-
opts['logger'].info('{} {}----'.format(i, N[i]))
536+
s = '----'.join(['{} {}'.format(i, N[i]) for i in N])
537+
opts['logger'].info(s)
535538
opts['logger'].info('')
536539
return N
537540

@@ -640,14 +643,21 @@ def main(opts=None):
640643
logger.debug('Options passed to functions: %s', opts)
641644
logger.debug('Builtin constants:')
642645
logger.debug(' CONFIG_PATH: %s', CONFIG_PATH)
646+
logger.debug(' USER_CONFIG_PATH: %s', USER_CONFIG_PATH)
643647
logger.debug(' ORDINARY_SLEEP_SEC: %s', ORDINARY_SLEEP_SEC)
644648
logger.debug(' SUNBW_SLEEP_SEC: %s', SUNBW_SLEEP_SEC)
645649
logger.debug(' REVIEW_SLEEP_SEC: %s', REVIEW_SLEEP_SEC)
646650
logger.debug(' SERVICE_RATING_SLEEP_SEC: %s', SERVICE_RATING_SLEEP_SEC)
647651

648652
# parse configurations
649653
logger.debug('Reading the configuration file')
650-
with open(CONFIG_PATH, 'r', encoding='utf-8') as f:
654+
if os.path.exists(USER_CONFIG_PATH):
655+
logger.debug('User configuration file exists')
656+
_cfg_path = USER_CONFIG_PATH
657+
else:
658+
logger.debug('User configuration file doesn\'t exist, fallback to the default one')
659+
_cfg_path = CONFIG_PATH
660+
with open(_cfg_path, 'r', encoding='utf-8') as f:
651661
cfg = yaml.safe_load(f)
652662
logger.debug('Closed the configuration file')
653663
logger.debug('Configurations in Python-dict format: %s', cfg)

0 commit comments

Comments
 (0)