66import argparse
77import copy
88import logging
9+ import os
910import random
1011import sys
1112import time
2021
2122# constants
2223CONFIG_PATH = './config.yml'
24+ USER_CONFIG_PATH = './config.user.yml'
2325ORDINARY_SLEEP_SEC = 10
2426SUNBW_SLEEP_SEC = 5
2527REVIEW_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