Skip to content

Commit 0c4843a

Browse files
refactored
1 parent a2e427b commit 0c4843a

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

src/models/models.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ class Sentiment(BaseModel):
1515
sentiment_title: None | str
1616
stock_codes: Optional[list[str]]
1717

18-
@classmethod
19-
@validator('stock_codes')
20-
def stock_codes(cls, value: list[str] | None):
21-
if value is None:
22-
return []
23-
return value
24-
18+
@validator('stock_codes', pre=True)
19+
def validate_stock_codes(cls, value):
20+
if not value:
21+
raise ValueError('invalid stock codes')
22+
if isinstance(value, str):
23+
return value.split(",")
24+
if isinstance(value, list):
25+
return value
2526

2627
class Resolution(BaseModel):
2728
url: str

src/tasks/schedulers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def compose_default_tweets(api_name, tweet_lines, media_ids=None):
7676

7777
class TaskScheduler:
7878
def __init__(self):
79-
self._run_counter = 0
79+
self._run_counter = 1
8080
self._tweepy_api = tweepy.API(auth=auth)
8181
self._article_queue = Queue()
8282
self._tweet_queue = Queue()
@@ -125,7 +125,7 @@ async def do_create_tweet(self, article: ArticleData) -> dict[str, str]:
125125
# Extract ticker symbols as hashtags
126126
if article.tickers:
127127
hashtags = ''.join(['#' + ticker for ticker in article.tickers])
128-
elif article.sentiment.stock_codes:
128+
elif article.sentiment and article.sentiment.stock_codes:
129129
_codes = article.sentiment.stock_codes
130130
hashtags = ''.join(['#' + ticker for ticker in _codes])
131131
else:
@@ -134,7 +134,7 @@ async def do_create_tweet(self, article: ArticleData) -> dict[str, str]:
134134
# Create the tweet text with hashtags
135135
_title: str = "Financial & Business News API"
136136
_crop_len: int = self._max_status_length - len(_title) - 6 - len(hashtags)
137-
tweet_body = f"{article.sentiment.article_tldr[: _crop_len]}" if article.sentiment.article_tldr else article.title
137+
tweet_body = f"{article.sentiment.article_tldr[: _crop_len]}" if article.sentiment and article.sentiment.article_tldr else article.title
138138

139139
tweet_text = f"{_title}\n-{tweet_body}...\n{hashtags}"
140140

0 commit comments

Comments
 (0)