from flask import Flask, request, send_file
import requests
from bs4 import BeautifulSoup
import re
import os
app = Flask(__name__)
def get_tiktok_video_url(tiktok_url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
}
response = requests.get(tiktok_url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
# Find the video URL
video_tag = soup.find('video')
if video_tag:
return video_tag['src']
return None
@app.route('/download', methods=['GET'])
def download_video():
tiktok_url = request.args.get('url')
video_url = get_tiktok_video_url(tiktok_url)
if video_url:
video_response = requests.get(video_url)
video_path = 'video.mp4'
with open(video_path, 'wb') as f:
f.write(video_response.content)
return send_file(video_path, as_attachment=True)
else:
return "Could not retrieve video."
if __name__ == '__main__':
app.run(debug=True)