Skip to content

Commit

Permalink
Update processing to compare time, add completion filter
Browse files Browse the repository at this point in the history
  • Loading branch information
0x16c3 committed Apr 22, 2022
1 parent 8537351 commit 14c8c9a
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 37 deletions.
26 changes: 23 additions & 3 deletions cogs/api/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async def channel_insert(self, items: List[Any]) -> None:
loop = asyncio.get_event_loop()

for item in items:
for status in ["progress", "planning", "dropped", "paused"]:
for status in ["progress", "completion", "planning", "dropped", "paused"]:
if not f"list_block_{status}" in item:
item[f"list_block_{status}"] = False

Expand All @@ -58,7 +58,7 @@ async def channel_insert(self, items: List[Any]) -> None:
async def channel_update(self, id, item: Any) -> None:
loop = asyncio.get_event_loop()

for status in ["progress", "planning", "dropped", "paused"]:
for status in ["progress", "completion", "planning", "dropped", "paused"]:
if not f"list_block_{status}" in item:
item[f"list_block_{status}"] = False

Expand All @@ -74,10 +74,30 @@ async def channel_update(self, id, item: Any) -> None:

return True

async def channel_repair(self, id) -> None:
loop = asyncio.get_event_loop()
channel = self.channels.get(where("channel") == int(id))

for status in ["progress", "completion", "planning", "dropped", "paused"]:
if not f"list_block_{status}" in channel:
channel[f"list_block_{status}"] = False

try:
await loop.run_in_executor(
None,
self.channels.update,
channel,
where("channel") == id,
)
except:
return False

return True

async def _channel_remove(self, item: Any) -> bool:
loop = asyncio.get_event_loop()

for status in ["progress", "planning", "dropped", "paused"]:
for status in ["progress", "completion", "planning", "dropped", "paused"]:
if not f"list_block_{status}" in item:
item[f"list_block_{status}"] = False

Expand Down
20 changes: 9 additions & 11 deletions cogs/api/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ async def send_embed(
channel: discord.TextChannel = None,
filter_adult: bool = True,
activity=None,
) -> Optional[discord.Embed]:
) -> Optional[Union[discord.Embed, Tuple[str, list, discord.Embed]]]:

item_idx = -1
if activity:
Expand All @@ -391,17 +391,15 @@ async def send_embed(
ch = matching[0]

# progress
if (
listitem.status
in [
"CURRENT",
"REPEATING",
"COMPLETED",
]
or item.status in ["REWATCHED", "REREAD"]
):
if listitem.status in [
"CURRENT",
"REPEATING",
] or item.status in ["REWATCHED", "REREAD"]:
if ch["list_block_progress"]:
return None
elif listitem.status in ["COMPLETED"]:
if ch["list_block_completion"]:
return None
elif listitem.status == "PAUSED":
if ch["list_block_paused"]:
return None
Expand Down Expand Up @@ -585,7 +583,7 @@ async def send_embed(
try:
await feed.sent_message.delete()
except Exception as e:
logger.info(
logger.debug(
f"Cannot remove message -> {str(channel.id)} : {item.username}\n{e}"
)

Expand Down
90 changes: 67 additions & 23 deletions cogs/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def __init__(self, username: str, userid: int, feed: int) -> None:
self.entries = []
self.entries_processed = []
self._init = False
self.reset = False

async def retrieve(
self,
Expand All @@ -91,7 +92,7 @@ async def retrieve(
"""Retrieves activity feed from AniList.
Returns:
List[Union[CListActivity, CTextActivity]],: First 15 activities.
List[Union[CListActivity, CTextActivity]],: First int(config["MEMORY_LIMIT"]) activities.
List[Union[CListActivity, CTextActivity]]]: Entire activity list.
"""
try:
Expand Down Expand Up @@ -125,31 +126,33 @@ async def retrieve(
self.errors += 1
return [], []

return res[:15], res
return res[: int(config["MEMORY_LIMIT"])], res

async def update(self, feed: list) -> None:
async def update(self, feed: List[Union[CListActivity, CTextActivity]]) -> None:
"""Updates activity list.
Checks for new activities.
Args:
feed (list): Latest activity feed from AniList
feed (List[Union[CListActivity, CTextActivity]]): Latest activity feed from AniList
"""

if not self._init:
if not self._init or self.reset:
if not len(feed):
logger.debug("Empty feed " + str(self))
return

for item in feed:
self.entries_processed.append(item)

self._init = True
if not self.reset:
logger.info("Initialized " + str(self))
logger.debug(
"\n List:\n "
+ "\n ".join(str(x.id) for x in self.entries_processed)
)

logger.info("Initialized " + str(self))
logger.debug(
"\n List:\n "
+ "\n ".join(str(x.id) for x in self.entries_processed)
)
self._init = True
self.reset = False
return

for item in feed:
Expand All @@ -158,8 +161,21 @@ async def update(self, feed: list) -> None:
if self.type == CListActivity:
if item.media.id in (i.media.id for i in self.entries):
continue
if item.media.id in (i.media.id for i in self.entries_processed):
# check date
first_occurence: CListActivity = next(
i for i in self.entries_processed if i.media.id == item.media.id
)

if item.date.timestamp < first_occurence.date.timestamp:
continue

if item.date.timestamp < self.entries_processed[-1].date.timestamp:
continue

self.entries.insert(0, item)
self.entries = self.entries[: int(config["MEMORY_LIMIT"])]

logger.info("Added " + str(item.id))

async def move_item(self, item, func=None, **kwargs) -> bool:
Expand Down Expand Up @@ -202,8 +218,16 @@ async def move_item(self, item, func=None, **kwargs) -> bool:
self.entries_processed.insert(0, item)
elif self.type == CTextActivity:
self.entries_processed.insert(0, item)
else:
logger.info("Unknown type " + str(self.type))

self.entries.remove(item)
if item_old in self.entries:
self.entries.remove(item_old)

self.entries_processed = self.entries_processed[
: int(config["MEMORY_LIMIT"])
]

processed = True

Expand All @@ -230,10 +254,11 @@ async def process_entries(self, func, **kwargs) -> None:
if moved:
processed = True

"""
if processed and len(self.entries_processed) > int(config["MEMORY_LIMIT"]):
self.entries_processed = []
self.entries = []
self._init = False
self.reset = True
items, items_full = await self.retrieve()
if len(items) == 0:
Expand All @@ -244,6 +269,7 @@ async def process_entries(self, func, **kwargs) -> None:
logger.info("Reset " + str(self) + " - exceeded entry limit.")
return
"""

return

Expand Down Expand Up @@ -363,6 +389,10 @@ def __init__(self, client):
async def on_ready(self):
"""Loads saved feeds from the database."""

channels = await database.channel_get()
for ch in channels:
await database.channel_repair(ch["channel"])

items = await database.feed_get()

logger.info(
Expand Down Expand Up @@ -435,11 +465,12 @@ async def on_ready(self):

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

logger.info(f"Created Activity objects, waiting 60 seconds to fetch feeds.")
await asyncio.sleep(60)
if len(self.feeds) > 30:
logger.info(f"Created Activity objects, waiting 60 seconds to fetch feeds.")
await asyncio.sleep(60)

for i, user in enumerate(self.feeds):
user: Activity
Expand All @@ -457,7 +488,7 @@ async def on_ready(self):

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

logger.info(f"Loaded {len(self.feeds)}.")
Expand Down Expand Up @@ -491,7 +522,7 @@ async def process(self):

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

@cog_ext.cog_slash(
Expand Down Expand Up @@ -1000,6 +1031,7 @@ async def _filter(self, ctx: SlashContext):
current = {
"channel": ctx.channel.id,
"list_block_progress": False,
"list_block_completion": False,
"list_block_planning": False,
"list_block_dropped": False,
"list_block_paused": False,
Expand All @@ -1013,9 +1045,9 @@ async def _filter(self, ctx: SlashContext):
value=i,
default=not current[f"list_block_{i}"],
)
for i in ["progress", "planning", "dropped", "paused"]
for i in ["progress", "completion", "planning", "dropped", "paused"]
],
max_values=4,
max_values=5,
placeholder="Filter enabled list activities for this channel.",
)
actionrow = create_actionrow(select)
Expand Down Expand Up @@ -1064,9 +1096,15 @@ def check_author(cctx: ComponentContext):
value=i,
default=not current[f"list_block_{i}"],
)
for i in ["progress", "planning", "dropped", "paused"]
for i in [
"progress",
"completion",
"planning",
"dropped",
"paused",
]
],
max_values=4,
max_values=5,
placeholder="Filter enabled list activities for this channel.",
)
actionrow = create_actionrow(select)
Expand All @@ -1083,9 +1121,15 @@ def check_author(cctx: ComponentContext):
value=i,
default=not current[f"list_block_{i}"],
)
for i in ["progress", "planning", "dropped", "paused"]
for i in [
"progress",
"completion",
"planning",
"dropped",
"paused",
]
],
max_values=4,
max_values=5,
placeholder="Filter enabled list activities for this channel.",
disabled=True,
)
Expand Down

0 comments on commit 14c8c9a

Please sign in to comment.