-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflushfreetube1.7.py
More file actions
97 lines (87 loc) · 3.38 KB
/
Copy pathflushfreetube1.7.py
File metadata and controls
97 lines (87 loc) · 3.38 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
import os
import shutil
import time
import subprocess
import pyautogui
from scapy.all import sniff, TCP
CACHE_DIR = os.path.expanduser("~/.config/FreeTube/Cache")
YOUTUBE_DOMAINS = ["youtube.com", "google.com"]
def clear_cache():
"""Clears the FreeTube cache directory."""
if os.path.exists(CACHE_DIR):
try:
shutil.rmtree(CACHE_DIR)
os.makedirs(CACHE_DIR)
print("[INFO] Cache cleared.")
except Exception as e:
print(f"[ERROR] Error clearing cache: {e}")
def get_focused_window():
"""Returns the title of the currently focused window using wmctrl."""
try:
result = subprocess.run(
["wmctrl", "-lp"],
stdout=subprocess.PIPE,
text=True
)
if result.returncode != 0:
print("[ERROR] Failed to retrieve focused window.")
return None
active_window = subprocess.check_output(["xdotool", "getwindowfocus", "getwindowname"], text=True).strip()
print(f"[DEBUG] Focused window: {active_window}")
return active_window
except Exception as e:
print(f"[ERROR] Unable to get focused window: {e}")
return None
def is_freetube_window():
"""Checks if the focused window belongs to FreeTube."""
focused_window = get_focused_window()
is_focused = focused_window and "FreeTube" in focused_window
print(f"[DEBUG] Is FreeTube window focused: {is_focused}")
return is_focused
def refresh_freetube():
"""Simulates Ctrl+Shift+R to refresh FreeTube using pyautogui."""
try:
pyautogui.hotkey('ctrl', 'shift', 'r')
print("[INFO] Refreshed FreeTube.")
except Exception as e:
print(f"[ERROR] Failed to refresh FreeTube: {e}")
def packet_callback(packet):
"""Callback function to handle packets."""
try:
if packet.haslayer(TCP):
# Extract the destination host if available
if packet.haslayer("IP"):
host = packet["IP"].dst
elif packet.haslayer("IPv6"):
host = packet["IPv6"].dst
else:
return
# Check if the host matches YouTube or Google domains
for domain in YOUTUBE_DOMAINS:
if domain in str(host):
print(f"[DEBUG] Detected HTTPS request to {host}")
if is_freetube_window():
print("[INFO] FreeTube is focused. Refreshing...")
clear_cache()
time.sleep(4) # Allow cache to clear before refreshing
refresh_freetube()
else:
print("[INFO] FreeTube is not focused. Skipping refresh.")
break
except Exception as e:
print(f"[ERROR] Exception in packet processing: {e}")
def monitor_https_requests():
"""Monitors network traffic for HTTPS requests to or from Google."""
print("[INFO] Monitoring HTTPS requests...")
try:
sniff(filter="tcp port 443", prn=packet_callback, store=False)
except KeyboardInterrupt:
print("[INFO] Stopping network monitor.")
except Exception as e:
print(f"[ERROR] Error monitoring network traffic: {e}")
if __name__ == "__main__":
try:
print("[INFO] Starting FreeTube HTTPS monitor...")
monitor_https_requests()
except KeyboardInterrupt:
print("[INFO] Exiting...")