-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
312 lines (249 loc) · 9.7 KB
/
Copy pathsetup.py
File metadata and controls
312 lines (249 loc) · 9.7 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
"""
Pinterest Analytics Setup Wizard
Interactive setup for easy configuration
"""
import os
import sys
from pathlib import Path
try:
from rich.console import Console
from rich.panel import Panel
from rich.prompt import Prompt, Confirm
from rich.table import Table
RICH_AVAILABLE = True
except ImportError:
RICH_AVAILABLE = False
console = Console() if RICH_AVAILABLE else None
def print_header():
"""Print setup wizard header"""
if RICH_AVAILABLE:
console.print(Panel.fit(
"[bold red]Pinterest Analytics[/bold red]\n[dim]Setup Wizard[/dim]",
border_style="red"
))
else:
print("\n" + "="*50)
print(" Pinterest Analytics - Setup Wizard")
print("="*50 + "\n")
def check_env_file() -> dict:
"""Check existing .env configuration"""
env_path = Path('.env')
config = {
'PINTEREST_ACCESS_TOKEN': None,
'PINTEREST_APP_ID': None,
'PINTEREST_APP_SECRET': None,
}
if env_path.exists():
with open(env_path, 'r') as f:
for line in f:
line = line.strip()
if '=' in line and not line.startswith('#'):
key, value = line.split('=', 1)
key = key.strip()
value = value.strip().strip('"').strip("'")
if key in config and value:
config[key] = value
return config
def save_env_file(config: dict):
"""Save configuration to .env file"""
env_path = Path('.env')
lines = []
lines.append("# Pinterest Analytics Configuration")
lines.append("# Generated by setup wizard")
lines.append("")
if config.get('PINTEREST_ACCESS_TOKEN'):
lines.append(f"PINTEREST_ACCESS_TOKEN={config['PINTEREST_ACCESS_TOKEN']}")
if config.get('PINTEREST_APP_ID'):
lines.append(f"PINTEREST_APP_ID={config['PINTEREST_APP_ID']}")
if config.get('PINTEREST_APP_SECRET'):
lines.append(f"PINTEREST_APP_SECRET={config['PINTEREST_APP_SECRET']}")
with open(env_path, 'w') as f:
f.write('\n'.join(lines) + '\n')
return env_path
def get_input(prompt: str, default: str = None, password: bool = False) -> str:
"""Get user input with optional default"""
if RICH_AVAILABLE:
return Prompt.ask(prompt, default=default or "", password=password)
else:
if default:
prompt = f"{prompt} [{default}]"
value = input(f"{prompt}: ")
return value if value else (default or "")
def confirm(prompt: str, default: bool = True) -> bool:
"""Get yes/no confirmation"""
if RICH_AVAILABLE:
return Confirm.ask(prompt, default=default)
else:
response = input(f"{prompt} [{'Y/n' if default else 'y/N'}]: ").strip().lower()
if not response:
return default
return response in ('y', 'yes')
def print_step(step: int, total: int, message: str):
"""Print step indicator"""
if RICH_AVAILABLE:
console.print(f"\n[bold cyan]Step {step}/{total}:[/bold cyan] {message}")
else:
print(f"\n[Step {step}/{total}] {message}")
def print_success(message: str):
"""Print success message"""
if RICH_AVAILABLE:
console.print(f"[green]✓[/green] {message}")
else:
print(f"[OK] {message}")
def print_warning(message: str):
"""Print warning message"""
if RICH_AVAILABLE:
console.print(f"[yellow]![/yellow] {message}")
else:
print(f"[!] {message}")
def print_error(message: str):
"""Print error message"""
if RICH_AVAILABLE:
console.print(f"[red]✗[/red] {message}")
else:
print(f"[ERROR] {message}")
def run_setup_wizard():
"""Run the interactive setup wizard"""
print_header()
# Check current configuration
current_config = check_env_file()
has_token = bool(current_config.get('PINTEREST_ACCESS_TOKEN'))
has_app = bool(current_config.get('PINTEREST_APP_ID'))
if has_token:
print_success("Access token found in .env")
if not confirm("Do you want to reconfigure?", default=False):
print("\nSetup complete! Run: python main.py test")
return
# Step 1: Introduction
print_step(1, 4, "Pinterest Developer App")
if RICH_AVAILABLE:
console.print("""
To use Pinterest Analytics, you need:
1. [bold]Pinterest Developer App[/bold]
→ Go to: [link]https://developers.pinterest.com/apps/[/link]
→ Click "Create app"
→ Fill in app details:
• App name: Pinterest Analytics
• Description: Personal analytics tool
• Website URL: https://github.com/xodapi/pin
• Privacy Policy: https://github.com/xodapi/pin/blob/main/PRIVACY.md
2. [bold]Get your credentials[/bold]
→ After approval, copy App ID and App Secret
""")
else:
print("""
To use Pinterest Analytics, you need:
1. Pinterest Developer App
→ Go to: https://developers.pinterest.com/apps/
→ Click "Create app"
2. Get your credentials
→ After approval, copy App ID and App Secret
""")
if not confirm("Have you created a Pinterest Developer App?"):
print("\nPlease create an app first:")
print(" https://developers.pinterest.com/apps/")
print("\nThen run: python setup.py")
return
# Step 2: App credentials
print_step(2, 4, "Enter App Credentials")
app_id = get_input(
"App ID",
default=current_config.get('PINTEREST_APP_ID', '')
)
app_secret = get_input(
"App Secret",
default=current_config.get('PINTEREST_APP_SECRET', ''),
password=True
)
# Step 3: Access Token
print_step(3, 4, "Access Token")
if RICH_AVAILABLE:
console.print("""
[bold]Option A:[/bold] If you have an access token, enter it now
[bold]Option B:[/bold] Leave empty to generate one using OAuth
""")
else:
print("\nOption A: Enter existing access token")
print("Option B: Leave empty to generate via OAuth")
access_token = get_input(
"Access Token (or leave empty)",
default=current_config.get('PINTEREST_ACCESS_TOKEN', ''),
password=True
)
# Step 4: Save configuration
print_step(4, 4, "Save Configuration")
new_config = {
'PINTEREST_APP_ID': app_id,
'PINTEREST_APP_SECRET': app_secret,
'PINTEREST_ACCESS_TOKEN': access_token,
}
# Show summary
if RICH_AVAILABLE:
table = Table(title="Configuration Summary")
table.add_column("Setting", style="cyan")
table.add_column("Value")
table.add_row("App ID", app_id[:10] + "..." if app_id else "[red]Not set[/red]")
table.add_row("App Secret", "****" if app_secret else "[red]Not set[/red]")
table.add_row("Access Token", "****" if access_token else "[yellow]Will generate[/yellow]")
console.print(table)
else:
print("\nConfiguration Summary:")
print(f" App ID: {app_id[:10] + '...' if app_id else 'Not set'}")
print(f" App Secret: {'****' if app_secret else 'Not set'}")
print(f" Access Token: {'****' if access_token else 'Will generate'}")
if confirm("\nSave this configuration?"):
env_path = save_env_file(new_config)
print_success(f"Configuration saved to {env_path}")
# Next steps
if RICH_AVAILABLE:
console.print("\n[bold green]Setup Complete![/bold green]\n")
if not access_token:
console.print("Next step - generate access token:")
console.print(" [cyan]python get_token.py[/cyan]\n")
console.print("Then test your connection:")
console.print(" [cyan]python main.py test[/cyan]\n")
console.print("Quick start commands:")
console.print(" [cyan]python main.py quick[/cyan] # Fast status")
console.print(" [cyan]python main.py dashboard[/cyan] # Web UI")
else:
print("\nSetup Complete!\n")
if not access_token:
print("Next: python get_token.py")
print("Test: python main.py test")
else:
print("\nSetup cancelled.")
def quick_test():
"""Quick test of current configuration"""
config = check_env_file()
if RICH_AVAILABLE:
console.print("\n[bold]Configuration Status:[/bold]\n")
table = Table()
table.add_column("Setting", style="cyan")
table.add_column("Status")
table.add_row(
"Access Token",
"[green]✓ Set[/green]" if config.get('PINTEREST_ACCESS_TOKEN') else "[red]✗ Missing[/red]"
)
table.add_row(
"App ID",
"[green]✓ Set[/green]" if config.get('PINTEREST_APP_ID') else "[yellow]○ Optional[/yellow]"
)
table.add_row(
"App Secret",
"[green]✓ Set[/green]" if config.get('PINTEREST_APP_SECRET') else "[yellow]○ Optional[/yellow]"
)
console.print(table)
if config.get('PINTEREST_ACCESS_TOKEN'):
console.print("\n[green]Ready to use![/green] Run: python main.py test")
else:
console.print("\n[yellow]Setup required.[/yellow] Run: python setup.py")
else:
print("\nConfiguration Status:")
print(f" Access Token: {'Set' if config.get('PINTEREST_ACCESS_TOKEN') else 'MISSING'}")
print(f" App ID: {'Set' if config.get('PINTEREST_APP_ID') else 'Not set'}")
print(f" App Secret: {'Set' if config.get('PINTEREST_APP_SECRET') else 'Not set'}")
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == 'check':
quick_test()
else:
run_setup_wizard()