Skip to content

Commit

Permalink
Merge pull request #11 from bbtufty/logger-updates
Browse files Browse the repository at this point in the history
Logging updates
  • Loading branch information
bbtufty authored Oct 23, 2024
2 parents 5aeb011 + 1d5b50e commit 0479fb7
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ body:
id: log
attributes:
label: Log
description: Attach the relevant log file(s)
description: Attach the relevant log file(s) (log/NXBrew.log), which will have sensitive info redacted
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ cython_debug/
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/

# Don't include cache/config
# Don't include cache/config/logs
config.yml
cache.json
log/
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
0.3 (Unreleased)
================

- Logging updates [#11]
- Update docs to reflect region preferences [#10]

0.2 (2024-10-20)
Expand Down
1 change: 1 addition & 0 deletions nxbrew_dl/configs/general.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dl_sites:
- "GoFile"
- "HexLoad"
- "MegaUp"
- "MixDrop"

dl_names:
Base Game:
Expand Down
4 changes: 2 additions & 2 deletions nxbrew_dl/gui/gui_nxbrew_dl.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from .gui_regions_languages import RegionLanguageWindow
from .gui_utils import (
open_url,
get_gui_logger,
add_row_to_table,
get_ordered_list,
)
Expand All @@ -32,6 +31,7 @@
from ..util import (
check_github_version,
get_game_dict,
NXBrewLogger,
load_yml,
save_yml,
load_json,
Expand Down Expand Up @@ -73,7 +73,7 @@ def __init__(self):
self.setWindowIcon(icon)

# Set up the logger
self.logger = get_gui_logger(log_level="INFO")
self.logger = NXBrewLogger(log_level="INFO")
self.logger.warning("Do not close this window!")

# Check for version updates
Expand Down
46 changes: 7 additions & 39 deletions nxbrew_dl/gui/gui_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import logging
import sys

import colorlog
from PySide6.QtCore import Slot, Qt
from PySide6.QtGui import QDesktopServices
from PySide6.QtWidgets import QListWidgetItem
Expand All @@ -17,9 +13,9 @@ def open_url(url):


def add_row_to_table(
table,
row_dict,
row_name_key="long_name",
table,
row_dict,
row_name_key="long_name",
):
"""Add row to table, using a dictionary of important info
Expand Down Expand Up @@ -69,9 +65,9 @@ def add_item_to_list(item_list, item_name, check_state=None):


def set_ordered_list(
list_widget,
item_dict,
items,
list_widget,
item_dict,
items,
):
"""Set checked items from an ordered list
Expand All @@ -97,7 +93,7 @@ def set_ordered_list(


def get_ordered_list(
list_widget,
list_widget,
):
"""Get checked items from an ordered list"""

Expand All @@ -116,31 +112,3 @@ def get_ordered_list(
items.append(item_name)

return items


def get_gui_logger(log_level="info"):
"""Get the logger for the GUI
Args:
log_level (str, optional): Logging level. Defaults to "info".
"""
logger = logging.getLogger()

# Set the log level based on the provided parameter
log_level = log_level.upper()
if log_level == "DEBUG":
logger.setLevel(logging.DEBUG)
elif log_level == "INFO":
logger.setLevel(logging.INFO)
elif log_level == "CRITICAL":
logger.setLevel(logging.CRITICAL)
else:
logger.critical(f"Invalid log level '{log_level}', defaulting to 'INFO'")
logger.setLevel(logging.INFO)
log_handler = colorlog.StreamHandler(sys.stdout)
log_handler.setFormatter(
colorlog.ColoredFormatter("%(log_color)s%(levelname)s: %(message)s")
)
logger.addHandler(log_handler)

return logger
20 changes: 17 additions & 3 deletions nxbrew_dl/nxbrew_dl/nxbrew.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from pathvalidate import sanitize_filename

import nxbrew_dl
from ..gui.gui_utils import get_gui_logger
from ..util import (
NXBrewLogger,
discord_push,
load_yml,
load_json,
Expand Down Expand Up @@ -132,7 +132,7 @@ def __init__(
self.user_cache_file = user_cache_file

if logger is None:
logger = get_gui_logger(log_level="INFO")
logger = NXBrewLogger(log_level="INFO")
self.logger = logger

# Set up JDownloader
Expand All @@ -143,6 +143,10 @@ def __init__(
jd.connect(self.user_config["jd_user"], self.user_config["jd_pass"])

jd_device_name = self.user_config["jd_device"]

# Redact the device name
self.logger.update_redact_filter(jd_device_name)

self.logger.info(f"Connecting to device {jd_device_name}")
self.jd_device = jd.get_device(jd_device_name)

Expand Down Expand Up @@ -285,9 +289,12 @@ def download_game(

for dl_site in dl_sites:
if dl_site in release_dl:
# print(release_dl[dl_site])
self.logger.info(f"\t\t\t{dl_site}:")
for dl_link in release_dl[dl_site]:

# Redact the DL link
self.logger.update_redact_filter(dl_link)

self.logger.info(f"\t\t\t- {dl_link}")

self.logger.info("")
Expand Down Expand Up @@ -536,6 +543,10 @@ def run_jdownloader(
self.logger.info(f"\t\tTrying {dl_site}:")

for d in dl_links:

# Redact the link
self.logger.update_redact_filter(d)

self.logger.info(f"\t\t\tLink: {d}")
if "ouo" in d:
self.logger.info(
Expand All @@ -545,6 +556,9 @@ def run_jdownloader(
else:
d_final = copy.deepcopy(d)

# Redact the link
self.logger.update_redact_filter(d_final)

self.logger.info(f"\t\t\t\tAdding {d_final} to JDownloader")
self.jd_device.linkgrabber.add_links(
[
Expand Down
2 changes: 2 additions & 0 deletions nxbrew_dl/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
from .github_tools import check_github_version
from .html_tools import get_html_page, get_game_dict, get_languages, get_thumb_url
from .io_tools import load_yml, save_yml, load_json, save_json
from .log_utils import NXBrewLogger
from .regex_tools import check_has_filetype, get_game_name

__all__ = [
"NXBrewLogger",
"discord_push",
"get_dl_dict",
"bypass_ouo",
Expand Down
36 changes: 25 additions & 11 deletions nxbrew_dl/util/download_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,16 +345,27 @@ def RecaptchaV3():
return answer


def bypass_ouo(url, logger=None, impersonate="safari"):
def bypass_ouo(
url,
logger=None,
impersonate="safari",
n_retry=0,
max_retries=5,
):
"""Bypass OUO url
Args:
url (str): URL to bypass
logger (logging.Logger): Logger to use. Defaults to None,
which will not log anything
impersonate (str): Type of browser to impersonate
n_retry (int): Current retry. Defaults to 0
max_retries (int): Maximum number of retries. Defaults to 5
"""

if n_retry >= max_retries:
raise ValueError("Max retries exceeded!")

client = cffi_requests.Session()
client.headers.update(
{
Expand All @@ -372,15 +383,20 @@ def bypass_ouo(url, logger=None, impersonate="safari"):
temp_url_id = tempurl.split("/")[-1]
res = client.get(tempurl, impersonate=impersonate)

# If we get a weird response
# If we get a weird response, try again
status_code = res.status_code
while status_code not in [200, 302]:
if status_code not in [200, 302]:

if logger is not None:
logger.warning(f"Received status code {status_code}. Waiting then retrying")

time.sleep(10)
res = client.get(tempurl, impersonate=impersonate)
bypass_ouo(
url,
logger=logger,
impersonate=impersonate,
n_retry=n_retry + 1,
)

next_url = f"{p.scheme}://{p.hostname}/go/{temp_url_id}"

Expand All @@ -406,22 +422,20 @@ def bypass_ouo(url, logger=None, impersonate="safari"):
)

status_code = res.status_code
while status_code not in [200, 302]:
if status_code not in [200, 302]:

if logger is not None:
logger.warning(
f"Received status code {status_code}. Waiting then retrying"
)

time.sleep(10)
res = client.post(
next_url,
data=data,
headers=h,
allow_redirects=False,
bypass_ouo(
url,
logger=logger,
impersonate=impersonate,
n_retry=n_retry + 1,
)
status_code = res.status_code

next_url = f"{p.scheme}://{p.hostname}/xreallcygo/{temp_url_id}"

Expand Down
1 change: 1 addition & 0 deletions nxbrew_dl/util/github_tools.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import requests


def check_github_version():
"""Check NXBrew-dl version on GitHub. Returns version and associated URL"""

Expand Down
Loading

0 comments on commit 0479fb7

Please sign in to comment.