diff --git a/src/music_kraken/pages/encyclopaedia_metallum.py b/src/music_kraken/pages/encyclopaedia_metallum.py index 86fadcf..6eaeed1 100644 --- a/src/music_kraken/pages/encyclopaedia_metallum.py +++ b/src/music_kraken/pages/encyclopaedia_metallum.py @@ -5,7 +5,7 @@ from bs4 import BeautifulSoup import pycountry from urllib.parse import urlparse -from ..utils.shared import ENCYCLOPAEDIA_METALLUM_LOGGER +from ..utils.shared import ENCYCLOPAEDIA_METALLUM_LOGGER, proxies from ..utils import string_processing from .abstract import Page from ..objects import ( @@ -25,6 +25,7 @@ from ..objects import ( class EncyclopaediaMetallum(Page): API_SESSION: requests.Session = requests.Session() + API_SESSION.proxies = proxies API_SESSION.headers = { "Host": "www.metal-archives.com", "Connection": "keep-alive" diff --git a/src/music_kraken/utils/config/__init__.py b/src/music_kraken/utils/config/__init__.py index abe32eb..6be6b0a 100644 --- a/src/music_kraken/utils/config/__init__.py +++ b/src/music_kraken/utils/config/__init__.py @@ -1,5 +1,6 @@ from .logging import LOGGING_SECTION from .audio import AUDIO_SECTION +from .connection import CONNECTION_SECTION from .config import read, write, config diff --git a/src/music_kraken/utils/config/base_classes.py b/src/music_kraken/utils/config/base_classes.py index c462922..08342c0 100644 --- a/src/music_kraken/utils/config/base_classes.py +++ b/src/music_kraken/utils/config/base_classes.py @@ -87,9 +87,25 @@ class IntAttribute(SingleAttribute): return int(self.value) +class BoolAttribute(SingleAttribute): + def validate(self, value: str): + if value.lower().strip() not in {"true", "false"}: + raise SettingValueError( + setting_name=self.name, + setting_value=value, + rule="has to be a bool (true/false)" + ) + + @property + def object_from_value(self) -> bool: + return self.value.lower().strip() in {"yes", "y", "t", "true"} + + class FloatAttribute(SingleAttribute): def validate(self, value: str): - if not value.isnumeric(): + try: + float(value) + except ValueError: raise SettingValueError( setting_name=self.name, setting_value=value, diff --git a/src/music_kraken/utils/config/config.py b/src/music_kraken/utils/config/config.py index d4254c8..67b083f 100644 --- a/src/music_kraken/utils/config/config.py +++ b/src/music_kraken/utils/config/config.py @@ -7,6 +7,7 @@ from ..path_manager import LOCATIONS from .base_classes import Description, Attribute, Section, EmptyLine, COMMENT_PREFIX from .audio import AUDIO_SECTION from .logging import LOGGING_SECTION +from .connection import CONNECTION_SECTION class Config: @@ -19,6 +20,8 @@ class Config: "If you, for some reason wanna fill your drive real quickly, I mean enjoy HIFI music,\n" "feel free to tinker with the Bitrate or smth. :)"), AUDIO_SECTION, + Description("Modify how Music-Kraken connects to the internet:"), + CONNECTION_SECTION, Description("For all your Logging needs.\n" "If you found a bug, and wan't to report it, please set the Logging level to 0,\n" "reproduce the bug, and attach the logfile in the bugreport. ^w^"), diff --git a/src/music_kraken/utils/config/connection.py b/src/music_kraken/utils/config/connection.py index e69de29..3067d6e 100644 --- a/src/music_kraken/utils/config/connection.py +++ b/src/music_kraken/utils/config/connection.py @@ -0,0 +1,35 @@ +import logging +from typing import Callable + +from .base_classes import SingleAttribute, StringAttribute, Section, FloatAttribute, Description, IntAttribute, EmptyLine, BoolAttribute + + +class ConnectionSection(Section): + def __init__(self): + self.USE_TOR = BoolAttribute( + name="tor", + description="Route ALL traffic through Tor.\nNo guarantee though!", + value="false" + ) + self.CHUNK_SIZE = IntAttribute( + name="chunk_size", + description="Size of the chunks that are streamed.", + value="1024" + ) + self.SHOW_DOWNLOAD_ERRORS_THRESHOLD = FloatAttribute( + name="show_download_errors_threshold", + description="If the percentage of failed downloads goes over this threshold,\n" + "all the error messages are shown.", + value="0.3" + ) + + self.attribute_list = [ + self.USE_TOR, + self.CHUNK_SIZE, + self.SHOW_DOWNLOAD_ERRORS_THRESHOLD + ] + + super().__init__() + + +CONNECTION_SECTION = ConnectionSection() diff --git a/src/music_kraken/utils/shared.py b/src/music_kraken/utils/shared.py index a3f9552..3163556 100644 --- a/src/music_kraken/utils/shared.py +++ b/src/music_kraken/utils/shared.py @@ -4,7 +4,7 @@ from pathlib import Path from typing import List, Set, Tuple from .path_manager import LOCATIONS -from .config import LOGGING_SECTION, AUDIO_SECTION +from .config import LOGGING_SECTION, AUDIO_SECTION, CONNECTION_SECTION # modifies the garbage collector to speed up the program # https://mkennedy.codes/posts/python-gc-settings-change-this-and-make-your-app-go-20pc-faster/ @@ -87,16 +87,16 @@ DEFAULT_VALUES = { } -TOR: bool = False +TOR: bool = CONNECTION_SECTION.USE_TOR.object_from_value proxies = { 'http': 'socks5h://127.0.0.1:9150', 'https': 'socks5h://127.0.0.1:9150' } if TOR else {} # size of the chunks that are streamed -CHUNK_SIZE = 1024 +CHUNK_SIZE = CONNECTION_SECTION.CHUNK_SIZE.object_from_value # this is a percentage describing the percentage of failed downloads, # relative to the total downloads. # If the percentage goes over this threshold DownloadResult returns the download errors # in the __str__ method -SHOW_DOWNLOAD_ERRORS_THRESHOLD = 0.3 +SHOW_DOWNLOAD_ERRORS_THRESHOLD = CONNECTION_SECTION.SHOW_DOWNLOAD_ERRORS_THRESHOLD.object_from_value