From f0804783850308c314dcc2751999b2210e4a51db Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Thu, 2 Jun 2022 00:36:14 +0200 Subject: [PATCH 1/3] s/click_url/url/g in ntfy message definitions --- platypush/message/event/ntfy.py | 6 +++--- platypush/plugins/ntfy/__init__.py | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/platypush/message/event/ntfy.py b/platypush/message/event/ntfy.py index 1709cb186..3dfb8fc8f 100644 --- a/platypush/message/event/ntfy.py +++ b/platypush/message/event/ntfy.py @@ -21,7 +21,7 @@ class NotificationEvent(Event): attachment: Optional[Mapping] = None, actions: Optional[Collection[Mapping]] = None, tags: Optional[Collection[str]] = None, - click_url: Optional[str] = None, + url: Optional[str] = None, **kwargs ): """ @@ -32,7 +32,7 @@ class NotificationEvent(Event): :param priority: Message priority. :param time: Message UNIX timestamp. :param tags: Notification tags. - :param click_url: URL spawned when the notification is clicked. + :param url: URL spawned when the notification is clicked. :param actions: List of actions associated to the notification. Example: @@ -89,6 +89,6 @@ class NotificationEvent(Event): tags=tags, attachment=attachment, actions=actions, - click_url=click_url, + url=url, **kwargs ) diff --git a/platypush/plugins/ntfy/__init__.py b/platypush/plugins/ntfy/__init__.py index d197a6b1f..f286ee4d3 100644 --- a/platypush/plugins/ntfy/__init__.py +++ b/platypush/plugins/ntfy/__init__.py @@ -95,7 +95,7 @@ class NtfyPlugin(RunnablePlugin): message=msg.get('message'), title=msg.get('title'), tags=msg.get('tags'), - click_url=msg.get('click'), + url=msg.get('click'), actions=msg.get('actions'), attachment=msg.get('attachment'), ) @@ -142,7 +142,7 @@ class NtfyPlugin(RunnablePlugin): username: Optional[str] = None, password: Optional[str] = None, title: Optional[str] = None, - click_url: Optional[str] = None, + url: Optional[str] = None, attachment: Optional[str] = None, filename: Optional[str] = None, actions: Optional[Collection[Mapping[str, str]]] = None, @@ -160,7 +160,7 @@ class NtfyPlugin(RunnablePlugin): :param username: Set if publishing to the topic requires authentication :param password: Set if publishing to the topic requires authentication :param title: Custom notification title. - :param click_url: URL that should be opened when the user clicks the + :param url: URL that should be opened when the user clicks the notification. It can be an ``http(s)://`` URL, a ``mailto:`, a ``geo:``, a link to another ntfy topic (e.g. ``ntfy://mytopic``) or a Twitter link (e.g. ``twitter://user?screen_name=myname``). @@ -240,7 +240,7 @@ class NtfyPlugin(RunnablePlugin): args['headers'] = { 'Filename': filename, **({'X-Title': title} if title else {}), - **({'X-Click': click_url} if click_url else {}), + **({'X-Click': url} if url else {}), **({'X-Email': email} if email else {}), **({'X-Priority': priority} if priority else {}), **({'X-Tags': ','.join(tags)} if tags else {}), @@ -255,7 +255,7 @@ class NtfyPlugin(RunnablePlugin): 'topic': topic, 'message': message, **({'title': title} if title else {}), - **({'click': click_url} if click_url else {}), + **({'click': url} if url else {}), **({'email': email} if email else {}), **({'priority': priority} if priority else {}), **({'tags': tags} if tags else {}), From d3f486539572639b39ac85734296a3f64373564e Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Thu, 2 Jun 2022 01:44:38 +0200 Subject: [PATCH 2/3] Fixed variable name conflict --- platypush/plugins/ntfy/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/platypush/plugins/ntfy/__init__.py b/platypush/plugins/ntfy/__init__.py index f286ee4d3..df9735bc3 100644 --- a/platypush/plugins/ntfy/__init__.py +++ b/platypush/plugins/ntfy/__init__.py @@ -226,6 +226,7 @@ class NtfyPlugin(RunnablePlugin): """ method = requests.post + click_url = url url = server_url or self._server_url args = {} if username and password: @@ -240,7 +241,7 @@ class NtfyPlugin(RunnablePlugin): args['headers'] = { 'Filename': filename, **({'X-Title': title} if title else {}), - **({'X-Click': url} if url else {}), + **({'X-Click': click_url} if click_url else {}), **({'X-Email': email} if email else {}), **({'X-Priority': priority} if priority else {}), **({'X-Tags': ','.join(tags)} if tags else {}), @@ -255,7 +256,7 @@ class NtfyPlugin(RunnablePlugin): 'topic': topic, 'message': message, **({'title': title} if title else {}), - **({'click': url} if url else {}), + **({'click': click_url} if click_url else {}), **({'email': email} if email else {}), **({'priority': priority} if priority else {}), **({'tags': tags} if tags else {}), From cb7021152f267ee1689175cc2f4fbd0b6c882c09 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Thu, 2 Jun 2022 20:57:35 +0200 Subject: [PATCH 3/3] Added `get_recent_tracks` method to the `lastfm` plugin --- platypush/plugins/lastfm/__init__.py | 59 +++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/platypush/plugins/lastfm/__init__.py b/platypush/plugins/lastfm/__init__.py index a43230247..1a838d7ad 100644 --- a/platypush/plugins/lastfm/__init__.py +++ b/platypush/plugins/lastfm/__init__.py @@ -1,4 +1,5 @@ import time +from typing import Optional, List from platypush.plugins import Plugin, action @@ -29,6 +30,7 @@ class LastfmPlugin(Plugin): """ import pylast + super().__init__() self.api_key = api_key @@ -40,7 +42,8 @@ class LastfmPlugin(Plugin): api_key=self.api_key, api_secret=self.api_secret, username=self.username, - password_hash=pylast.md5(self.password)) + password_hash=pylast.md5(self.password), + ) @action def scrobble(self, artist, title, album=None): @@ -81,4 +84,58 @@ class LastfmPlugin(Plugin): album=album, ) + @action + def get_recent_tracks( + self, + username: Optional[str] = None, + limit: int = 10, + time_from: Optional[int] = None, + time_to: Optional[int] = None, + ) -> List[dict]: + """ + Get a list of recently played tracks. + + :param username: Target username (default: the one registered to this + plugin). + :param limit: Maximum number of tracks to be returned (default: 10). + :param time_from: Return tracks starting from this time + (as a UNIX timestamp). + :param time_to: Return tracks starting up to this time + (as a UNIX timestamp). + :return: Example: + + .. code-block:: json + + [ + { + "artist": "Led Zeppelin", + "title": "Stairway to Heaven", + "album": "IV", + "timestamp": 1654196137 + } + ] + + """ + + return [ + { + 'title': track.track.title, + 'album': track.album, + 'timestamp': int(track.timestamp or 0), + **( + {'artist': track.track.artist.name} + if track.track.artist + else {'artist': None} + ), + } + for track in self.lastfm.get_user( + username or self.username + ).get_recent_tracks( + limit=limit, + time_from=time_from, + time_to=time_to, + ) + ] + + # vim:sw=4:ts=4:et: