File size: 5,056 Bytes
16e4ad1
 
 
 
 
 
 
f05937f
 
 
 
 
16e4ad1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f05937f
 
 
078910c
f05937f
 
078910c
f05937f
078910c
f05937f
 
 
 
 
 
 
078910c
 
 
 
f05937f
 
 
 
 
 
 
 
 
16e4ad1
 
f05937f
 
 
 
 
 
16e4ad1
 
 
 
 
 
 
f05937f
 
 
 
 
 
 
16e4ad1
f05937f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16e4ad1
 
 
 
 
 
 
 
 
 
 
 
f05937f
 
 
16e4ad1
 
 
 
f05937f
 
 
 
 
 
 
 
 
16e4ad1
 
f05937f
 
 
 
 
 
16e4ad1
 
 
f05937f
 
 
16e4ad1
 
f05937f
 
16e4ad1
 
 
 
 
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
from flask import Flask, render_template, request, send_file, jsonify
from yt_dlp import YoutubeDL
import instaloader
import os
import uuid
import shutil
from waitress import serve
import ssl
import certifi

# Disable SSL verification warnings
ssl._create_default_https_context = ssl._create_unverified_context

app = Flask(__name__)

# Create downloads directory
os.makedirs('downloads', exist_ok=True)

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/download', methods=['POST'])
def download():
    url = request.form.get('url')
    if not url:
        return jsonify({'error': 'No URL provided'}), 400

    try:
        if 'youtube.com' in url or 'youtu.be' in url:
            return download_youtube(url)
        elif 'instagram.com' in url:
            return download_instagram(url)
        elif 'tiktok.com' in url:
            return download_tiktok(url)
        else:
            return jsonify({'error': 'Please enter a valid YouTube, Instagram, or TikTok URL'}), 400
    except Exception as e:
        return jsonify({'error': str(e)}), 500

def download_youtube(url):
    try:
        filename = f"youtube_{uuid.uuid4().hex}.mp4"
        filepath = os.path.join('downloads', filename)
        
        ydl_opts = {
            'format': 'best',
            'outtmpl': filepath,
            'nocheckcertificate': True,
            'no_check_certificates': True,
            'quiet': True,
            'no_warnings': True,
            'ignoreerrors': True,
            'extract_flat': True,
            'force_generic_extractor': True,
            'http_headers': {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
            }
        }
        
        with YoutubeDL(ydl_opts) as ydl:
            info = ydl.extract_info(url, download=True)
            if info:
                return send_file(
                    filepath,
                    as_attachment=True,
                    download_name=f"{info.get('title', 'video')}.mp4"
                )
            else:
                raise Exception("Failed to extract video information")
                
    except Exception as e:
        return jsonify({'error': f'YouTube download failed: {str(e)}'}), 500
    finally:
        if os.path.exists(filepath):
            try:
                os.remove(filepath)
            except:
                pass

def download_instagram(url):
    try:
        L = instaloader.Instaloader()
        temp_dir = f"downloads/insta_{uuid.uuid4().hex}"
        os.makedirs(temp_dir, exist_ok=True)

        try:
            if "/reel/" in url:
                shortcode = url.split("/reel/")[1].split("/")[0]
            elif "/p/" in url:
                shortcode = url.split("/p/")[1].split("/")[0]
            else:
                raise ValueError("Invalid Instagram URL")

            post = instaloader.Post.from_shortcode(L.context, shortcode)
            L.download_post(post, target=temp_dir)

            for file in os.listdir(temp_dir):
                if file.endswith('.mp4'):
                    filepath = os.path.join(temp_dir, file)
                    return send_file(
                        filepath,
                        as_attachment=True,
                        download_name=f"instagram_{shortcode}.mp4"
                    )

            raise Exception("No video found in this post")
        finally:
            if os.path.exists(temp_dir):
                shutil.rmtree(temp_dir, ignore_errors=True)

    except Exception as e:
        return jsonify({'error': f'Instagram download failed: {str(e)}'}), 500

def download_tiktok(url):
    try:
        filename = f"tiktok_{uuid.uuid4().hex}.mp4"
        filepath = os.path.join('downloads', filename)
        
        ydl_opts = {
            'format': 'best',
            'outtmpl': filepath,
            'quiet': True,
            'no_warnings': True,
            'nocheckcertificate': True
        }
        
        with YoutubeDL(ydl_opts) as ydl:
            ydl.download([url])
            if os.path.exists(filepath):
                return send_file(
                    filepath,
                    as_attachment=True,
                    download_name=f"tiktok_video.mp4"
                )
            else:
                raise Exception("Failed to download video")
                
    except Exception as e:
        return jsonify({'error': f'TikTok download failed: {str(e)}'}), 500
    finally:
        if os.path.exists(filepath):
            try:
                os.remove(filepath)
            except:
                pass

@app.after_request
def cleanup(response):
    try:
        for file in os.listdir('downloads'):
            filepath = os.path.join('downloads', file)
            if os.path.isfile(filepath):
                os.remove(filepath)
    except:
        pass
    return response

if __name__ == "__main__":
    port = int(os.environ.get("PORT", 7860))
    serve(app, host="0.0.0.0", port=port)