From 3b2ca4d70c9aa918be4653d31437faed8624732f Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sat, 11 Jan 2020 16:11:35 +0100 Subject: [PATCH] Added Foursquare backend --- docs/source/backends.rst | 1 + docs/source/events.rst | 1 + docs/source/platypush/backend/foursquare.rst | 5 ++ docs/source/platypush/events/foursquare.rst | 5 ++ platypush/backend/foursquare.py | 55 ++++++++++++++++++++ platypush/message/event/foursquare.py | 14 +++++ 6 files changed, 81 insertions(+) create mode 100644 docs/source/platypush/backend/foursquare.rst create mode 100644 docs/source/platypush/events/foursquare.rst create mode 100644 platypush/backend/foursquare.py create mode 100644 platypush/message/event/foursquare.py diff --git a/docs/source/backends.rst b/docs/source/backends.rst index 820d0969..ca3b548a 100644 --- a/docs/source/backends.rst +++ b/docs/source/backends.rst @@ -15,6 +15,7 @@ Backends platypush/backend/button.flic.rst platypush/backend/camera.pi.rst platypush/backend/chat.telegram.rst + platypush/backend/foursquare.rst platypush/backend/google.fit.rst platypush/backend/google.pubsub.rst platypush/backend/gps.rst diff --git a/docs/source/events.rst b/docs/source/events.rst index 5526ba5c..dc496513 100644 --- a/docs/source/events.rst +++ b/docs/source/events.rst @@ -15,6 +15,7 @@ Events platypush/events/camera.rst platypush/events/chat.telegram.rst platypush/events/distance.rst + platypush/events/foursquare.rst platypush/events/geo.rst platypush/events/google.rst platypush/events/google.fit.rst diff --git a/docs/source/platypush/backend/foursquare.rst b/docs/source/platypush/backend/foursquare.rst new file mode 100644 index 00000000..fda4c1a6 --- /dev/null +++ b/docs/source/platypush/backend/foursquare.rst @@ -0,0 +1,5 @@ +``platypush.backend.foursquare`` +================================ + +.. automodule:: platypush.backend.foursquare + :members: diff --git a/docs/source/platypush/events/foursquare.rst b/docs/source/platypush/events/foursquare.rst new file mode 100644 index 00000000..ff01c125 --- /dev/null +++ b/docs/source/platypush/events/foursquare.rst @@ -0,0 +1,5 @@ +``platypush.message.event.foursquare`` +====================================== + +.. automodule:: platypush.message.event.foursquare + :members: diff --git a/platypush/backend/foursquare.py b/platypush/backend/foursquare.py new file mode 100644 index 00000000..e0f53ce5 --- /dev/null +++ b/platypush/backend/foursquare.py @@ -0,0 +1,55 @@ +import time + +from platypush.backend import Backend +from platypush.context import get_plugin +from platypush.message.event.foursquare import FoursquareCheckinEvent + + +class FoursquareBackend(Backend): + """ + This backend polls for new check-ins on the user's Foursquare account and triggers an event when a new check-in + occurs. + + Requires: + + * The :class:`platypush.plugins.foursquare.FoursquarePlugin` plugin configured and enabled. + + Triggers: + + - :class:`platypush.message.event.foursquare.FoursquareCheckinEvent` when a new check-in occurs. + + """ + + _last_created_at_varname = '_foursquare_checkin_last_created_at' + + def __init__(self, poll_seconds: float = 60.0, *args, **kwargs): + """ + :param poll_seconds: How often the backend should check for new check-ins (default: one minute). + """ + super().__init__(*args, **kwargs) + self.poll_seconds = poll_seconds + self._last_created_at = None + + def run(self): + super().run() + self._last_created_at = int(get_plugin('variable').get(self._last_created_at_varname). + output.get(self._last_created_at_varname)) + self.logger.info('Started Foursquare backend') + + while not self.should_stop(): + try: + checkins = get_plugin('foursquare').get_checkins().output + if checkins: + last_checkin = checkins[0] + if not self._last_created_at or last_checkin.get('createdAt', 0) > self._last_created_at: + self.bus.post(FoursquareCheckinEvent(checkin=last_checkin)) + self._last_created_at = last_checkin.get('createdAt', 0) + get_plugin('variable').set(**{self._last_created_at_varname: self._last_created_at}) + except Exception as e: + self.logger.error('Error while retrieving the list of checkins: {}'.format(str(e))) + self.logger.exception(e) + finally: + time.sleep(self.poll_seconds) + + +# vim:sw=4:ts=4:et: diff --git a/platypush/message/event/foursquare.py b/platypush/message/event/foursquare.py new file mode 100644 index 00000000..33182ec5 --- /dev/null +++ b/platypush/message/event/foursquare.py @@ -0,0 +1,14 @@ +from typing import Dict, Any + +from platypush.message.event import Event + + +class FoursquareCheckinEvent(Event): + """ + Event triggered when a new check-in occurs. + """ + def __init__(self, checkin: Dict[str, Any], *args, **kwargs): + super().__init__(*args, checkin=checkin, **kwargs) + + +# vim:sw=4:ts=4:et: