-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
59 lines (46 loc) · 1.88 KB
/
app.py
File metadata and controls
59 lines (46 loc) · 1.88 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
from flask import Flask, request, jsonify, send_file
import os
import uuid
from pytube import YouTube
import instaloader
app = Flask(__name__)
DOWNLOAD_FOLDER = "downloads"
os.makedirs(DOWNLOAD_FOLDER, exist_ok=True)
# YouTube Video Downloader API
@app.route('/api/youtube', methods=['POST'])
def download_youtube():
data = request.get_json()
if not data or 'link' not in data:
return jsonify({"error": "Missing 'link' parameter in request body"}), 400
url = data['link']
try:
yt = YouTube(url)
stream = yt.streams.get_highest_resolution()
filename = f"{uuid.uuid4()}.mp4"
file_path = os.path.join(DOWNLOAD_FOLDER, filename)
stream.download(output_path=DOWNLOAD_FOLDER, filename=filename)
return send_file(file_path, as_attachment=True)
except Exception as e:
return jsonify({"error": str(e)}), 500
# Instagram Video Downloader API
@app.route('/api/instareel', methods=['POST'])
def download_instagram_reel():
data = request.get_json()
if not data or 'link' not in data:
return jsonify({"error": "Missing 'link' parameter in request body"}), 400
url = data['link']
try:
loader = instaloader.Instaloader(download_videos=True)
post_shortcode = url.strip("/").split("/")[-1]
filename = f"{uuid.uuid4()}.mp4"
file_path = os.path.join(DOWNLOAD_FOLDER, filename)
loader.download_post(instaloader.Post.from_shortcode(loader.context, post_shortcode), target=DOWNLOAD_FOLDER)
for file in os.listdir(DOWNLOAD_FOLDER):
if file.endswith(".mp4"):
os.rename(os.path.join(DOWNLOAD_FOLDER, file), file_path)
break
return send_file(file_path, as_attachment=True)
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
app.run(debug=True,port=5000,host='0.0.0.0')