11import argparse
2- from src import make_extract , transform_article , load_news , init_database , create_news_tables
2+ from src import make_extract , transform_article_debug , transform_article_web , load_news , init_database , create_news_tables
33import logging
44
55logging .basicConfig (
88)
99logger = logging .getLogger (__name__ )
1010
11+
12+
1113def positive_int (value : str ) -> int :
1214 ivalue = int (value )
1315 if ivalue <= 0 :
@@ -39,20 +41,31 @@ def parse_args():
3941 )
4042 return parser .parse_args ()
4143
42- def pipeline () -> None :
43- args = parse_args ()
44- key_word = args .keyword
45- limit = args .limit
46- page_size = args .page_size
44+ def pipeline_for_web_user (user_id : int , search_request_id : int , key_word : str , limit : int , page_size : int , debug :bool = False ) -> int :
45+ if debug :
46+ args = parse_args ()
47+ key_word = args .keyword
48+ limit = args .limit
49+ page_size = args .page_size
50+ user_id = 1
51+ search_request_id = 1
4752 num_of_news = 0
4853 page = 1
4954 while num_of_news < limit :
5055 remaining = limit - num_of_news
51- raw_file_name ,raw_articles_count = make_extract (key_word , page , page_size )
56+ if debug :
57+ raw_file_name , raw_articles_count = make_extract (key_word , page , page_size )
58+ if raw_articles_count == 0 :
59+ logger .warning ("there is no more artical" )
60+ break
61+ clean_file_name = transform_article_debug (raw_file_name , key_word , page )
62+ result_num_of_news = load_news (clean_file_name , max_rows = remaining )
63+
64+ payload , raw_articles_count = make_extract (key_word , page , page_size )
5265 if raw_articles_count == 0 :
5366 logger .warning ("there is no more artical" )
5467 break
55- clean_file_name = transform_article ( raw_file_name , key_word , page )
68+ clean_file_name = transform_article_web ( payload , key_word , page )
5669 result_num_of_news = load_news (clean_file_name , max_rows = remaining )
5770
5871 num_of_news += result_num_of_news
@@ -71,6 +84,16 @@ def main()-> None:
7184 logger .exception ("pipeline failed: %s" , e )
7285 raise
7386
87+ def main2 (user_id , search_request_id , key_word )-> None :
88+ logger .info ("Starting pipeline, init database, build table.." )
89+ try :
90+ init_database ()
91+ create_news_tables ()
92+ loaded = pipeline_for_web_user (user_id ,search_request_id , key_word , 20 , 50 )
93+ logger .info ("Pipline finished. loaded rows: %s" , loaded )
94+ except Exception as e :
95+ logger .exception ("pipeline failed: %s" , e )
96+ raise
7497
7598
7699if __name__ == "__main__" :
0 commit comments