Skip to content

Commit

Permalink
switch to loguru for logging, attempt to fix duplicate activities
Browse files Browse the repository at this point in the history
  • Loading branch information
0x16c3 committed Jun 23, 2022
1 parent 9d4d4fd commit 327828a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 49 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ __pycache__
.editorconfig
config.ini

venv
tmp
logs

cogs/_guild.json
cogs/_user.json
Expand Down
47 changes: 30 additions & 17 deletions cogs/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from .api.database import database
from .api.types import CCharacter, CUser, CAnime, CManga, CListActivity, CTextActivity
from typing import Union
from loguru import logger
import sys


Expand All @@ -44,11 +45,11 @@ class Feed:
def get_type(i: any):
return list(Feed.TYPE.keys())[list(Feed.TYPE.values()).index(i)]

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

self.feed = feed
self.feed = type

self.function = None
self.arguments = None
Expand Down Expand Up @@ -118,7 +119,7 @@ async def retrieve(
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
logger.print(
logger.error(
"Error on {}: {}\n[STACK] {} {} {}".format(
self.username, e, exc_type, fname, exc_tb.tb_lineno
)
Expand Down Expand Up @@ -158,11 +159,12 @@ async def update(self, feed: List[Union[CListActivity, CTextActivity]]) -> None:
for item in feed:
if item.id in (i.id for i in self.entries_processed):
continue

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
)
Expand Down Expand Up @@ -208,7 +210,7 @@ async def move_item(self, item, func=None, **kwargs) -> bool:

exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
logger.print(
logger.error(
"Error running function on entry {}: {}\n[STACK] {} {} {}".format(
item.id, e, exc_type, fname, exc_tb.tb_lineno
)
Expand Down Expand Up @@ -273,6 +275,12 @@ async def process_entries(self, func, **kwargs) -> None:

return

def __repr__(self):
return "<Feed: {}:{}>".format(self.username, str(self.feed))

def __eq__(self, o: "Feed"):
return "<Feed: {}:{}>".format(self.username, str(self.feed)) == str(o)


class Activity:
"""Activity feed object to manage / process activities.
Expand Down Expand Up @@ -402,11 +410,11 @@ async def on_ready(self):
error_channel_ids = []

for i, item in enumerate(items):
channel: discord.TextChannel = self.client.get_channel(int(item["channel"]))

if item in self.feeds:
continue

channel: discord.TextChannel = self.client.get_channel(int(item["channel"]))

if not channel:
logger.debug(
f"Could not load <{item['username']}:{item['channel']}:{item['type']}>"
Expand Down Expand Up @@ -506,18 +514,23 @@ async def process(self):

await asyncio.sleep(int(config["INTERVAL"]))

for i, user in enumerate(self.feeds):
user: Activity
no_dupes = []
[no_dupes.append(x) for x in self.feeds if x not in no_dupes]

self.feeds = no_dupes

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

enable_filter = not user.channel.is_nsfw()
enable_filter = not activity.channel.is_nsfw()

await user.get_feed(user.feed)
await user.feed.process_entries(
user.feed.type.send_embed,
channel=user.channel,
await activity.get_feed(activity.feed)
await activity.feed.process_entries(
activity.feed.type.send_embed,
channel=activity.channel,
anilist=anilist,
filter_adult=enable_filter,
activity=user,
activity=activity,
)

# wait 60 seconds after every 30 feeds to prevent rate limiting
Expand Down Expand Up @@ -1245,7 +1258,7 @@ async def _get_profile(self, ctx: SlashContext, username: str, **kwargs):
return

embed = await profile.send_embed()
await ctx.send("Done!", embed=embed, hidden=send_message)
await ctx.send("", embed=embed, hidden=send_message)

@cog_ext.cog_slash(
name="search",
Expand Down Expand Up @@ -1411,7 +1424,7 @@ def check_author(cctx: ComponentContext):
)
actionrow = create_actionrow(select)
await message.edit(
content="Done!",
content=None,
components=[actionrow],
)
break
Expand Down
30 changes: 3 additions & 27 deletions cogs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,11 @@
from datetime import datetime
from typing import Dict, List, Optional
import configparser
from loguru import logger

# anilist
import anilist

"""
LOGGING
"""


class Log:
def __init__(self, debug: bool = False, info: bool = False) -> None:
self._debug = debug
self._info = info

def debug(self, msg):
if self._debug:
print("DEBUG: " + msg)

def info(self, msg):
if self._info or self._debug:
print("INFO: " + msg)

def print(self, msg):
print("LOG: " + msg)


logger = Log()
""""""


"""
COLORS
Expand Down Expand Up @@ -231,7 +207,7 @@ def rotate_hue(color: Tuple[int, int, int], angle) -> Tuple[int, int, int]:
cfgparser.read("tmp/config.ini", encoding="utf-8-sig")
config = cfgparser["DEFAULT"]

logger.print(
logger.info(
f"LOADED CONFIG:\n"
f' INTERVAL = {config["INTERVAL"]}\n'
f' MEMORY_LIMIT = {config["MEMORY_LIMIT"]}\n'
Expand All @@ -243,7 +219,7 @@ def get_debug_guild_id() -> Optional[List[int]]:
if not config["SLASH_TEST_GUILD"].isdecimal():
return None

if not logger._debug:
if os.environ["LOG_LEVEL"] != "DEBUG":
return None

if int(config["SLASH_TEST_GUILD"]) == -1:
Expand Down
31 changes: 27 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
from discord.ext import commands
from discord_slash import SlashCommand

import os, sys
import argparse

os.environ["LOG_LEVEL"] = "WARNING"

from cogs.client import client
from cogs.controller import Controller
from cogs.utils import *
from cogs.api.database import database

import os
import argparse
from loguru import logger



# create `/tmp`
if not os.path.exists("tmp"):
Expand Down Expand Up @@ -114,8 +120,25 @@ def str2bool(v):
)

args = parser.parse_args()
logger._debug = args.debug
logger._info = args.info

os.environ["LOG_LEVEL"] = "WARNING"

if args.info:
os.environ["LOG_LEVEL"] = "INFO"
elif args.debug:
os.environ["LOG_LEVEL"] = "DEBUG"

fmt = (
"<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green>"
" | "
"<cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan>"
" | "
"<level>{level: <8}</level>: <level>{message}</level>"
)

logger.remove()
logger.add(sys.stderr, format=fmt, level=os.environ["LOG_LEVEL"])
logger.add("logs/log_{time}.log", enqueue=True, rotation="12:00")

for extension in cogs:
if args.debug and extension == "cogs.error":
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ discord-py-slash-command==2.3.2
discord.py==1.7.1
git+https://github.com/0x16c3/python-anilist.git#egg=python-anilist
tinydb==4.4.0
pillow==8.3.1
pillow==9.1.1
loguru==0.6.0

0 comments on commit 327828a

Please sign in to comment.