-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
160 lines (135 loc) · 5.47 KB
/
main.py
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
import argparse
import requests
import subprocess
import time
import csv
import sys
import os
from generator import generate_video, generate_video_script
def parse_project(project_data):
return {
"id": project_data["id"],
"title": project_data["title"],
"preview": project_data["preview"],
"description": project_data["description"],
"techTags": ', '.join(tag["name"] for tag in project_data.get("techTags", [])),
"domainTags": ', '.join(tag["name"] for tag in project_data.get("domainTags", [])),
"thumbnail": project_data.get("thumbnail", {}).get("url") if project_data.get("thumbnail") else None
}
def fetch_sundai_project_data(project_id):
"""Fetch project data from the Sundai API"""
url = f"https://www.sundai.club/api/projects/{project_id}"
response = requests.get(url)
if response.status_code == 200:
return parse_project(response.json())
else:
raise Exception(f"Failed to fetch project data: {response.status_code}")
def all_projects():
"""Fetch all projects from the Sundai API"""
url = "https://www.sundai.club/api/projects"
response = requests.get(url)
if response.status_code == 200:
return [
parse_project(project) for project in response.json()
]
else:
raise Exception(f"Failed to fetch projects: {response.status_code}")
def main():
os.makedirs("output", exist_ok=True)
parser = argparse.ArgumentParser(description="Process project ID")
parser.add_argument(
"--project-id",
type=str,
nargs='?',
help="Project ID to process, for example 34dffa61-f90e-4184-a034-7bb5ab4f0981",
)
parser.add_argument(
"--dump-all-projects",
action="store_true",
help="Export all projects to CSV file",
)
parser.add_argument(
"--no-video",
action="store_true",
help="Skip video generation and only create the script",
)
args = parser.parse_args()
if args.dump_all_projects:
try:
print("Fetching all projects...")
projects = all_projects()
csv_file = "output/all_projects.csv"
with open(csv_file, 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow(['id', 'title', 'preview', 'description', 'techTags', 'domainTags'])
for project in projects:
writer.writerow([
project['id'],
project['title'],
project['preview'],
project['description'],
project['techTags'],
project['domainTags']
])
print(f"Successfully exported {len(projects)} projects to {csv_file}")
sys.exit(0)
except Exception as e:
print(f"Error exporting projects: {str(e)}")
sys.exit(1)
if not args.project_id:
parser.error("project_id is required when not using --dump-all-projects")
project_id = args.project_id
print(f"Processing project ID: {project_id}")
try:
# Fetch project data
print(f"Fetching project {project_id} data...")
project_data = fetch_sundai_project_data(project_id)
print(f"Fetched project: {project_data.get('title', 'N/A')}")
timestamp = int(time.time())
# Generate video script
print("Generating video script...")
script = generate_video_script(project_data)
# Save script to local file
print("Saving video script to local file...")
script_filename = f"output/{project_data.get('id')}-{project_data.get('title')}-{timestamp}.txt"
with open(script_filename, 'w') as f:
f.write(script)
print(f"Saved script to {script_filename}")
# Print the generated script
print("Generated video script:")
print("=" * 50)
print(script)
print("=" * 50)
if not args.no_video:
# Generate video
print("\nGenerating video...")
thumbnail = project_data.get('thumbnail')
thumbnail = None
video_url = generate_video(script, thumbnail)
print(f"Video URL: {video_url}")
# Download video
print("\nDownloading video...")
video_filename = f"output/{project_data.get('id')}-{project_data.get('title')}-{timestamp}.mp4"
video_response = requests.get(video_url, stream=True)
with open(video_filename, 'wb') as f:
for chunk in video_response.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
print(f"Downloaded video to {video_filename}")
# Convert video to animated GIF
print("Converting video to animated GIF...")
gif_filename = f"output/{project_data.get('id')}-{project_data.get('title')}-{timestamp}.gif"
subprocess.run([
"ffmpeg",
"-i", video_filename,
"-vf", "fps=10,scale=320:-1:flags=lanczos",
"-c:v", "gif",
gif_filename
])
print(f"Saved animated GIF to {gif_filename}")
else:
print("\nSkipping video generation as --no-video flag was provided")
except Exception as e:
print(f"Error: {str(e)}") # Add your project processing logic here
if __name__ == "__main__":
main()