Skip to content

Commit

Permalink
More delays to prevent rate limits
Browse files Browse the repository at this point in the history
  • Loading branch information
0x16c3 committed Apr 18, 2022
1 parent 4aac57d commit 8ed5cae
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions cogs/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Feed:
Args:
username (str): Username of the profile
userid (int): User id of the profile
feed (int): Feed type
Attributes:
Expand All @@ -43,8 +44,10 @@ class Feed:
def get_type(i: any):
return list(Feed.TYPE.keys())[list(Feed.TYPE.values()).index(i)]

def __init__(self, username: str, feed: int) -> None:
def __init__(self, username: str, userid: int, feed: int) -> None:
self.username = username
self.userid = userid

self.feed = feed

self.function = None
Expand All @@ -57,21 +60,21 @@ def __init__(self, username: str, feed: int) -> None:
self.type = CListActivity
self.function = anilist.get_activity
self.arguments = {
"id": self.username,
"id": self.userid,
"content_type": "anime",
}
if self.feed == self.TYPE["MANGA"]:
self.type = CListActivity
self.function = anilist.get_activity
self.arguments = {
"id": self.username,
"id": self.userid,
"content_type": "manga",
}
if self.feed == self.TYPE["TEXT"]:
self.type = CTextActivity
self.function = anilist.get_activity
self.arguments = {
"id": self.username,
"id": self.userid,
"content_type": "text",
}

Expand Down Expand Up @@ -266,16 +269,18 @@ class Activity:
def __init__(
self,
username: str,
userid: int,
channel: discord.TextChannel,
profile: CUser,
t: Union[str, int] = "ANIME",
) -> None:
self.username = username
self.userid = userid
self.channel = channel
self.profile = profile

self.type = Feed.TYPE[t] if isinstance(t, str) else t
self.feed = Feed(username, self.type)
self.feed = Feed(username, userid, self.type)

self.loop = None

Expand All @@ -294,7 +299,7 @@ async def create(
except:
return None

return Activity(username, channel, profile, t)
return Activity(username, profile.id, channel, profile, t)

async def get_feed(
self, feed: Feed = None
Expand Down Expand Up @@ -428,6 +433,13 @@ async def on_ready(self):

self.feeds.append(user)

# wait 60 seconds after every 90 activity to prevent rate limiting
if i % 90 == 0 and i >= 90:
logger.info(f"Waiting 60 seconds.")
await asyncio.sleep(60)

logger.info(f"Created Activity objects, waiting 60 seconds to fetch feeds.")

for i, user in enumerate(self.feeds):
user: Activity

Expand All @@ -442,8 +454,8 @@ async def on_ready(self):
activity=user,
)

# wait 60 seconds after every 30 feeds to prevent rate limiting
if i % 30 == 0 and i >= 30:
# wait 60 seconds after every 45 feeds to prevent rate limiting
if i % 45 == 0 and i >= 45:
logger.info(f"Waiting 60 seconds.")
await asyncio.sleep(60)

Expand Down Expand Up @@ -477,7 +489,7 @@ async def process(self):
)

# wait 60 seconds after every 30 feeds to prevent rate limiting
if i % 30 == 0 and i >= 30:
if i % 45 == 0 and i >= 45:
logger.info(f"Waiting 60 seconds.")
await asyncio.sleep(60)

Expand Down

0 comments on commit 8ed5cae

Please sign in to comment.