-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
135 lines (109 loc) · 3.9 KB
/
main.py
File metadata and controls
135 lines (109 loc) · 3.9 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
"""
DevShare Main Module
This script runs the DevShare application, connecting to the cloud service
for handling screenshots sent from your phone via Telegram.
"""
import os
import signal
import sys
import json
from modules.ui import ScreenshotUI
from modules.cloud_service import CloudService
from modules.setup_wizard import SetupWizard
from modules.utils import load_history
def signal_handler(sig, frame):
"""Handle Ctrl+C to properly clean up resources."""
print("Shutting down...")
if 'cloud_service' in globals() and cloud_service:
cloud_service.disconnect()
if 'ui' in globals() and ui:
ui.stop()
sys.exit(0)
def check_first_run():
"""Check if this is the first run of the application"""
config_file = './config.json'
# If config file doesn't exist, it's first run
if not os.path.exists(config_file):
return True
# If config file exists but is incomplete
try:
with open(config_file, 'r') as f:
config = json.load(f)
if 'telegram_id' not in config or not config['telegram_id']:
return True
if 'service_url' not in config:
return True
return False
except:
return True
def run_setup_wizard():
"""Run the setup wizard and return configuration"""
wizard = SetupWizard()
wizard.show()
# Load the saved config
config_file = './config.json'
if os.path.exists(config_file):
with open(config_file, 'r') as f:
config = json.load(f)
return config.get('telegram_id', ''), config.get('service_url', '')
return None, None
def handle_new_screenshot(file_path, timestamp):
"""Handle new screenshots from the cloud service"""
global ui
# Update UI with new screenshot
if ui:
# Reload history
history = load_history()
ui.history = history
# Update the UI - allowing cooldown to manage refresh rate
ui.refresh_history()
# Just update status without triggering another refresh
ui.update_status(f"New screenshot received: {timestamp}")
def main():
global cloud_service, ui
# Register signal handler for clean shutdown
signal.signal(signal.SIGINT, signal_handler)
# Create screenshot directory if it doesn't exist
os.makedirs('./screenshots', exist_ok=True)
# Check if this is the first run
if check_first_run():
telegram_id, service_url = run_setup_wizard()
if not telegram_id:
print("Setup not completed. Exiting.")
sys.exit(1)
else:
# Load config
with open('./config.json', 'r') as f:
config = json.load(f)
telegram_id = config.get('telegram_id', '')
service_url = config.get('service_url', '')
# Connect to the cloud service
cloud_service = CloudService(telegram_id, callback=handle_new_screenshot)
if service_url:
cloud_service.service_url = service_url
# Initialize history
history = load_history()
# Create and start the UI
ui = ScreenshotUI(None) # No bot parameter, using cloud service instead
# Connect to cloud service
connected = cloud_service.connect()
if connected:
ui.update_status(f"Connected to cloud service. Ready for screenshots.")
else:
ui.update_status(f"Failed to connect to cloud service. Check your settings.")
try:
# Run the UI (this blocks until the UI is closed)
ui.run()
except Exception as e:
print(f"Error in UI: {e}")
finally:
# Clean up when the UI is closed
print("UI closed, shutting down application...")
if cloud_service:
cloud_service.disconnect()
# Exit the program to ensure everything is properly closed
sys.exit(0)
if __name__ == "__main__":
cloud_service = None
ui = None
main()