Skip to content

Commit fd79579

Browse files
code refactored
1 parent b93ac25 commit fd79579

File tree

1 file changed

+39
-43
lines changed

1 file changed

+39
-43
lines changed

src/tasks/schedulers.py

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,36 @@
2424

2525
FIVE_MINUTE = 300
2626

27-
DEFAULT_TWEETS: list[dict[str, str]] = [
28-
dict(
29-
status="""
30-
EOD Stock Market API
31-
32-
- Exchange & Ticker Data
33-
- (EOD) Stock Data
34-
- Fundamental Data
35-
- Stock Options And Splits Data
36-
- Financial News API
37-
- Social Media Trend Data For Stocks
38-
39-
Create A free API Key today
40-
https://eod-stock-api.site/plan-descriptions/basic
41-
""",
42-
media_ids=["1647575420009603073"]),
43-
dict(
44-
status="""
45-
Financial & Business News API
46-
47-
- Articles By UUID
48-
- Articles By Publishing Date
49-
- Articles By Stock Tickers
50-
- Articles By Exchange
51-
- Get List of Exchanges & Tickers
52-
- Get List of Publishers & Articles By Publisher
53-
54-
Create A free API Key today
55-
https://bit.ly/financial-business-news-api
56-
""",
57-
media_ids=["1647575420009603073"])
27+
28+
def compose_default_tweets(api_name, tweet_lines, media_ids=None):
29+
tweet_text = "\n".join(tweet_lines)
30+
tweet = {"status": f"{api_name}\n{tweet_text}"}
31+
if media_ids:
32+
tweet["media_ids"] = media_ids
33+
return tweet
34+
35+
36+
DEFAULT_TWEETS = [
37+
compose_default_tweets("EOD Stock Market API", [
38+
"- Exchange & Ticker Data",
39+
"- (EOD) Stock Data",
40+
"- Fundamental Data",
41+
"- Stock Options And Splits Data",
42+
"- Financial News API",
43+
"- Social Media Trend Data For Stocks",
44+
"Create A free API Key today",
45+
"https://eod-stock-api.site/plan-descriptions/basic"
46+
], media_ids=["1647575420009603073"]),
47+
compose_default_tweets("Financial & Business News API", [
48+
"- Articles By UUID",
49+
"- Articles By Publishing Date",
50+
"- Articles By Stock Tickers",
51+
"- Articles By Exchange",
52+
"- Get List of Exchanges & Tickers",
53+
"- Get List of Publishers & Articles By Publisher",
54+
"Create A free API Key today",
55+
"https://bit.ly/financial-business-news-api"
56+
], media_ids=["1647575420009603073"])
5857
]
5958

6059

@@ -96,6 +95,7 @@ async def send_tweet(self, tweet: dict[str, str]):
9695
return True
9796
except Forbidden as e:
9897
self._logger.error(f"Error updating status: {str(e)}")
98+
self._logger.info(f"Tweet that Caused the Errror : {tweet.get('status')}")
9999
return False
100100

101101
async def do_create_tweet(self, article: ArticleData) -> dict[str, str]:
@@ -105,16 +105,9 @@ async def do_create_tweet(self, article: ArticleData) -> dict[str, str]:
105105
internal_link: str = f"https://eod-stock-api.site/blog/financial-news/tweets/{article.uuid}"
106106

107107
# Create the tweet text with hashtags
108-
tweet_text: str = f"""Financial & Business News API
109-
{hashtags}
110-
- {article.title}
111-
{internal_link}
112-
"""
108+
tweet_text: str = f"Financial & Business News API\n{hashtags}\n- {article.title}\n{internal_link}"
113109
if len(tweet_text) > self._max_status_length:
114-
tweet_text = f"""Financial & Business News API
115-
- {article.title}
116-
{internal_link}
117-
"""
110+
tweet_text = f"Financial & Business News API\n- {article.title}\n{internal_link}"
118111

119112
if article.thumbnail.resolutions:
120113
_url: str = article.thumbnail.resolutions[0].url
@@ -149,7 +142,8 @@ async def create_tweets(self):
149142
tweet: dict[str, str] = await self.do_create_tweet(article=ArticleData(**article))
150143
self._logger.info(f"Tweet : {tweet}")
151144
await self._tweet_queue.put(tweet)
152-
except ValidationError:
145+
except ValidationError as e:
146+
self._logger.error(f"Error Creating Tweet: {str(e)}")
153147
pass
154148

155149
async def run(self):
@@ -166,9 +160,11 @@ async def run(self):
166160

167161
tweet: dict[str, str] | None = await self._tweet_queue.get()
168162
if tweet:
169-
while tweet and not await self.send_tweet(tweet=tweet):
163+
tweet_sent = await self.send_tweet(tweet=tweet)
164+
while not tweet_sent:
170165
await asyncio.sleep(delay=self._error_delay)
171-
tweet: str = await self._tweet_queue.get()
166+
tweet: dict[str, str] = await self._tweet_queue.get()
167+
tweet_sent = await self.send_tweet(tweet=tweet)
172168

173169
return None
174170

0 commit comments

Comments
 (0)