mirror of
https://github.com/gabrielkheisa/instagram-downloader.git
synced 2024-12-22 18:13:20 +07:00
Compare commits
No commits in common. "6fa7019fb7a09d804237f66ba89911dd9b8c3da5" and "b37c957637ffbfe4310628f9bcc1a6d773e1b4df" have entirely different histories.
6fa7019fb7
...
b37c957637
@ -17,9 +17,8 @@ https://ig.gabrielkheisa.xyz/reel/Cz3dNmDMVC9/?igshid=MzRlODBiNWFlZA==
|
||||
```
|
||||
### Returns redirect:
|
||||
```
|
||||
https://scontent.cdninstagram.com/v/t66.30100-16/316926421_1723935788092224_3596729375098306652_n.mp4?_nc_ht=scontent.cdninstagram.com&_nc_cat=100&_nc_ohc=6lyBPVcj...............
|
||||
https://scontent.cdninstagram.com/v/t66.30100-16/316926421_1723935788092224_3596729375098306652_n.mp4?_nc_ht=scontent.cdninstagram.com&_nc_cat=100&_nc_ohc=6lyBPVcjJkYAX8kLe3I&edm=APs17CUBAAAA&ccb=7-5&oh=00_AfBNGf7HzFPnd-mhfvhZZZRk_-PlN3qx3hqbsINaUGA4aA&oe=6576D61D&_nc_sid=10d13b
|
||||
```
|
||||
Notes: This redirect URL (https://scontent.cdninstagram.com/...) has expiration, in which you need to re-run the query to get a new URL signature
|
||||
|
||||
## DISCLAIMER:
|
||||
|
||||
|
53
run.py
53
run.py
@ -1,9 +1,7 @@
|
||||
from selenium import webdriver
|
||||
from flask import Flask, request, redirect
|
||||
import concurrent.futures
|
||||
import re
|
||||
from collections import OrderedDict
|
||||
import time
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
@ -20,13 +18,7 @@ options.add_argument('--no-sandbox')
|
||||
options.add_argument(f'user-agent={user_agent}')
|
||||
browser = webdriver.Chrome(executable_path="/usr/bin/chromedriver", options=options)
|
||||
|
||||
# Define the maximum cache size and duration in seconds (4 hours)
|
||||
MAX_CACHE_SIZE = 50
|
||||
CACHE_DURATION = 4 * 60 * 60 # 4 hours in seconds
|
||||
cache = OrderedDict(maxlen=MAX_CACHE_SIZE)
|
||||
|
||||
# Validate query, modify this regex as needed
|
||||
VALID_QUERY_REGEX = re.compile(r'^[\w\-\.\/]+$')
|
||||
cache = OrderedDict(maxlen=50)
|
||||
|
||||
# Function to handle web scraping using Selenium
|
||||
def get_video_source(query_string):
|
||||
@ -52,52 +44,27 @@ def get_video_source(query_string):
|
||||
except Exception as e:
|
||||
# Handle exceptions and return a default URL or re-raise the exception
|
||||
return base_url
|
||||
|
||||
@app.route("/", methods=["GET"]) # Route for empty query string
|
||||
def handle_empty_query():
|
||||
return redirect("https://github.com/gabrielkheisa/instagram-downloader")
|
||||
|
||||
@app.route("/<path:query_string>", methods=["GET"])
|
||||
def get_video_source_server(query_string):
|
||||
global cache # Ensure we reference the global cache variable
|
||||
print(query_string)
|
||||
if len(query_string) > 30:
|
||||
return '', 204
|
||||
|
||||
if not VALID_QUERY_REGEX.match(query_string):
|
||||
return "Invalid link", 400
|
||||
|
||||
|
||||
# Clean up entries older than 4 hours
|
||||
current_time = time.time()
|
||||
keys_to_remove = []
|
||||
for key in list(cache.keys()):
|
||||
value = cache[key]
|
||||
if isinstance(value, dict) and "timestamp" in value:
|
||||
timestamp = value["timestamp"]
|
||||
if current_time - timestamp >= CACHE_DURATION:
|
||||
keys_to_remove.append(key)
|
||||
|
||||
for key in keys_to_remove:
|
||||
cache.pop(key, None)
|
||||
|
||||
# Reject the request by returning a 414 error code
|
||||
return abort(414, description="Query string too long")
|
||||
if query_string in cache:
|
||||
# Move the existing entry to the front of the cache and update its timestamp
|
||||
# If cached, move to the front of the OrderedDict to update its age
|
||||
video_source = cache.pop(query_string)
|
||||
video_source["timestamp"] = time.time()
|
||||
cache[query_string] = video_source
|
||||
return redirect(video_source["url"])
|
||||
|
||||
# Create a ThreadPoolExecutor for parallel execution with a timeout of 8 seconds
|
||||
return redirect(video_source)
|
||||
# Create a ThreadPoolExecutor for parallel execution with a timeout of 3 seconds
|
||||
with concurrent.futures.ThreadPoolExecutor() as executor:
|
||||
future = executor.submit(get_video_source, query_string)
|
||||
try:
|
||||
video_source = future.result(timeout=8) # Timeout set to 8 seconds
|
||||
# Add the new entry to the cache with a timestamp
|
||||
cache[query_string] = {"url": video_source, "timestamp": time.time()}
|
||||
video_source = future.result(timeout=10) # Timeout set to 3 seconds
|
||||
cache[query_string] = video_source
|
||||
return redirect(video_source)
|
||||
except concurrent.futures.TimeoutError:
|
||||
return redirect(base_url) # Handle timeout - return a default URL or handle as needed
|
||||
# Handle timeout - return a default URL or handle as needed
|
||||
return redirect(base_url)
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(debug=False, port=8080, host="0.0.0.0")
|
Loading…
Reference in New Issue
Block a user