Skip to content

Commit 4366db8

Browse files
committed
multy client extract + main + transform
1 parent 7e36293 commit 4366db8

4 files changed

Lines changed: 52 additions & 15 deletions

File tree

main.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 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
33
import logging
44

55
logging.basicConfig(
@@ -8,6 +8,8 @@
88
)
99
logger = logging.getLogger(__name__)
1010

11+
12+
1113
def 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

7699
if __name__ == "__main__":

src/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from .extract import make_extract
2-
from .transform import transform_article
2+
from .transform import transform_article_web, transform_article_debug
33
from .load import load_news
44
from .db import init_database, create_news_tables

src/extract.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def import_to_raw_json(data:dict[str, Any], key_word: str, page: int) -> str:
2323

2424

2525

26-
def make_extract( key_word: str, page: int = 1, page_size: int = 100) -> tuple[str,int]:
26+
def make_extract( key_word: str, page: int = 1, page_size: int = 100, debug_mode: bool = False) -> tuple[dict,int]:
2727
params = {
2828
"apiKey": settings.KEY_API,
2929
"language":settings.langueage,
@@ -43,9 +43,10 @@ def make_extract( key_word: str, page: int = 1, page_size: int = 100) -> tuple[s
4343
articles_count = len(payload.get("articles", []))
4444
if articles_count == 0:
4545
logger.info("There are no more articles")
46-
47-
new_file_name = import_to_raw_json(payload, key_word, page)
48-
return new_file_name, articles_count
46+
if debug_mode:
47+
new_file_name = import_to_raw_json(payload, key_word, page)
48+
return new_file_name, articles_count
49+
return payload, articles_count
4950

5051
except r.exceptions.Timeout:
5152
logger.error("Error: NewsAPI reauest time out")

src/transform.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,12 @@ def clean_article(data) -> tuple[list[dict[str,str]], dict]:
6666
"url": element["url"],
6767
"published_at": element["publishedAt"],
6868
"fetched_at": data["fetched_at"],
69+
"source_name": element.get("source", {}).get("name")
6970
}
7071
)
7172
return clean_data, statistic
7273

73-
def transform_article(new_file_name:str, key_word: str, page: int) -> str:
74+
def transform_article_debug(new_file_name:str, key_word: str, page: int) -> str:
7475
extract_dir = BASE_DIR / "raw" / new_file_name
7576
with open(extract_dir, 'r', encoding='utf-8') as f:
7677
data = json.load(f)
@@ -96,4 +97,16 @@ def transform_article(new_file_name:str, key_word: str, page: int) -> str:
9697
logger.info("rejected_articles=%s", stats['rejected_articles'])
9798
logger.info("reasons_counts=%s", stats['reasons_counts'])
9899
logger.info("prime_reason=%s", stats['prime_reason'])
99-
return create_clean_data
100+
return create_clean_data
101+
102+
def transform_article_web(payload:dict)-> tuple[list[dict],dict]:
103+
logger.info(f"income: {len(payload.get('articles', []))} articals")
104+
clean, stats = clean_article(payload)
105+
logger.info(f"outcome: {len(clean)} articals")
106+
logger.info("stats collected")
107+
logger.info("income_articles=%s", stats['income_articles'])
108+
logger.info("accepted_articles=%s", stats['accepted_articles'])
109+
logger.info("rejected_articles=%s", stats['rejected_articles'])
110+
logger.info("reasons_counts=%s", stats['reasons_counts'])
111+
logger.info("prime_reason=%s", stats['prime_reason'])
112+
return clean, stats

0 commit comments

Comments
 (0)