summaryrefslogtreecommitdiff
path: root/searx/engines/gitea.py
blob: 3775938edf56a7418d7b9efd86ded1226d6200b0 (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
106
107
108
109
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Engine to search in collaborative software platforms based on Gitea_.

.. _Gitea: https://about.gitea.com/

Configuration
=============

The engine has the following mandatory setting:

- :py:obj:`base_url`

Optional settings are:

- :py:obj:`sort`
- :py:obj:`order`
- :py:obj:`page_size`

.. code:: yaml

  - name: gitea.com
    engine: gitea
    base_url: https://gitea.com
    shortcut: gitea

If you would like to use additional instances, just configure new engines in the
:ref:`settings <settings engine>` and set the ``base_url``.


Implementation
==============

"""

from urllib.parse import urlencode
from dateutil import parser

about = {
    "website": 'https://about.gitea.com',
    "wikidata_id": None,
    "official_api_documentation": 'https://docs.gitea.com/next/development/api-usage',
    "use_official_api": True,
    "require_api_key": False,
    "results": 'JSON',
}

categories = ['it', 'repos']
paging = True

base_url: str = ''
"""URL of the Gitea_ instance."""

sort: str = "updated"
"""Sort criteria, possible values:

- ``updated`` (default)
- ``alpha``
- ``created``
- ``size``
- ``id``
"""

order = "desc"
"""Sort order, possible values:

- ``desc`` (default)
- ``asc``
"""

page_size: int = 10
"""Maximum number of results per page (default 10)."""


def init(_):
    if not base_url:
        raise ValueError('gitea engine: base_url is unset')


def request(query, params):
    args = {'q': query, 'limit': page_size, 'sort': sort, 'order': order, 'page': params['pageno']}
    params['url'] = f"{base_url}/api/v1/repos/search?{urlencode(args)}"

    return params


def response(resp):
    results = []

    for item in resp.json().get('data', []):
        content = [item.get(i) for i in ['language', 'description'] if item.get(i)]

        results.append(
            {
                'template': 'packages.html',
                'url': item.get('html_url'),
                'title': item.get('full_name'),
                'content': ' / '.join(content),
                'img_src': item.get('owner', {}).get('avatar_url'),
                'package_name': item.get('name'),
                'maintainer': item.get('owner', {}).get('login'),
                'publishedDate': parser.parse(item.get("updated_at") or item.get("created_at")),
                'tags': item.get('topics', []),
                'popularity': item.get('stargazers_count'),
                'homepage': item.get('homepage'),
                'source_code_url': item.get('clone_url'),
            }
        )

    return results