Unverified Commit af7e97ad authored by TheTechRobo's avatar TheTechRobo
Browse files

Fix some bugs lol

parent 08fe1891
Loading
Loading
Loading
Loading
+14 −6
Original line number Diff line number Diff line
"""
The CLI interface of LostMediaFinder.
None of this is public API!
"""

import click
@@ -7,22 +8,28 @@ from switch import Switch

from . import YouTubeResponse

@click.group(help="CLI tool to search for lost media")
def main():
    """
    Error codes:
        - 0: All operations (seem) successful.
        - 1: A fatal error was thrown.
        - 2: One or more operations failed.
    """

@click.command
@click.option("--format", default="text", help="Selects which format to output to stdout.", type=click.Choice(["json", "text"]))
@click.argument("id")
@click.pass_context
def main(ctx, id: str, format: str) -> int:
def youtube(ctx, id: str, format: str) -> int:
    """
    Parses CLI arguments and returns the Response for the video ID <IDENT>.

    Error codes:
        - 0: All operations (seem) successful.
        - 1: A fatal error was thrown.
        - 2: One or more operations failed.
    """
    click.echo("\033[1m\033[4m\033[1;31mUsing LostMediaFinder from the command-line is unstable!\033[0m", err=True)
    click.echo("Generating report, this could take some time...", err=True)
    response = YouTubeResponse.generate(id)
    if response.status == "bad.id":
        raise ValueError("Bad video ID - does not match regex")
    with Switch(format) as case:
        if case("json"):
            click.echo(response.json())
@@ -34,4 +41,5 @@ def main(ctx, id: str, format: str) -> int:
    code = 2 if errors else 0
    ctx.exit(code)

main.add_command(youtube)
main() # pylint: disable=no-value-for-parameter
+4 −4
Original line number Diff line number Diff line
@@ -117,8 +117,8 @@ class Ya(YouTubeService):
    @classmethod
    def _run(cls, id, includeRaw=True, asynchronous=False):
        vid = id
        assert cls._getFromConfig("enabled"), "#youtubearchive API access is not enabled"
        auth = HTTPBasicAuth(cls._getFromConfig("username"), cls._getFromConfig("password"))
        assert cls._getFromConfig("ya", "enabled"), "#youtubearchive API access is not enabled"
        auth = HTTPBasicAuth(cls._getFromConfig("ya", "username"), cls._getFromConfig("ya", "password"))
        comments = False
        count = requests.get("https://ya.borg.xyz/cgi-bin/capture-count?v=" + vid, auth=auth, timeout=5).text
        if not count:
@@ -142,9 +142,9 @@ class Filmot(YouTubeService):

    @classmethod
    def _run(cls, id, includeRaw=True, asynchronous=False) -> T:
        enabled = cls._getFromConfig("enabled")
        enabled = cls._getFromConfig("filmot", "enabled")
        assert enabled, "Filmot API access is not enabled."
        key = cls._getFromConfig("key")
        key = cls._getFromConfig("filmot", "key")
        while time.time() - cls.lastretrieved < cls.cooldown:
            time.sleep(0.1)
        lastupdated = time.time()
+13 −3
Original line number Diff line number Diff line
@@ -61,8 +61,11 @@ class Service(JSONDataclass):
    error: bool = False

    @staticmethod
    def _getFromConfig(key):
        return getattr(config, key)
    def _getFromConfig(key, key1=None):
        val = getattr(config, key)
        if key1:
            val = getattr(val, key1)
        return val

    @classmethod
    def _run(cls, id, includeRaw=True, asynchronous=False) -> T:
@@ -142,6 +145,13 @@ class YouTubeResponse(JSONDataclass):
    def _get_services(cls):
        return YouTubeService.__subclasses__()

    @staticmethod
    def verifyId(id: str) -> bool:
        """
        Checks if a video ID is valid.
        """
        return bool(re.match(r"^[A-Za-z0-9_-]{10}[AEIMQUYcgkosw048]$", id))

    @classmethod
    def generate(cls, id, asyncio=False):
        """
@@ -150,7 +160,7 @@ class YouTubeResponse(JSONDataclass):
            id: The video ID
            asyncio: Whether or not to use asyncio.run_until_complete; this is implied if you use generateAsync
        """
        if not re.match(r"^[A-Za-z0-9_-]{10}[AEIMQUYcgkosw048]$", id):
        if not cls.verifyId(id):
            return cls(status="bad.id", id=id, keys=[])
        keys = []
        services = cls._get_services()