aboutsummaryrefslogtreecommitdiff
path: root/src/wikiget/client.py
blob: 27291449b4d5807754b0bbccf77021758161d880 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# wikiget - CLI tool for downloading files from Wikimedia sites
# Copyright (C) 2018-2023 Cody Logan
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Wikiget 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.
#
# Wikiget 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 Wikiget. If not, see <https://www.gnu.org/licenses/>.

"""Handle API calls (via mwclient) for site and image information."""

from __future__ import annotations

import logging
from typing import TYPE_CHECKING

from mwclient import APIError, InvalidResponse, LoginError, Site
from requests import ConnectionError, HTTPError

import wikiget

if TYPE_CHECKING:
    from argparse import Namespace

    from mwclient.image import Image

logger = logging.getLogger(__name__)


def connect_to_site(site_name: str, args: Namespace) -> Site:
    """Create and return a Site object using the given site name and CLI arguments.

    :param site_name: hostname of the site to connect to
    :type site_name: str
    :param args: command-line arguments and their values
    :type args: argparse.Namespace
    :return: a new Site object
    :rtype: mwclient.Site
    """
    logger.info("Connecting to %s", site_name)

    try:
        # connect to site and identify ourselves
        site = Site(site_name, path=args.path, clients_useragent=wikiget.USER_AGENT)
        if args.username and args.password:
            logger.info("Attempting to authenticate with credentials")
            site.login(args.username, args.password)
    except ConnectionError as e:
        # usually this means there is no such site, or there's no network connection,
        # though it could be a certificate problem
        logger.error("Could not connect to specified site")
        logger.debug(e)
        raise
    except HTTPError as e:
        # most likely a 403 forbidden or 404 not found error for api.php
        logger.error(
            "Could not find the specified wiki's api.php. Check the value of --path."
        )
        logger.debug(e)
        raise
    except (InvalidResponse, LoginError) as e:
        # InvalidResponse: site exists, but we couldn't communicate with the API
        # endpoint for some reason other than an HTTP error.
        # LoginError: missing or invalid credentials
        logger.error(e)
        raise

    return site


def query_api(filename: str, site: Site) -> Image:
    """Query the given Site for an Image object matching the given filename.

    Even if there's no file by that name on the site, an Image will still be returned,
    though with an empty imageinfo attribute.

    :param filename: name of the file to retrieve
    :type filename: str
    :param site: the Site object to query
    :type site: mwclient.Site
    :return: an Image object representing the requested file
    :rtype: mwclient.image.Image
    """
    try:
        # get info about the target file
        image = site.images[filename]
    except APIError as e:
        # an API error at this point likely means access is denied, which could happen
        # with a private wiki
        logger.error(
            "Access denied. Try providing credentials with --username and --password."
        )
        for i in e.args:
            logger.debug(i)
        raise

    return image