]> kaliko git repositories - sid.git/blob - sid/lib.py
Bump version
[sid.git] / sid / lib.py
1 # coding: utf-8
2
3 # Copyright (C) 2020 kaliko <kaliko@azylum.org>
4
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, version 3 only.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 import json
18 import logging
19
20 from urllib.error import URLError, HTTPError
21 from urllib.request import Request, urlopen
22 from urllib.parse import urlencode
23
24
25 def get_pkg(pkg):
26     """
27     Uses the DAK HTTP api
28     https://ftp-team.pages.debian.net/dak/epydoc/dakweb.queries.madison-module.html
29     """
30     logger = logging.getLogger(__package__)
31     data = {}
32     data['package'] = pkg
33     data['f'] = ''
34     values = urlencode(data)
35     url = f'https://api.ftp-master.debian.org/madison?{values}'
36     logger.debug(url)
37     req = Request(url, method='GET')
38     try:
39         response = urlopen(req)
40         ans = response.read()
41     except HTTPError as err:
42         logger.info('The server couldn\'t fulfill the request.')
43         logger.debug('Error code: %s', err.code)
44     except URLError as err:
45         logger.info('We failed to reach a server.')
46         logger.debug('Reason: %s', err.reason)
47     try:
48         pkg_info = json.loads(ans)
49     except Exception as err:
50         logger.error('Failed to decode json')
51     if not pkg_info:
52         return []
53     return pkg_info[0][pkg]
54
55
56 # Script starts here
57 if __name__ == '__main__':
58     pass
59
60 # VIM MODLINE
61 # vim: ai ts=4 sw=4 sts=4 expandtab fileencoding=utf8