Skip to content

Commit

Permalink
add some improvements for discord log and final log
Browse files Browse the repository at this point in the history
  • Loading branch information
datawhores committed May 26, 2024
1 parent c9c21b4 commit dbd03de
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 55 deletions.
2 changes: 1 addition & 1 deletion ofscraper/commands/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def process_item():
try:
row, key = table.row_queue.get()
except Exception as E:
log.error(f"Error getting item from queue: {E}")
log.debug(f"Error getting item from queue: {E}")
return
for count, _ in enumerate(range(0, 2)):
try:
Expand Down
8 changes: 6 additions & 2 deletions ofscraper/commands/helpers/context.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import contextlib
import logging
import traceback
from itertools import chain



import ofscraper.models.selector as selector
import ofscraper.utils.args.helpers.areas as areas
Expand Down Expand Up @@ -45,7 +47,9 @@ async def wrapper(userdata,session,*args, **kwargs):
raise e
finally:
progress_utils.increment_user_activity()
log.warning(data)
for record in chain.from_iterable(data):
log.warning(record)

return wrapper


Expand Down
17 changes: 9 additions & 8 deletions ofscraper/commands/scraper/actions/like.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
import ofscraper.utils.constants as constants
import ofscraper.utils.context.exit as exit
import ofscraper.utils.live.screens as progress_utils
import ofscraper.api.profile as profile
from rich.markup import escape
from rich.text import Text


log = logging.getLogger("shared")
Expand Down Expand Up @@ -133,17 +134,17 @@ def _like(model_id, username,ids: list, like_action: bool):
else:
sleep_duration = stable_sleep_duration
if out == 1:
progress_utils.increment_like_Ftask(task2)
progress_utils.increment_like_task(task2)
progress_utils.increment_like_task(task)
time.sleep(sleep_duration)
out="[{username}] {post} post checked, {liked} post changes to {action}".format(post=progress_utils.get_like_task(task2).completed,liked=progress_utils.get_like_task(task).completed,
username = username,
action="liked" if like_action else "unliked"
)
title="Liked" if like_action else "Unliked"
liked=progress_utils.get_like_task(task2).completed
post=progress_utils.get_like_task(task).completed
action=title.lower()
text_out=f"[bold]\\[{username}][/bold] [bold]\\[Action {title}][/bold] ({post} post checked, {liked} post changes to {action})"
progress_utils.remove_like_task(task)
progress_utils.remove_like_task(task2)
return out

return text_out

def _toggle_like_requests(c, id, model_id):

Expand Down
11 changes: 5 additions & 6 deletions ofscraper/download/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import ofscraper.utils.system.system as system
from ofscraper.download.shared.utils.text import textDownloader
from ofscraper.utils.context.run_async import run
from ofscraper.download.shared.utils.log import empty_log


@run
Expand All @@ -26,10 +27,11 @@ async def download_process(username, model_id, medialist, posts=None):

async def download_picker(username, model_id, medialist):
if len(medialist) == 0:
out=empty_log(username)
logging.getLogger("shared").error(
f"[bold]{username}[/bold] ({0} photos, {0} videos, {0} audios, {0} skipped, {0} failed)"
out
)
return 0, 0, 0, 0, 0
return out
elif (
system.getcpu_count() > 1
and (
Expand Down Expand Up @@ -71,7 +73,4 @@ def download_post_process(username, model_id, medialist, postlist):
log.traceback_(traceback.format_exc())


def empty_log(username):
logging.getLogger("shared").error(
f"[bold]{username}[/bold] ({0} photos, {0} videos, {0} audios, {0} skipped, {0} failed)"
)

10 changes: 2 additions & 8 deletions ofscraper/download/downloadbatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
get_medialog,
subProcessVariableInit,
)
from ofscraper.download.shared.utils.log import final_log, log_download_progress
from ofscraper.download.shared.utils.log import final_log, final_log_text ,log_download_progress
from ofscraper.download.shared.utils.metadata import metadata
from ofscraper.download.shared.utils.paths import addGlobalDir, setDirectoriesDate
from ofscraper.download.shared.utils.progress import convert_num_bytes
Expand Down Expand Up @@ -199,13 +199,7 @@ def process_dicts(username, model_id, filtered_medialist):
with exit.DelayedKeyboardInterrupt():
raise E
final_log(username)
return (
common_globals.photo_count,
common_globals.video_count,
common_globals.audio_count,
common_globals.forced_skipped,
common_globals.skipped,
)
return final_log_text(username)


def queue_process(pipe_, task1, total):
Expand Down
11 changes: 3 additions & 8 deletions ofscraper/download/downloadnormal.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from ofscraper.download.main_download import main_download
from ofscraper.download.shared.classes.session import download_session
from ofscraper.download.shared.common.general import get_medialog
from ofscraper.download.shared.utils.log import final_log, log_download_progress
from ofscraper.download.shared.utils.log import final_log, final_log_text,log_download_progress
from ofscraper.download.shared.utils.metadata import metadata
from ofscraper.download.shared.utils.paths import setDirectoriesDate
from ofscraper.download.shared.utils.progress import convert_num_bytes
Expand Down Expand Up @@ -148,13 +148,8 @@ async def process_dicts(username, model_id, medialist):
log_thread.join()
other_thread.join() if other_thread else None
final_log(username, log=logging.getLogger("shared"))
return (
common_globals.photo_count,
common_globals.video_count,
common_globals.audio_count,
common_globals.forced_skipped,
common_globals.skipped,
)
return final_log_text(username)


except Exception as E:
with exit.DelayedKeyboardInterrupt():
Expand Down
31 changes: 20 additions & 11 deletions ofscraper/download/shared/utils/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,30 @@ def log_download_progress(media_type):


def final_log(username, log=None):
(log or common_globals.log).error(
final_log_text(username)
)

def final_log_text(username):

if read_args.retriveArgs().metadata:
skipped_word = "media metadata unchanged"
(log or common_globals.log).error(
f"[bold]{username}[/bold] ({format_size(common_globals.total_bytes )}) ({common_globals.photo_count+common_globals.audio_count+common_globals.video_count}"
f" downloads total [{common_globals.video_count} videos, {common_globals.audio_count} audios, {common_globals.photo_count} photos], "
f"{common_globals.forced_skipped} {skipped_word}, {common_globals.skipped} failed)"
)
return (f"[bold]\\[{username}][/bold] [bold]\\[Action Download][/bold] ({format_size(common_globals.total_bytes )}) ({common_globals.photo_count+common_globals.audio_count+common_globals.video_count}"
f" downloads total [{common_globals.video_count} videos, {common_globals.audio_count} audios, {common_globals.photo_count} photos], "
f"{common_globals.forced_skipped} {skipped_word}, {common_globals.skipped} failed)")
else:
skipped_word = "skipped"
(log or common_globals.log).error(
f"[bold]{username}[/bold] ({format_size(common_globals.total_bytes )}) ({common_globals.photo_count+common_globals.audio_count+common_globals.video_count}"
f" downloads total [{common_globals.video_count} videos, {common_globals.audio_count} audios, {common_globals.photo_count} photos], "
f"{common_globals.forced_skipped} {skipped_word}, {common_globals.skipped} failed)"
)
return (f"[bold]\\[{username}][/bold] [bold]\\[Action Download][/bold] ({format_size(common_globals.total_bytes )}) ({common_globals.photo_count+common_globals.audio_count+common_globals.video_count}"
f" downloads total [{common_globals.video_count} videos, {common_globals.audio_count} audios, {common_globals.photo_count} photos], "
f"{common_globals.forced_skipped} {skipped_word}, {common_globals.skipped} failed)")


def empty_log(username):
return f"[bold]\\[{username}][/bold] [bold]\\[Action Download][/bold] ({0} photos, {0} videos, {0} audios, {0} skipped, {0} failed)"







def text_log(username, value=0, fails=0, exists=0, log=None):
Expand Down
4 changes: 2 additions & 2 deletions ofscraper/utils/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ def get_console():
def get_shared_console():
global shared_console
if not shared_console:
shared_console = Console(theme=theme, quiet=quiet)
shared_console = Console(theme=theme, quiet=quiet,markup=True)

return shared_console


def get_other_console():
global other_console
if not other_console:
other_console = Console(theme=theme)
other_console = Console(theme=theme,markup=True)
return other_console


Expand Down
17 changes: 14 additions & 3 deletions ofscraper/utils/logs/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import ofscraper.utils.constants as constants
import ofscraper.utils.dates as dates_manager
import ofscraper.utils.logs.helpers as helpers
from rich.text import Text


class PipeHandler(logging.Handler):
Expand Down Expand Up @@ -163,7 +164,6 @@ def emit(self, record):
self._url = record
return
log_entry = self.format(record)
log_entry = re.sub("\[bold\]|\[/bold\]", "**", log_entry)
log_entry = f"{log_entry}\n\n"
if constants.getattr("DISCORD_ASYNC"):
self._tasks.append(self.loop.create_task(self._async_emit(log_entry)))
Expand Down Expand Up @@ -261,11 +261,22 @@ def format(self, record):
return self._filter(original)


class DiscordFormatter(SensitiveFormatter):
"""Formatter that removes sensitive information in logs."""

@staticmethod
def _filter(s):
t = SensitiveFormatter._filter(s)
s=re.sub("\\\\+","",t)
s=re.sub(r"\[(bold|/?bold(?:\s\w+\s\w+)?)\]","**",s)
return s



class LogFileFormatter(SensitiveFormatter):
"""Formatter that removes sensitive information in logs."""

@staticmethod
def _filter(s):
s = SensitiveFormatter._filter(s)
s = re.sub("\[bold\]|\[/bold\]", "", s)
return s
return Text.from_markup(Text(s).plain).plain
2 changes: 1 addition & 1 deletion ofscraper/utils/logs/other.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def init_other_logger(name):
# #discord
cord = log_class.DiscordHandler()
cord.setLevel(log_helpers.getLevel(read_args.retriveArgs().discord))
cord.setFormatter(log_class.SensitiveFormatter("%(message)s"))
cord.setFormatter(log_class.DiscordFormatter("%(message)s"))
# console
log.addHandler(cord)
if settings.get_log_level() != "OFF":
Expand Down
11 changes: 6 additions & 5 deletions ofscraper/utils/logs/stdout.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,16 @@ def logger_process(input_, name=None, stop_count=1, event=None):
# check for shutdown
if event and event.is_set():
break
if message == "None":
if hasattr(message, "message") and message.message!="None":
log.handle(message)
elif hasattr(message, "message") and message.message=="None":
count = count + 1
continue
if message.message == "None":
elif message != "None":
log.handle(message)
elif message == "None":
count = count + 1
continue
if message.message != "None":
# log the message
log.handle(message)
if count == stop_count:
break
except queue.Empty:
Expand Down

0 comments on commit dbd03de

Please sign in to comment.