3
3
from dotenv import load_dotenv
4
4
5
5
# Load environment variables
6
- load_dotenv ("../.env" )
6
+ # check if .env is in the folder or look one more level up
7
+ if os .path .exists (".env" ):
8
+ load_dotenv (".env" )
9
+ else :
10
+ load_dotenv ("../.env" )
7
11
# Check if all required environment variables are set
8
12
# This must happen before importing video which uses API keys without checking
9
13
check_env_vars ()
20
24
from apiclient .errors import HttpError
21
25
from flask import Flask , request , jsonify
22
26
from moviepy .config import change_settings
23
-
24
-
27
+ from classes .instagram_downloader import InstagramDownloader
25
28
26
29
# Set environment variables
27
30
SESSION_ID = os .getenv ("TIKTOK_SESSION_ID" )
30
33
31
34
32
35
# Initialize Flask
33
- app = Flask (__name__ , static_folder = "../ static" , static_url_path = "/static" )
36
+ app = Flask (__name__ , static_folder = "static" , static_url_path = "/static" )
34
37
CORS (app )
35
38
36
39
# Constants
41
44
42
45
# Create a method to create all the required folders
43
46
def create_folders ():
44
- # Create static folder if it doesn't exist
45
- if not os .path .exists ("../static" ):
46
- os .makedirs ("../static" )
47
- # Create static/Songs and static/generated_videos folder if it doesn't exist
48
- if not os .path .exists ("../static/assets" ):
49
- os .makedirs ("../static/assets" )
50
- if not os .path .exists ("../static/generated_videos" ):
51
- os .makedirs ("../static/generated_videos" )
52
-
47
+ """Create all required folders for the application"""
48
+ folders = [
49
+ "static" ,
50
+ "static/assets" ,
51
+ "static/assets/temp" ,
52
+ "static/assets/subtitles" ,
53
+ "static/generated_videos" ,
54
+ "static/generated_videos/instagram" ,
55
+ ]
56
+
57
+ for folder in folders :
58
+ folder_path = os .path .join (os .path .dirname (__file__ ), folder )
59
+ os .makedirs (folder_path , exist_ok = True )
60
+ print (f"Created/verified folder: { folder_path } " )
53
61
54
62
# Create folders
55
63
create_folders ()
56
64
65
+ # Instagram video download endpoint
66
+ @app .route ("/api/instagram/download" , methods = ["POST" ])
67
+ def download_instagram_video ():
68
+ try :
69
+ data = request .get_json ()
70
+ video_url = data .get ('url' )
71
+
72
+ if not video_url :
73
+ return jsonify ({
74
+ "status" : "error" ,
75
+ "message" : "No Instagram URL provided" ,
76
+ }), 400
77
+
78
+ # Initialize downloader with output path in static/assets
79
+ downloader = InstagramDownloader (output_path = os .path .join (os .path .dirname (__file__ ), "static/generated_videos/instagram" ))
80
+
81
+ # Download the video
82
+ result = downloader .download_video (video_url )
83
+
84
+ return jsonify ({
85
+ "status" : "success" ,
86
+ "message" : "Video downloaded successfully" ,
87
+ "data" : result
88
+ })
89
+
90
+ except Exception as e :
91
+ return jsonify ({
92
+ "status" : "error" ,
93
+ "message" : str (e ),
94
+ }), 500
95
+
57
96
58
97
# Generation Endpoint
59
98
@app .route ("/api/generate" , methods = ["POST" ])
@@ -64,8 +103,8 @@ def generate():
64
103
GENERATING = True
65
104
66
105
# Clean
67
- clean_dir ("../ static/assets/temp/" )
68
- clean_dir ("../ static/assets/subtitles/" )
106
+ clean_dir ("static/assets/temp/" )
107
+ clean_dir ("static/assets/subtitles/" )
69
108
70
109
71
110
# Parse JSON
@@ -209,7 +248,7 @@ def generate_script_only():
209
248
# Set generating to true
210
249
GENERATING = True
211
250
212
- clean_dir ("../ static/assets/subtitles/" )
251
+ clean_dir ("static/assets/subtitles/" )
213
252
print (colored ("[+] Received script request..." , "green" ))
214
253
215
254
data = request .get_json ()
@@ -245,8 +284,8 @@ def search_and_download():
245
284
global GENERATING
246
285
GENERATING = True
247
286
# Clean
248
- clean_dir ("../ static/assets/temp" )
249
- clean_dir ("../ static/assets/subtitles" )
287
+ clean_dir ("static/assets/temp" )
288
+ clean_dir ("static/assets/subtitles" )
250
289
251
290
252
291
print (colored ("[+] Received search and download request..." , "green" ))
@@ -280,6 +319,7 @@ def search_and_download():
280
319
281
320
videoClass .CombineVideos ()
282
321
322
+ # videoClass.GenerateMetadata()
283
323
videoClass .Stop ()
284
324
285
325
@@ -337,7 +377,7 @@ def addAudio():
337
377
# Get all available songs
338
378
@app .route ("/api/getSongs" , methods = ["GET" ])
339
379
def get_songs ():
340
- songs = os .listdir ("../ static/assets/music" )
380
+ songs = os .listdir (os . path . join ( os . path . dirname ( __file__ ), " static/assets/music") )
341
381
return jsonify ({
342
382
"status" : "success" ,
343
383
"message" : "Songs retrieved successfully!" ,
@@ -350,22 +390,25 @@ def get_songs():
350
390
@app .route ("/api/getVideos" , methods = ["GET" ])
351
391
def get_videos ():
352
392
# Get all videos mp4 only
353
- videos = os .listdir ("../ static/generated_videos" )
393
+ videos = os .listdir (os . path . join ( os . path . dirname ( __file__ ), " static/generated_videos") )
354
394
videos = [video for video in videos if video .endswith (".mp4" )]
395
+ instagramVideos = os .listdir (os .path .join (os .path .dirname (__file__ ), "static/generated_videos/instagram" ))
396
+ instagramVideos = [video for video in instagramVideos if video .endswith (".mp4" )]
355
397
return jsonify (
356
398
{
357
399
"status" : "success" ,
358
400
"message" : "Videos retrieved successfully!" ,
359
401
"data" : {
360
- "videos" : videos
402
+ "videos" : videos ,
403
+ "instagram" : instagramVideos
361
404
}
362
405
}
363
406
)
364
407
365
408
# Get all available subtitles
366
409
@app .route ("/api/getSubtitles" , methods = ["GET" ])
367
410
def get_subtitles ():
368
- subtitles = os .listdir ("../ static/assets/subtitles" )
411
+ subtitles = os .listdir (os . path . join ( os . path . dirname ( __file__ ), " static/assets/subtitles") )
369
412
return jsonify (
370
413
{
371
414
"status" : "success" ,
@@ -393,8 +436,9 @@ def get_models():
393
436
394
437
@app .route ("/api/assets" , methods = ["GET" ])
395
438
def get_assets ():
396
- video_assets = os .listdir ("../static/assets/temp" )
397
- videos = [video for video in videos if video .endswith (".mp4" )]
439
+ assets_path = os .path .join (os .path .dirname (__file__ ), "static/assets/temp" )
440
+ video_assets = os .listdir (assets_path )
441
+ videos = [video for video in video_assets if video .endswith (".mp4" )]
398
442
return jsonify (
399
443
{
400
444
"status" : "success" ,
0 commit comments