#!/usr/bin/env python
"""LICENSE
Copyright 2021 Hermann Krumrey <hermann@krumreyh.com>

This file is part of torrent-download.

torrent-download is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

torrent-download is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with torrent-download.  If not, see <http://www.gnu.org/licenses/>.
LICENSE"""

import argparse
from puffotter.init import cli_start, argparse_add_verbosity
from torrent_download import sentry_dsn
from torrent_download.search import search_engines
from torrent_download.search.SearchEngine import SearchEngine


def main(args: argparse.Namespace):
    """
    Conducts a torrent file search
    :param args: The command line arguments
    :return: None
    """
    search_engine: SearchEngine = {
        x.name(): x for x in search_engines
    }[args.search_engine]()
    results = search_engine.search(args.search_term)

    for result in reversed(results):
        print(result)


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("search_term", help="The term to search for")
    parser.add_argument("search_engine", help="The search engine to use",
                        choices={x.name() for x in search_engines})
    argparse_add_verbosity(parser)
    cli_start(
        main, parser,
        sentry_dsn=sentry_dsn,
        package_name="torrent-download",
        exit_msg="Thanks for using torrent-download!"
    )
