48leewsypc commited on
Commit
f106ec5
·
verified ·
1 Parent(s): 35d8e76

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -0
app.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request, send_file, jsonify
2
+ from pytube import YouTube
3
+ from yt_dlp import YoutubeDL
4
+ import instaloader
5
+ import os
6
+ import uuid
7
+ import shutil
8
+
9
+ app = Flask(__name__)
10
+
11
+ # Create downloads directory if it doesn't exist
12
+ if not os.path.exists('downloads'):
13
+ os.makedirs('downloads')
14
+
15
+ @app.route('/')
16
+ def home():
17
+ return render_template('index.html')
18
+
19
+ @app.route('/download', methods=['POST'])
20
+ def download():
21
+ url = request.form.get('url')
22
+ if not url:
23
+ return jsonify({'error': 'No URL provided'}), 400
24
+
25
+ try:
26
+ if 'youtube.com' in url or 'youtu.be' in url:
27
+ return download_youtube(url)
28
+ elif 'instagram.com' in url:
29
+ return download_instagram(url)
30
+ elif 'tiktok.com' in url:
31
+ return download_tiktok(url)
32
+ else:
33
+ return jsonify({'error': 'Unsupported platform'}), 400
34
+ except Exception as e:
35
+ return jsonify({'error': str(e)}), 500
36
+
37
+ def download_youtube(url):
38
+ try:
39
+ yt = YouTube(url)
40
+ stream = yt.streams.get_highest_resolution()
41
+ filename = f"youtube_{uuid.uuid4().hex}.mp4"
42
+ filepath = os.path.join('downloads', filename)
43
+ stream.download(output_path='downloads', filename=filename)
44
+ return send_file(filepath, as_attachment=True)
45
+ except Exception as e:
46
+ return jsonify({'error': f'YouTube download error: {str(e)}'}), 500
47
+
48
+ def download_instagram(url):
49
+ try:
50
+ L = instaloader.Instaloader()
51
+ temp_dir = f"downloads/insta_{uuid.uuid4().hex}"
52
+ os.makedirs(temp_dir, exist_ok=True)
53
+
54
+ if "/reel/" in url:
55
+ shortcode = url.split("/reel/")[1].split("/")[0]
56
+ elif "/p/" in url:
57
+ shortcode = url.split("/p/")[1].split("/")[0]
58
+ else:
59
+ raise ValueError("Invalid Instagram URL")
60
+
61
+ post = instaloader.Post.from_shortcode(L.context, shortcode)
62
+ L.download_post(post, target=temp_dir)
63
+
64
+ for file in os.listdir(temp_dir):
65
+ if file.endswith('.mp4'):
66
+ filepath = os.path.join(temp_dir, file)
67
+ return send_file(filepath, as_attachment=True)
68
+
69
+ raise Exception("No video found")
70
+ except Exception as e:
71
+ return jsonify({'error': f'Instagram download error: {str(e)}'}), 500
72
+ finally:
73
+ if os.path.exists(temp_dir):
74
+ shutil.rmtree(temp_dir)
75
+
76
+ def download_tiktok(url):
77
+ try:
78
+ filename = f"tiktok_{uuid.uuid4().hex}.mp4"
79
+ filepath = os.path.join('downloads', filename)
80
+
81
+ ydl_opts = {
82
+ 'format': 'best',
83
+ 'outtmpl': filepath,
84
+ 'quiet': True
85
+ }
86
+
87
+ with YoutubeDL(ydl_opts) as ydl:
88
+ ydl.download([url])
89
+ return send_file(filepath, as_attachment=True)
90
+ except Exception as e:
91
+ return jsonify({'error': f'TikTok download error: {str(e)}'}), 500
92
+
93
+ @app.after_request
94
+ def cleanup(response):
95
+ # Clean up downloads directory
96
+ for file in os.listdir('downloads'):
97
+ filepath = os.path.join('downloads', file)
98
+ try:
99
+ if os.path.isfile(filepath):
100
+ os.remove(filepath)
101
+ except Exception:
102
+ pass
103
+ return response
104
+
105
+ if __name__ == "__main__":
106
+ serve(app, host="0.0.0.0", port=port)