aboutsummaryrefslogtreecommitdiff
path: root/management/parsers/abstractparser.py
blob: 3164082ed8cd6cf8c711956b37b69c23d2dc6473 (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
#    Copyright (C) 2018  Alban Gruin
#
#    celcatsanitizer is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published
#    by the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    celcatsanitizer 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 Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with celcatsanitizer.  If not, see <http://www.gnu.org/licenses/>.

import abc
import requests

import edt


class AbstractParser(metaclass=abc.ABCMeta):
    def __init__(self, source):
        self.source = source
        self.user_agent = "celcatsanitizer/" + edt.VERSION

    def _make_request(self, url, user_agent=None, encoding="utf8", **kwargs):
        user_agent = user_agent if user_agent is not None else self.user_agent

        params = kwargs["params"] if "params" in kwargs else {}
        headers = kwargs["headers"] if "headers" in kwargs else {}
        headers["User-Agent"] = user_agent

        req = requests.get(url, headers=headers, params=params)
        req.encoding = encoding

        return req

    @abc.abstractmethod
    def get_events(self):
        pass

    @abc.abstractmethod
    def get_update_date(self):
        pass

    @abc.abstractmethod
    def get_weeks(self):
        pass

    def get_source(self):
        return self._make_request(self.source.url)


class ParserError(Exception):
    def __init__(self, message):
        super(Exception, self).__init__(message)