summaryrefslogtreecommitdiffstats
path: root/share/extensions/other/clipart/sources/reactome.py
blob: 1a1c8b397f1a3c1691ca0fc15f001183e01b38ab (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
#
# Copyright 2021 Martin Owens <doctormo@gmail.com>
#
# This program 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.
#
#  This program 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 this program.  If not, see <http://www.gnu.org/licenses/>
#
"""
Access Reactome Content Services.
"""

import re

from import_sources import RemoteSource

TAG_REX = re.compile(r'<[^<]+?>')

class Reactome(RemoteSource):
    name = 'Reactome (Bio)'
    icon = 'sources/reactome.svg'
    search_url = "https://reactome.org/ContentService/search/query"
    file_url = "https://reactome.org/icon/{stId}.svg"
    icon_url = "https://reactome.org/icon/{stId}.png"
    all_licence = "cc-by-sa-4.0"

    def search(self, query):
        params = {
            "query": query,
            "types": "Icon",
            "cluster": "true",
            "Start row": 0,
            "rows": 100,
        }
        response = {}
        try:
            response = self.session.get(self.search_url, params=params).json()
        except Exception:
            pass

        if 'messages' in response and 'No entries' in response['messages'][0]:
            return
        for cats in response.get('results', []):
            for entry in cats['entries']:
                yield {
                    'id': entry['dbId'],
                    'name': TAG_REX.sub('', entry['name']),
                    'author': 'Reactome/'+entry.get('iconDesignerName', "Unknown"),
                    'summary': TAG_REX.sub('', entry.get('summation', '')),
                    'created': None, # No data
                    'popularity': 0, # No data
                    'thumbnail': self.icon_url.format(**entry),
                    'file': self.file_url.format(**entry),
                    'license': self.all_licence,
                }