66from apscheduler .schedulers .asyncio import AsyncIOScheduler
77from discord import TextChannel
88from loguru import logger
9+ from pragmail .exceptions import IMAP4Error
910from sqlalchemy .exc import NoResultFound
1011
1112from dayong .core .configs import DayongDynamicLoader
@@ -68,6 +69,8 @@ async def check_email_cred():
6869
6970@logger .catch
7071async def get_devto_article ():
72+ global job_running
73+
7174 task = get_devto_article .__name__
7275
7376 try :
@@ -80,7 +83,7 @@ async def get_devto_article():
8083 logger .info (f"{ task } is not scheduled to run" )
8184 return
8285
83- content = await rest .get_devto_article (sort_by_date = True )
86+ content = await rest .get_devto_article ()
8487 channel = await get_guild_channel (result .channel_name )
8588
8689 if not isinstance (channel , TextChannel ):
@@ -89,13 +92,16 @@ async def get_devto_article():
8992 logger .info (
9093 f"{ get_devto_article .__name__ } delivering content to: { result .channel_name } "
9194 )
95+
9296 for content in content .content :
9397 await channel .send (content )
9498 await asyncio .sleep (60 )
9599
96100
97101@logger .catch
98102async def get_medium_daily_digest ():
103+ global email , job_running
104+
99105 task = get_devto_article .__name__
100106 notsched = f"{ task } is not scheduled to run"
101107 xsession = f"{ task } cannot run. reason: no session started.\n ```{ info } ```"
@@ -118,12 +124,19 @@ async def get_medium_daily_digest():
118124 await del_schedule (table_model )
119125 return
120126
121- content = await email .get_medium_daily_digest ()
127+ try :
128+ content = await email .get_medium_daily_digest ()
129+ except IMAP4Error :
130+ email = EmailClient (
131+ CONFIG .imap_domain_name , CONFIG .email , CONFIG .email_password
132+ )
133+ content = await email .get_medium_daily_digest ()
122134
123135 logger .info (
124136 f"{ get_medium_daily_digest .__name__ } "
125137 "delivering content to: {result.channel_name}"
126138 )
139+
127140 for content in content .content :
128141 await channel .send (content )
129142 await asyncio .sleep (60 )
@@ -134,8 +147,8 @@ async def get_medium_daily_digest():
134147async def on_ready ():
135148 await check_email_cred ()
136149 scheduler = AsyncIOScheduler ()
137- scheduler .add_job (get_devto_article , "cron " , day_of_week = "mon-sun" , hour = 7 )
138- scheduler .add_job (get_medium_daily_digest , "cron " , day_of_week = "mon-sun" , hour = 7 )
150+ scheduler .add_job (get_devto_article , "interval " , hours = 24 )
151+ scheduler .add_job (get_medium_daily_digest , "interval " , hours = 24 )
139152 scheduler .start ()
140153
141154
0 commit comments