summaryrefslogtreecommitdiff
path: root/sniffa.py
diff options
context:
space:
mode:
authorB. Stack <bgstack15@gmail.com>2022-01-11 15:40:54 -0500
committerB. Stack <bgstack15@gmail.com>2022-01-11 15:40:54 -0500
commite67d26de5b45164b944c396c9292f33d7ebefd3f (patch)
treea103925a08fb94f95d9721e67bcab420edd0da21 /sniffa.py
parentCreate minimalistic implementation and docs (diff)
downloadsniffa-e67d26de5b45164b944c396c9292f33d7ebefd3f.tar.gz
sniffa-e67d26de5b45164b944c396c9292f33d7ebefd3f.tar.bz2
sniffa-e67d26de5b45164b944c396c9292f33d7ebefd3f.zip
add discord logic and ini example
Diffstat (limited to 'sniffa.py')
-rwxr-xr-xsniffa.py52
1 files changed, 28 insertions, 24 deletions
diff --git a/sniffa.py b/sniffa.py
index b1e8b18..69dd6ae 100755
--- a/sniffa.py
+++ b/sniffa.py
@@ -1,26 +1,29 @@
-#!/usr/local/bin/python3
-
-#
-# Note: Ideally the shebang line would contain /usr/bin/env python3 instead of the
-# hardcoded path but it seems the user's environment is not inherited by
-# LaunchAgents (at least not on El Capitan).
-#
-
-import os
-import sys
-import errno
-import json
-import configparser
-import urllib3
-import urllib.parse
-import datetime
-
-import certifi
-import pync
+#!/usr/bin/env python3
+# File: sniffa.py
+# Location: https://gitlab.com/bgstack15/sniffa
+# Author: danielmitterdorfer
+# SPDX-License-Identifer: GPL-3.0
+# Startdate: 2016-07-16
+# Title: Discourse sniffer
+# Purpose: Send alerts for saved searches on Discourse forum websites
+# History:
+# 2022-01-07 forked by bgstack15 to send discord messages
+# Homepage: https://github.com/danielmitterdorfer/sniffa
+# Usage:
+# ./sniffa.py aoe2
+# Reference:
+# https://meta.discourse.org/t/watching-and-sending-notifications-for-keywords/59286
+# Improve:
+# Documentation: See README.md
+# Dependencies:
+# req-fedora: python3-certifi
+# req-pip3: discord
+# A webhook given to me by discord "server" admin
+import os, sys, errno, json, configparser, urllib3, urllib.parse, datetime, certifi, requests
+from discord import Webhook, RequestsWebhookAdapter
DOMAIN_SECTION_KEY = "sniffa.domain"
-
def ensure_dir(directory):
try:
os.makedirs(directory)
@@ -31,7 +34,6 @@ def ensure_dir(directory):
def creation_date(item):
return datetime.datetime.strptime(item["created_at"], "%Y-%m-%dT%H:%M:%S.%fZ").timestamp()
-
def main():
if not len(sys.argv) == 2:
print("usage: %s domain_key" % sys.argv[0], file=sys.stderr)
@@ -95,11 +97,13 @@ def main():
sorted(posts, key=creation_date, reverse=True)
+ if len(posts) > 0:
+ webhook = Webhook.from_url(config[DOMAIN_SECTION_KEY]["webhook"], adapter=RequestsWebhookAdapter())
for post in posts:
topic = topics_by_id[post["topic_id"]]
- pync.Notifier.notify(topic["title"], title="New post mentioning '%s'" % keyword,
- open="%s/t/%s/%d" % (domain, topic["slug"], topic["id"]), group=str(topic["id"]))
-
+ message = f"New post mentioning \"{keyword}\". Url=\"{domain}/t/{topic['slug']}/{topic['id']}\". group={str(topic['id'])}."
+ print(message)
+ webhook.send(f"{domain}/t/{topic['slug']}/{topic['id']}", username=config[DOMAIN_SECTION_KEY]["discord_user"])
with open(watches_file, "w") as f:
config.write(f)
print("Finished")
bgstack15