-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.py
More file actions
315 lines (250 loc) · 11.2 KB
/
Copy pathtools.py
File metadata and controls
315 lines (250 loc) · 11.2 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import datetime
import os
import subprocess
import threading
import time
import webbrowser
import psutil
import pyautogui
import requests
from ddgs import DDGS
INBOX = os.path.join(os.path.dirname(__file__), "inbox")
pyautogui.FAILSAFE = False
# ── Web ──────────────────────────────────────────────────────────────────────
def web_search(query: str, max_results: int = 5) -> str:
with DDGS() as ddgs:
results = list(ddgs.text(query, max_results=max_results))
if not results:
return "No results found."
return "\n\n".join(f"{r['title']}: {r['body']} ({r['href']})" for r in results)
def get_weather(city: str = "San Antonio") -> str:
try:
r = requests.get(f"https://wttr.in/{city}?format=3", timeout=5)
return r.text.strip()
except Exception as e:
return f"Couldn't get weather: {e}"
# ── Files ─────────────────────────────────────────────────────────────────────
def read_file(path: str) -> str:
try:
with open(path, encoding="utf-8") as f:
return f.read()
except Exception as e:
return f"Error: {e}"
def write_file(path: str, content: str) -> str:
try:
os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)
with open(path, "w", encoding="utf-8") as f:
f.write(content)
return f"Written to {path}"
except Exception as e:
return f"Error: {e}"
def list_directory(path: str = "C:\\Users\\Levitikus") -> str:
try:
items = os.listdir(path)
dirs = [f"[folder] {i}" for i in items if os.path.isdir(os.path.join(path, i))]
files = [f"[file] {i}" for i in items if os.path.isfile(os.path.join(path, i))]
return "\n".join(dirs + files) or "Empty."
except Exception as e:
return f"Error: {e}"
# ── Inbox ─────────────────────────────────────────────────────────────────────
def list_inbox() -> str:
files = [f for f in os.listdir(INBOX) if f != ".gitkeep"]
return "Files in inbox: " + ", ".join(files) if files else "The inbox is empty."
def read_inbox_file(filename: str) -> str:
path = os.path.join(INBOX, filename)
if not os.path.exists(path):
return f"'{filename}' not found. Inbox has: {list_inbox()}"
ext = os.path.splitext(filename)[1].lower()
if ext == ".pdf":
try:
from pypdf import PdfReader
return "\n".join(p.extract_text() or "" for p in PdfReader(path).pages)
except Exception as e:
return f"PDF error: {e}"
if ext in (".docx", ".doc"):
try:
from docx import Document
return "\n".join(p.text for p in Document(path).paragraphs)
except Exception as e:
return f"Word error: {e}"
try:
with open(path, encoding="utf-8") as f:
return f.read()
except Exception as e:
return f"Error: {e}"
# ── Apps & Browser ────────────────────────────────────────────────────────────
APP_ALIASES = {
"chrome": "chrome.exe", "google chrome": "chrome.exe", "browser": "chrome.exe",
"firefox": "firefox.exe", "edge": "msedge.exe",
"notepad": "notepad.exe", "calculator": "calc.exe", "calc": "calc.exe",
"explorer": "explorer.exe", "file explorer": "explorer.exe",
"spotify": "spotify.exe", "discord": "discord.exe",
"vs code": "code.exe", "vscode": "code.exe",
"word": "winword.exe", "excel": "excel.exe", "powerpoint": "powerpnt.exe",
"paint": "mspaint.exe", "task manager": "taskmgr.exe",
"settings": "ms-settings:", "terminal": "wt.exe", "powershell": "powershell.exe",
}
def open_app(name: str) -> str:
exe = APP_ALIASES.get(name.lower(), name)
try:
subprocess.Popen(exe, shell=True)
return f"Opened {name}"
except Exception as e:
return f"Couldn't open {name}: {e}"
def open_url(url: str) -> str:
if not url.startswith("http"):
url = "https://" + url
webbrowser.open(url)
return f"Opened {url}"
def close_app(name: str) -> str:
killed = 0
for proc in psutil.process_iter(["name"]):
if name.lower() in proc.info["name"].lower():
try:
proc.kill()
killed += 1
except Exception:
pass
return f"Closed {killed} process(es) matching '{name}'." if killed else f"No running app found matching '{name}'."
def get_running_apps() -> str:
names = sorted({p.info["name"] for p in psutil.process_iter(["name"]) if p.info["name"]})
return ", ".join(names)
# ── Volume & Media ────────────────────────────────────────────────────────────
def get_volume() -> str:
try:
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
vol = cast(interface, POINTER(IAudioEndpointVolume))
level = round(vol.GetMasterVolumeLevelScalar() * 100)
muted = vol.GetMute()
return f"Volume is at {level}%{' (muted)' if muted else ''}."
except Exception as e:
return f"Couldn't get volume: {e}"
def set_volume(level: int) -> str:
try:
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
vol = cast(interface, POINTER(IAudioEndpointVolume))
vol.SetMasterVolumeLevelScalar(max(0, min(100, level)) / 100.0, None)
return f"Volume set to {level}%."
except Exception as e:
return f"Couldn't set volume: {e}"
def mute_volume() -> str:
try:
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
vol = cast(interface, POINTER(IAudioEndpointVolume))
vol.SetMute(1, None)
return "Muted."
except Exception as e:
return f"Error: {e}"
def unmute_volume() -> str:
try:
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
vol = cast(interface, POINTER(IAudioEndpointVolume))
vol.SetMute(0, None)
return "Unmuted."
except Exception as e:
return f"Error: {e}"
def media_control(action: str) -> str:
actions = {
"play": "playpause", "pause": "playpause", "play/pause": "playpause",
"next": "nexttrack", "skip": "nexttrack",
"previous": "prevtrack", "back": "prevtrack",
"stop": "stop",
}
key = actions.get(action.lower())
if not key:
return f"Unknown action '{action}'. Try: play, pause, next, previous, stop."
pyautogui.press(key)
return f"Media: {action}"
# ── Clipboard ─────────────────────────────────────────────────────────────────
def get_clipboard() -> str:
try:
import pyperclip
return pyperclip.paste() or "(clipboard is empty)"
except Exception as e:
return f"Error: {e}"
def set_clipboard(text: str) -> str:
try:
import pyperclip
pyperclip.copy(text)
return "Copied to clipboard."
except Exception as e:
return f"Error: {e}"
# ── Keyboard & Screen ─────────────────────────────────────────────────────────
def type_text(text: str) -> str:
try:
import pyperclip
pyperclip.copy(text)
time.sleep(0.3)
pyautogui.hotkey("ctrl", "v")
return f"Typed text."
except Exception as e:
return f"Error: {e}"
def press_hotkey(keys: str) -> str:
try:
key_list = [k.strip() for k in keys.replace("+", " ").split()]
pyautogui.hotkey(*key_list)
return f"Pressed {keys}."
except Exception as e:
return f"Error: {e}"
def take_screenshot() -> str:
try:
from PIL import ImageGrab
path = os.path.join(INBOX, "screenshot.png")
ImageGrab.grab().save(path)
return "Screenshot saved to inbox."
except Exception as e:
return f"Error: {e}"
# ── Notifications & Timers ────────────────────────────────────────────────────
def send_notification(title: str, message: str) -> str:
try:
from plyer import notification
notification.notify(title=title, message=message, timeout=8)
return "Notification sent."
except Exception as e:
return f"Error: {e}"
def set_timer(minutes: float, label: str = "Timer") -> str:
def _run():
time.sleep(minutes * 60)
send_notification(label, f"Your {minutes}-minute timer is up!")
threading.Thread(target=_run, daemon=True).start()
return f"Timer set for {minutes} minute{'s' if minutes != 1 else ''}."
# ── System ────────────────────────────────────────────────────────────────────
def get_datetime() -> str:
return datetime.datetime.now().strftime("%A, %B %d %Y — %I:%M %p")
def run_python(code: str) -> str:
try:
result = subprocess.run(
["python", "-c", code],
capture_output=True, text=True, timeout=10,
)
out = result.stdout
if result.stderr:
out += f"\nstderr: {result.stderr}"
return out.strip() or "(no output)"
except subprocess.TimeoutExpired:
return "Error: timed out (10s limit)"
except Exception as e:
return f"Error: {e}"
def shutdown_computer(action: str = "shutdown") -> str:
actions = {"shutdown": "shutdown /s /t 10", "restart": "shutdown /r /t 10", "sleep": "rundll32.exe powrprof.dll,SetSuspendState 0,1,0"}
cmd = actions.get(action.lower())
if not cmd:
return f"Unknown action. Use: shutdown, restart, or sleep."
subprocess.run(cmd, shell=True)
return f"Computer will {action} shortly."