-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
272 lines (213 loc) · 8.64 KB
/
utils.py
File metadata and controls
272 lines (213 loc) · 8.64 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
"""
Utility Functions
Helper functions for file I/O, date parsing, and formatting.
"""
import json
import os
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List, Optional
import config
def clear_screen() -> None:
"""Clear terminal screen (cross-platform)."""
os.system('cls' if os.name == 'nt' else 'clear')
def print_startup_banner() -> None:
"""Print startup banner with ASCII logo and credits."""
clear_screen()
print("""
╔════════════════════════════════════════════════════════════════╗
║ ██████╗ █████╗ ████████╗██████╗ ███████╗ ██████╗ ███╗ ██╗ ║
║ ██╔══██╗██╔══██╗╚══██╔══╝██╔══██╗██╔════╝██╔═══██╗████╗ ██║ ║
║ ██████╔╝███████║ ██║ ██████╔╝█████╗ ██║ ██║██╔██╗ ██║ ║
║ ██╔═══╝ ██╔══██║ ██║ ██╔══██╗██╔══╝ ██║ ██║██║╚██╗██║ ║
║ ██║ ██║ ██║ ██║ ██║ ██║███████╗╚██████╔╝██║ ╚████║ ║
║ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝╚══════╝ ╚═════╝ ╚═╝ ╚═══╝ ║
║ ║
║ Video Extractor v1.0 ║
║ Extract Vimeo & YouTube URLs from Patreon ║
║ ║
║ Created by: github.com/evenwebb ║
╚════════════════════════════════════════════════════════════════╝
""")
def save_results_to_json(
data: Dict[str, Any],
creator_vanity: str,
output_dir: Optional[str] = None
) -> str:
"""
Save scraping results to a JSON file.
Args:
data: Dictionary containing scraping results
creator_vanity: Creator's vanity URL (used in filename)
output_dir: Output directory path (uses config.OUTPUT_DIR if None)
Returns:
Path to the saved file
"""
if output_dir is None:
output_dir = config.OUTPUT_DIR
output_path = Path(output_dir)
# Add creator subfolder if configured
if config.OUTPUT_ORGANIZE_BY_CREATOR:
output_path = output_path / creator_vanity
# Create output directory if it doesn't exist
output_path.mkdir(parents=True, exist_ok=True)
# Generate filename with timestamp
timestamp = datetime.now().strftime(config.TIMESTAMP_FORMAT)
filename = config.OUTPUT_FILENAME_FORMAT.format(
creator_vanity=creator_vanity,
timestamp=timestamp
)
filepath = output_path / filename
# Save to file
with filepath.open('w', encoding='utf-8') as f:
json.dump(data, f, indent=2, ensure_ascii=False)
return str(filepath)
def save_raw_urls_to_txt(
urls: List[str],
creator_vanity: str,
output_dir: Optional[str] = None,
deduplicate: bool = True
) -> str:
"""
Save raw video URLs to a text file (one URL per line).
Args:
urls: List of video URLs
creator_vanity: Creator's vanity URL (used in filename)
output_dir: Output directory path (uses config.OUTPUT_DIR if None)
deduplicate: Whether to deduplicate URLs before saving
Returns:
Path to the saved file
"""
if output_dir is None:
output_dir = config.OUTPUT_DIR
output_path = Path(output_dir)
# Add creator subfolder if configured
if config.OUTPUT_ORGANIZE_BY_CREATOR:
output_path = output_path / creator_vanity
# Create output directory if it doesn't exist
output_path.mkdir(parents=True, exist_ok=True)
# Deduplicate if requested
if deduplicate:
urls = list(dict.fromkeys(urls)) # Preserves order while deduplicating
# Generate filename with timestamp (same as JSON but .txt)
timestamp = datetime.now().strftime(config.TIMESTAMP_FORMAT)
filename = f"{creator_vanity}_{timestamp}.txt"
filepath = output_path / filename
# Save to file (one URL per line)
with filepath.open('w', encoding='utf-8') as f:
for url in urls:
f.write(f"{url}\n")
return str(filepath)
def parse_date_input(date_str: str) -> datetime:
"""
Parse a date string into a datetime object.
Supports formats:
- YYYY-MM-DD
- YYYY/MM/DD
- DD-MM-YYYY
- DD/MM/YYYY
Args:
date_str: Date string to parse
Returns:
datetime object
Raises:
ValueError: If date format is not recognized
"""
if not date_str:
raise ValueError("Empty date string")
# Try different formats
formats = [
"%Y-%m-%d",
"%Y/%m/%d",
"%d-%m-%Y",
"%d/%m/%Y",
]
for fmt in formats:
try:
return datetime.strptime(date_str, fmt)
except ValueError:
continue
raise ValueError(f"Could not parse date: {date_str}. Expected format: YYYY-MM-DD")
def format_post_for_output(post: dict, video_urls: List[str]) -> dict:
"""
Format a post dictionary for JSON output.
Args:
post: Post dictionary from API
video_urls: List of video URLs (Vimeo and YouTube) extracted from the post
Returns:
Formatted dictionary with relevant fields
"""
attrs = post.get('attributes', {})
return {
'post_id': post.get('id'),
'title': attrs.get('title', 'Untitled'),
'post_type': attrs.get('post_type'),
'published_at': attrs.get('published_at'),
'url': attrs.get('url', f"https://www.patreon.com/posts/{post.get('id')}"),
'video_urls': video_urls
}
def print_banner(text: str) -> None:
"""
Print a formatted banner.
Args:
text: Text to display in banner
"""
width = max(60, len(text) + 4)
print()
print("=" * width)
print(f" {text}")
print("=" * width)
print()
def print_creator_list(creators: List[Dict[str, str]]) -> None:
"""
Print a formatted list of creators with box borders.
Args:
creators: List of creator dictionaries
"""
print()
print("╭─────────────────────── Subscribed Creators ────────────────────────╮")
for i, creator in enumerate(creators, 1):
name = creator['name'][:28] # Truncate if too long
vanity = creator['vanity'][:15] # Truncate if too long
name_vanity = f"{name:28s} (@{vanity:15s})"
# Check if compatibility was checked and mark incompatible creators
if 'compatible' in creator and not creator['compatible']:
print(f"│ {i:2d}. {name_vanity} ⚠️ [NOT SUPPORTED] │")
else:
print(f"│ {i:2d}. {name_vanity:52s} │")
print("╰─────────────────────────────────────────────────────────────────────╯")
# Show legend if any incompatible creators found
if any('compatible' in c and not c['compatible'] for c in creators):
print(" ⚠️ = Creator Website format (Patreon-hosted videos, not supported)")
print()
def confirm_action(prompt: str) -> bool:
"""
Ask user for yes/no confirmation.
Args:
prompt: Confirmation question
Returns:
True if user confirms, False otherwise
"""
if config.AUTO_CONFIRM:
return True
while True:
response = input(f"{prompt} (y/n): ").strip().lower()
if response in ['y', 'yes']:
return True
elif response in ['n', 'no']:
return False
else:
print("Please enter 'y' or 'n'")
def format_file_size(size_bytes: int) -> str:
"""
Format file size in human-readable format.
Args:
size_bytes: Size in bytes
Returns:
Formatted string (e.g., "1.5 MB")
"""
for unit in ['B', 'KB', 'MB', 'GB']:
if size_bytes < 1024.0:
return f"{size_bytes:.1f} {unit}"
size_bytes /= 1024.0
return f"{size_bytes:.1f} TB"