Skip to content

Commit 991be39

Browse files
committed
limit fix
1 parent b33c552 commit 991be39

3 files changed

Lines changed: 16 additions & 10 deletions

File tree

main.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,14 @@ def pipeline() -> None:
5454
num_of_news = 0
5555
page = 1
5656
while num_of_news < limit:
57-
raw_file_name = make_extract(category, key_word, page, page_size)
58-
clean_file_name = transform_article(raw_file_name, category, key_word, page)
59-
result_num_of_news = load_news(clean_file_name)
60-
if result_num_of_news == 0:
57+
remaining = limit - num_of_news
58+
raw_file_name,raw_articles_count = make_extract(category, key_word, page, page_size)
59+
if raw_articles_count == 0:
6160
logger.warning("there is no more artical")
6261
break
62+
clean_file_name = transform_article(raw_file_name, category, key_word, page)
63+
result_num_of_news = load_news(clean_file_name, max_rows=remaining)
64+
6365
num_of_news += result_num_of_news
6466
page += 1
6567
logger.info(f"{num_of_news} news on category {category} already aploaded")

src/extract.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ def import_to_raw_json(data:dict[str, Any], category: str, key_word: str, page:
1515
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
1616
create_data = f"{timestamp}_{category}_{key_word}_page_{page}.json"
1717
file_path = raw_dir / create_data
18+
1819
with open(file_path, "w", encoding="utf-8") as f:
1920
json.dump(data, f, ensure_ascii=False, indent=2)
2021

2122
return create_data
2223

2324

2425

25-
def make_extract(category: str, key_word: str, page: int = 1, page_size: int = 100) -> str:
26+
def make_extract(category: str, key_word: str, page: int = 1, page_size: int = 100) -> tuple[str,int]:
2627
params = {
2728
"apiKey": settings.KEY_API,
2829
"country":settings.COUNTRY,
@@ -33,18 +34,18 @@ def make_extract(category: str, key_word: str, page: int = 1, page_size: int = 1
3334
}
3435
try:
3536
data = r.get(settings.NEWS_URL, params=params, timeout=15)
36-
data.raise_for_status()
3737
payload = data.json()
3838
logger.info(f"raise of status: {data.raise_for_status()}")
3939
payload["fetched_at"] = datetime.now().isoformat()
4040
payload["country"] = settings.COUNTRY
4141
payload["category"] = category
4242
payload["key_word"] = key_word
43-
if payload.get("totalResults", 0) == 0:
44-
logger.info("There no more articles")
43+
articles_count = len(payload.get("articles", []))
44+
if articles_count == 0:
45+
logger.info("There are no more articles")
4546

4647
new_file_name = import_to_raw_json(payload, category, key_word, page)
47-
return new_file_name
48+
return new_file_name, articles_count
4849

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

src/load.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
from pathlib import Path
33
import json
44
from config.config import settings
5+
from typing import Optional
56
BASE_DIR = (Path(__file__).resolve().parent.parent)/"data"/"clean"
67

7-
def load_news(clean_news:str)-> int:
8+
def load_news(clean_news:str, max_rows: Optional[int] = None)-> int:
89
LOAD_DIR = BASE_DIR/clean_news
910
num_of_news = 0
1011
with open(LOAD_DIR, 'r', encoding='utf-8') as f:
@@ -30,6 +31,8 @@ def load_news(clean_news:str)-> int:
3031

3132
with get_cursor(settings.db_news) as (conn, cur):
3233
for new in data:
34+
if max_rows is not None and num_of_news >= max_rows:
35+
break
3336
cur.execute(query,
3437
(new["country"],
3538
new["category"],

0 commit comments

Comments
 (0)