diff options
author | B. Stack <bgstack15@gmail.com> | 2022-01-11 15:40:54 -0500 |
---|---|---|
committer | B. Stack <bgstack15@gmail.com> | 2022-01-11 15:40:54 -0500 |
commit | e67d26de5b45164b944c396c9292f33d7ebefd3f (patch) | |
tree | a103925a08fb94f95d9721e67bcab420edd0da21 | |
parent | Create minimalistic implementation and docs (diff) | |
download | sniffa-e67d26de5b45164b944c396c9292f33d7ebefd3f.tar.gz sniffa-e67d26de5b45164b944c396c9292f33d7ebefd3f.tar.bz2 sniffa-e67d26de5b45164b944c396c9292f33d7ebefd3f.zip |
add discord logic and ini example
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | README.md | 8 | ||||
-rwxr-xr-x | sniffa.py | 52 | ||||
-rw-r--r-- | watches-example.ini | 8 |
4 files changed, 41 insertions, 28 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a01ee28 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.*.swp @@ -1,16 +1,16 @@ sniffa ====== -sniffa is a small utility that allows you to watch Discuss forums for keywords. +#### This is the bgstack15 fork of sniffa! + +[sniffa](https://github.com/danielmitterdorfer/sniffa) is a small utility that allows you to watch Discuss forums for keywords. Every time it is invoked, it checks for new posts matching the keywords and creates a notification in the Mac OS X notification bar. # Requirements -* Mac OS X 10.8 or later: As it uses Mac OS X notifications, sniffa works only on Mac OS X 10.8 or later. * Python 3 -* certifi: Install with `pip3 install certifi` -* pync: Install with `pip3 install pync` +* certifi: Install with `yum install python3-certifi` or `pip3 install --user certifi` # Installation @@ -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") diff --git a/watches-example.ini b/watches-example.ini new file mode 100644 index 0000000..4854e23 --- /dev/null +++ b/watches-example.ini @@ -0,0 +1,8 @@ +[sniffa.domain] +url = https://forums.ageofempires.com +webhook = 'https://discord.com/api/webhooks/123498237429749274924/justanexamplewebhook123458987s87' +discord_user = AoE2 forum bot + +[VOTE NOW ranked rotation #age-of-empires-ii:aoe2-de in:title] +ids = 666250,280747,280748,666257,651927 + |