-
Notifications
You must be signed in to change notification settings - Fork 1
/
semwikievents.py
executable file
·104 lines (68 loc) · 2.83 KB
/
semwikievents.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#! /usr/bin/python3
# by FvH, released under Apache License v2.0
# either install 'python3-paho-mqtt' or 'pip3 install paho-mqtt'
import datetime
import json
import paho.mqtt.client as mqtt
import requests
import threading
import time
import socket
import sys
mqtt_server = 'mqtt.vm.nurd.space'
topic_prefix = 'GHBot/'
channels = ['nurdbottest', 'nurds', 'nurdsbofh']
prefix = '!'
def announce_commands(client):
target_topic = f'{topic_prefix}to/bot/register'
client.publish(target_topic, 'cmd=events|descr=NURDspace events')
def on_message(client, userdata, message):
global prefix
text = message.payload.decode('utf-8')
topic = message.topic[len(topic_prefix):]
if topic == 'from/bot/command' and text == 'register':
announce_commands(client)
return
if topic == 'from/bot/parameter/prefix':
prefix = text
return
if len(text) == 0:
return
parts = topic.split('/')
channel = parts[2] if len(parts) >= 3 else 'nurds'
nick = parts[3] if len(parts) >= 4 else 'jemoeder'
if text[0] != prefix:
return
command = text[1:].split(' ')[0]
if channel in channels or (len(channel) >= 1 and channel[0] == '\\'):
response_topic = f'{topic_prefix}to/irc/{channel}/privmsg'
if command == 'events':
now = datetime.datetime.now()
from_day = now.strftime("%d")
from_month = now.strftime("%m")
from_year = now.strftime("%Y")
url = f'https://nurdspace.nl/Special:Ask/-5B-5BCategory:Events-5D-5D-20-5B-5BDate::-3E-20{from_month}-20{from_day}-20{from_year}-5D-5D-20OR-20-5B-5BDateEnd::-3E-20{from_month}-20{from_day}-20{from_year}-5D-5D/-3FName/-3FDate/-3FDateEnd/-3FLocation/-3F-23-2D/format%3Djson/limit%3D5/offset%3D0/sort%3DDate/mainlabel%3D-2D/default%3DNone-20currently-20planned'
r = requests.get(url, timeout=10)
events_j = json.loads(r.content.decode('ascii'))
events = []
for e in events_j['results']:
event = events_j['results'][e]
events.append(f'{e}: {event["fullurl"]}')
client.publish(response_topic, ', '.join(events))
def on_connect(client, userdata, flags, rc):
client.subscribe(f'{topic_prefix}from/irc/#')
client.subscribe(f'{topic_prefix}from/bot/command')
def announce_thread(client):
while True:
try:
announce_commands(client)
time.sleep(4.1)
except Exception as e:
print(f'Failed to announce: {e}')
client = mqtt.Client(f'{socket.gethostname()}_{sys.argv[0]}', clean_session=False)
client.on_message = on_message
client.on_connect = on_connect
client.connect(mqtt_server, port=1883, keepalive=4, bind_address="")
t = threading.Thread(target=announce_thread, args=(client,))
t.start()
client.loop_forever()