From d02046f9255a07c4eb2bda9eb73d229cdb4f4a53 Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Mon, 27 Aug 2018 17:43:16 +0200 Subject: parsers: parseur orienté objet avec une classe abstraite Signed-off-by: Alban Gruin --- management/parsers/abstractparser.py | 52 ++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 management/parsers/abstractparser.py (limited to 'management/parsers/abstractparser.py') diff --git a/management/parsers/abstractparser.py b/management/parsers/abstractparser.py new file mode 100644 index 0000000..8d55b6d --- /dev/null +++ b/management/parsers/abstractparser.py @@ -0,0 +1,52 @@ +# 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 . + +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) -- cgit v1.2.1