]> kaliko git repositories - sid.git/blob - sid/lib.py
Switch to SPDX headers
[sid.git] / sid / lib.py
1 # coding: utf-8
2 # SPDX-FileCopyrightText:  2020 kaliko <kaliko@azylum.org>
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 import json
6 import logging
7
8 from urllib.error import URLError, HTTPError
9 from urllib.request import Request, urlopen
10 from urllib.parse import urlencode
11
12
13 def get_pkg(pkg):
14     """
15     Uses the DAK HTTP api
16     https://ftp-team.pages.debian.net/dak/epydoc/dakweb.queries.madison-module.html
17     """
18     logger = logging.getLogger(__package__)
19     data = {}
20     data['package'] = pkg
21     data['f'] = ''
22     values = urlencode(data)
23     url = f'https://api.ftp-master.debian.org/madison?{values}'
24     logger.debug(url)
25     req = Request(url, method='GET')
26     try:
27         response = urlopen(req)
28         ans = response.read()
29     except HTTPError as err:
30         logger.info('The server couldn\'t fulfill the request.')
31         logger.debug('Error code: %s', err.code)
32     except URLError as err:
33         logger.info('We failed to reach a server.')
34         logger.debug('Reason: %s', err.reason)
35     try:
36         pkg_info = json.loads(ans)
37     except Exception as err:
38         logger.error('Failed to decode json')
39     if not pkg_info:
40         return []
41     return pkg_info[0][pkg]
42
43
44 # Script starts here
45 if __name__ == '__main__':
46     pass
47
48 # VIM MODLINE
49 # vim: ai ts=4 sw=4 sts=4 expandtab fileencoding=utf8