Skip to content

Instantly share code, notes, and snippets.

@statik
Last active July 17, 2023 15:26
Show Gist options
  • Save statik/db1944c31f29262be900236c2a9986ff to your computer and use it in GitHub Desktop.
Save statik/db1944c31f29262be900236c2a9986ff to your computer and use it in GitHub Desktop.
apprise custom notifications for h2r experiments
#!/usr/bin/env python3
# include the decorator
from apprise.decorators import notify
from apprise.logger import logger
import requests
import re
import json
import sys
import time
import urllib.parse as urlparse
import uuid
@notify(on="h2rdata")
def h2r_datasource_wrapper(body, title, notify_type, *args, **kwargs):
# kwargs is a dictionary like
# {'attach': <apprise.AppriseAttachment.AppriseAttachment object at 0x10563aee0>,
# 'body_format': 'text',
# 'meta': {'asset': <apprise.AppriseAsset.AppriseAsset object at 0x104db4cd0>,
# 'fullpath': '/data/O9W9H133BM/',
# 'host': '127.0.0.1',
# 'path': '/data/O9W9H133BM/',
# 'port': 4001,
# 'schema': 'h2rdata',
# 'tag': set(),
# 'url': 'h2rdata://127.0.0.1:4001/data/O9W9H133BM/'}}
meta = kwargs.get('meta', {})
url_parts = list(urlparse.urlsplit(meta.get('url', '')))
url_parts[0] = 'http'
url = urlparse.urlunsplit(url_parts)
logger.debug("We got this for url %s", url)
send_h2r_notification(url, title, body)
def get_author_details(title, body):
# https://flickr.com/photos/volvob12b/14245584798/in/photolist-nGQm41-KYu4xN-oQ8WKw-gzdu5b-89XtpE-o9xWWz-agis4c-sm8uwh-9NzeEF-nGLwVZ-SVApxj-89Udq4-uY2HvC-KqmCB-acghe5-aCCEd6-jpqNFt-i2UfoY-TeJyEW-T6UZqa-8zRH6y-TeJnyY-59Q8eL-aa69ZG-p62kbB-55d4jp-576TE2-S1QpXs-2nFPSD2-WHsdi8-2mxme5Y-2nNPcLn-2nMPLgX-2kRdfE9-5uz8xd-7kW49F-7dD6Ez-2hssrS4-2iUNVHa-2ca92mg-5VhKgK-29mwJgC-27DwUYM-LhhPhR-MV4uEH-2hfhTjT-2kfsyRh-7z66r7-K8y7yj-SJjGhr
defaultFlickrUrl = "https://live.staticflickr.com/3836/14245584798_cd17cb6bb2_q_d.jpg"
flickrUrl = defaultFlickrUrl
match = re.search("(?P<url>https?://[^\s]+)", body)
if match:
flickrUrl = match.group("url")
body = body.replace(flickrUrl, "")
body = body.rstrip(" ").rstrip("{")
return {
"snippet" : {
"displayMessage": body,
},
"authorDetails": {
"displayName": title,
"profileImageUrl": flickrUrl,
}
}
def get_platform_details():
return {
"platform": {
"name": "BirdNET-Pi",
"logoUrl": "https://raw.githubusercontent.com/mcguirepr89/BirdNET-Pi/main/homepage/images/bird.png",
}
}
def send_h2r_notification(url, title, body):
timestamp = time.monotonic_ns()
id = str(uuid.uuid4())
h2rmessages = {
"messages": [
{
"id": id,
"timestamp": timestamp,
**get_platform_details(),
**get_author_details(title, body),
}
]
}
r = requests.post(url, json=h2rmessages)
return h2rmessages
if __name__ == "__main__":
print("in main")
destination = "http://127.0.0.1:4001/data/O9W9H133BM/"
httpbin = "https://httbin.org/post"
sent_json = send_h2r_notification(destination, "Palm Desert BirdNet-Pi", "DEAD BIRD SPOTTED")
print(json.dumps(sent_json, sort_keys=True, indent=4))
sys.exit()
#!/usr/bin/env bash
server_url="h2rdata://192.168.254.54:4001/data/QDMEWOHP5Z/"
apprise \
-vvvvv \
--plugin-path h2rnotify.py \
--body "FOUND A DANG BIRD https://live.staticflickr.com/7430/27545810581_8bfa8289a3_c.jpg " \
--title "Bird Alert" \
"${server_url}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment