aboutsummaryrefslogtreecommitdiff
path: root/management/parsers/abstractparser.py
diff options
context:
space:
mode:
authorAlban Gruin2018-08-27 17:43:16 +0200
committerAlban Gruin2018-09-03 19:20:54 +0200
commitd02046f9255a07c4eb2bda9eb73d229cdb4f4a53 (patch)
treeab1b58efe7f519decaa3d0ce2d9b616b2a35ddc6 /management/parsers/abstractparser.py
parent77b6ade4a7d465ca0fbc6b82950f3b54689d60e3 (diff)
parsers: parseur orienté objet avec une classe abstraite
Signed-off-by: Alban Gruin <alban at pa1ch dot fr>
Diffstat (limited to 'management/parsers/abstractparser.py')
-rw-r--r--management/parsers/abstractparser.py52
1 files changed, 52 insertions, 0 deletions
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 <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)