-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
261 lines (216 loc) · 9.42 KB
/
bot.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import os
import logging
import sqlite3
import requests
from requests.auth import HTTPDigestAuth
import xml.etree.ElementTree as ET
import urllib.parse
import time
# Environment variables
webdav_url = os.getenv("WEBDAV_URL")
webdav_login = os.getenv("WEBDAV_LOGIN")
webdav_password = os.getenv("WEBDAV_PASSWORD")
discord_webhook = os.getenv("DISCORD_WEBHOOK")
coursefolders_path = os.getenv("COURSEFOLDERS_PATH")
# Logging configuration
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# Session initialization for preemptive digest authentication
def initialize_session():
global session
session = requests.Session()
session.auth = HTTPDigestAuth(webdav_login, webdav_password)
# Initialize session
initialize_session()
# Function to handle requests with session checks
def make_authenticated_request(method, url, **kwargs):
global session
response = session.request(method, url, **kwargs)
if response.status_code == 401:
initialize_session()
response = session.request(method, url, **kwargs)
if response.status_code == 401:
raise Exception("Failed to re-authenticate")
return response
# Database initialization
conn = sqlite3.connect('/usr/src/app/database/files.db')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS files (
path TEXT PRIMARY KEY,
last_modified TEXT,
size TEXT
)
''')
# Create folders table
cursor.execute('''
CREATE TABLE IF NOT EXISTS folders (
path TEXT PRIMARY KEY
)
''')
conn.commit()
def get_webdav_items(path):
logging.debug(f"Getting WebDAV items for path: {path}")
url = f"{webdav_url}/{path}"
response = make_authenticated_request("PROPFIND", url, headers={'Depth': '1'})
if response.status_code != 207:
logging.error(f"Failed to access WebDAV directory: {path} with status code {response.status_code}")
return []
tree = ET.fromstring(response.content)
items = []
for response in tree.findall('{DAV:}response'):
href = response.find('{DAV:}href').text
is_directory = response.find('.//{DAV:}collection') is not None
items.append((href, is_directory))
return items
def get_parent_folders(path, base_path):
"""Return a string representing the parent folders of a given path, excluding the base path."""
if path.startswith(base_path):
path = path[len(base_path):].lstrip('/')
folders = path.split('/')
if len(folders) > 1:
return ' -> '.join(folders[:-1])
return "Root"
def notify_discord_new_folder(folder_path):
# Remove the base path from the folder path
display_path = folder_path.replace(coursefolders_path, '').lstrip('/').rstrip('/')
if not display_path: # Skip notification if the path is just the base path
return
# Check if the folder is already in the database
cursor.execute("SELECT path FROM folders WHERE path = ?", (folder_path,))
if cursor.fetchone() is not None:
# Folder already processed
logging.debug(f"Folder already processed: {folder_path}")
return
# Construct the message for Discord
message = {
"content": f"New folder detected: {display_path}"
}
# Send the notification to Discord
discord_response = requests.post(discord_webhook, json=message)
if discord_response.status_code in [200, 204]:
logging.info(f"Notification sent to Discord for new folder: {display_path}")
# Record the folder in the database
cursor.execute("INSERT INTO folders (path) VALUES (?)", (folder_path,))
conn.commit()
else:
logging.error(f"Failed to send folder notification to Discord: {discord_response.status_code}, {discord_response.text}")
def get_folder_path(path, base_path):
"""Return the folder path excluding the base path."""
if path.startswith(base_path):
path = path[len(base_path):].lstrip('/')
return os.path.dirname(path)
def notify_discord_new_file(path, last_modified=None):
"""Send a notification to Discord about a new file with the file attached."""
folder_path = get_folder_path(path, coursefolders_path)
file_url = f"{webdav_url}/{urllib.parse.quote(path)}"
filename = os.path.basename(path)
# Download the file to attach it to Discord
response = make_authenticated_request("GET", file_url)
if response.status_code == 200:
with open(filename, 'wb') as f:
f.write(response.content)
# Preparing Discord message
message = {
"content": f"New file detected in folder: {folder_path}\nName: {filename}"
}
files = {
"file": (filename, open(filename, 'rb'))
}
# Sending the message
discord_response = requests.post(discord_webhook, data=message, files=files)
if discord_response.status_code in [200, 204]:
logging.info(f"Notification sent to Discord for new file: {filename}")
else:
logging.error(f"Failed to send file notification to Discord: {discord_response.status_code}, {discord_response.text}")
# Clean up the downloaded file
os.remove(filename)
else:
logging.error(f"Failed to download file for Discord notification: {response.status_code}, {response.text}")
def process_webdav_directory(relative_path, processed_paths):
logging.debug(f"Entering directory: {relative_path}")
if relative_path in processed_paths:
logging.debug(f"Already processed: {relative_path}")
return
processed_paths.add(relative_path)
items = get_webdav_items(relative_path)
for href, is_directory in items:
corrected_path = urllib.parse.unquote(href.replace(webdav_url, '').lstrip('/'))
logging.debug(f"Found item: {corrected_path}, Is directory: {is_directory}")
if corrected_path == relative_path or corrected_path in processed_paths:
logging.debug(f"Skipping: {corrected_path}")
continue
# Adjusted to handle path correctly
if corrected_path.startswith('webdav/'):
corrected_path = corrected_path[len('webdav/'):]
if is_directory:
logging.debug(f"Processing subdirectory: {corrected_path}")
if corrected_path not in processed_paths:
notify_discord_new_folder(corrected_path)
process_webdav_directory(corrected_path, processed_paths)
else:
process_file(corrected_path)
def process_file(path):
logging.debug(f"Processing file: {path}")
url = f"{webdav_url}/{path}"
response = make_authenticated_request("HEAD", url)
if response.status_code != 200:
logging.error(f"Failed to access file: {path} with status code {response.status_code}")
return
last_modified = response.headers.get('Last-Modified', None)
cursor.execute("SELECT last_modified FROM files WHERE path = ?", (path,))
result = cursor.fetchone()
if not result:
# New file, send a notification
logging.info(f"New file detected: {path}")
notify_discord_new_file(path)
# Insert into database
insert_value = last_modified if last_modified else "no_modif_Date"
cursor.execute("INSERT INTO files (path, last_modified) VALUES (?, ?)", (path, insert_value))
elif last_modified:
if result[0] != last_modified and result[0] != "no_modif_Date":
# File updated
notify_discord_updated_file(path, last_modified)
cursor.execute("UPDATE files SET last_modified = ? WHERE path = ?", (last_modified, path))
conn.commit()
def notify_discord_updated_file(path, last_modified=None):
"""Send a notification to Discord about an updated file with the file attached."""
folder_path = get_folder_path(path, coursefolders_path)
file_url = f"{webdav_url}/{urllib.parse.quote(path)}"
filename = os.path.basename(path)
# Download the file to attach it to Discord
response = make_authenticated_request("GET", file_url)
if response.status_code == 200:
with open(filename, 'wb') as f:
f.write(response.content)
# Preparing Discord message
last_modified_info = f"\nLast Modified: {last_modified}" if last_modified else ""
message_content = f"File updated in folder: {folder_path}\nName: {filename}{last_modified_info}"
message = {
"content": message_content
}
files = {
"file": (filename, open(filename, 'rb'))
}
# Sending the message
discord_response = requests.post(discord_webhook, data=message, files=files)
if discord_response.status_code in [200, 204]:
logging.info(f"Notification sent to Discord for updated file: {filename}")
else:
logging.error(f"Failed to send updated file notification to Discord: {discord_response.status_code}, {discord_response.text}")
# Clean up the downloaded file
os.remove(filename)
else:
logging.error(f"Failed to download file for Discord notification: {response.status_code}, {response.text}")
def main():
while True:
processed_paths = set()
process_webdav_directory(coursefolders_path, processed_paths)
logging.info("Waiting for 2.5 minutes before next run.")
time.sleep(150)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
logging.info("Script interrupted by user")
except Exception as e:
logging.error(f"An error occurred: {e}")