-
Notifications
You must be signed in to change notification settings - Fork 1
/
webhook.py
102 lines (91 loc) · 3.56 KB
/
webhook.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
import os
import time
import json
from datetime import datetime
LOG_FILE_DIR = './'
LOG_FILE_FORMAT = '%y%m%d.log'
CONFIG_FILE = 'config.json'
DELAY = 5 # Check for new messages every 5 seconds. Adjust this value as needed.
def main():
last_position = get_end_of_log_file()
sent_lines = set()
webhook_url = get_webhook_url()
current_log_file_path = get_current_log_file_path()
while True:
# Check if a new day has started and if so, reset the last_position and sent_lines
new_log_file_path = get_current_log_file_path()
if new_log_file_path != current_log_file_path:
current_log_file_path = new_log_file_path
last_position = 0
sent_lines = set()
if os.path.exists(current_log_file_path):
try:
with open(current_log_file_path, 'r') as log_file:
log_file.seek(last_position)
lines = log_file.readlines()
if lines:
last_position = log_file.tell()
for line in lines:
line = line.strip()
if line not in sent_lines:
sent_lines.add(line)
message = line.split(None, 6)[-1] if len(line.split()) > 6 else line
send_message_to_discord(message, webhook_url)
print(f'Sent message: {message}')
except IOError as e:
print(f'I/O error: {e}')
except Exception as e:
print(f'Unexpected error: {e}')
else:
print(f"Log file {current_log_file_path} not found. Waiting for it to be created...")
time.sleep(DELAY * 12) # Wait for a longer period if the file doesn't exist.
continue # Skip the rest of the loop and check again.
time.sleep(DELAY)
def get_current_log_file_path():
current_date = datetime.now()
log_file_name = current_date.strftime(LOG_FILE_FORMAT)
log_file_path = os.path.join(LOG_FILE_DIR, log_file_name)
return log_file_path
def get_end_of_log_file():
log_file_path = get_current_log_file_path()
try:
with open(log_file_path, 'r') as log_file:
log_file.seek(0, os.SEEK_END)
return log_file.tell()
except FileNotFoundError:
return 0
def send_message_to_discord(message, webhook_url):
data = {
'embeds': [
{
'description': message,
'color': 16777215 # You can change the color of the embed here.
}
]
}
try:
response = requests.post(webhook_url, json=data)
if response.status_code != 204:
print(f'Error sending message to Discord: {response.status_code}')
print(response.text)
except Exception as e:
print(f'Error sending message to Discord: {e}')
def get_webhook_url():
try:
with open(CONFIG_FILE, 'r') as config_file:
config_data = json.load(config_file)
return config_data['webhook_url']
except FileNotFoundError as e:
print(f'Error: {e}. Check if the config file exists and has the correct format.')
exit(1)
except KeyError as e:
print(f'Error: {e}. Check if the webhook_url key exists in the config file.')
exit(1)
if __name__ == '__main__':
try:
import requests
except ImportError:
print('requests module not found. Installing it now...')
os.system('pip install requests')
import requests
main()