From e67d26de5b45164b944c396c9292f33d7ebefd3f Mon Sep 17 00:00:00 2001 From: "B. Stack" Date: Tue, 11 Jan 2022 15:40:54 -0500 Subject: add discord logic and ini example --- .gitignore | 1 + README.md | 8 ++++---- sniffa.py | 52 ++++++++++++++++++++++++++++------------------------ watches-example.ini | 8 ++++++++ 4 files changed, 41 insertions(+), 28 deletions(-) create mode 100644 .gitignore create mode 100644 watches-example.ini diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a01ee28 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.*.swp diff --git a/README.md b/README.md index 6c74686..6fd1408 100644 --- a/README.md +++ b/README.md @@ -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 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") 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 + -- cgit