-
-
Save nadermx/0c98fc7f9b9407c35b5f90580647912a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fetch_with_gallery_dl(url): | |
print("running fetch_with_gallery_dl") | |
results = { | |
"items": [], | |
"page": 1, | |
} | |
"""Fetch publicly facing URL's only using gallery-dl (https://github.com/mikf/gallery-dl) and generated thumbnails.""" | |
try: | |
result = subprocess.run( | |
["gallery-dl", "-G", url], | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE, | |
text=True | |
) | |
if result.returncode != 0: | |
raise ValueError(f"gallery-dl error: {result.stderr}") | |
output = result.stdout.strip().splitlines() | |
for media_url in output: | |
thumb = quote(media_url, safe='') | |
results["items"].append({ | |
"url": media_url, | |
"thumb": thumb, | |
"mime_type": "image/jpeg", # Assume JPEG for simplicity | |
"type": "video" if media_url.endswith(('.mp4', '.mkv', '.avi')) else "image" | |
}) | |
return results | |
except Exception as e: | |
print(f"Error with gallery-dl: {e}") | |
return {"error": "We could not fetch data"} | |
"""Function to pipe the urls/video to the browser once the urls are found with gallery-dl""" | |
class ImageProxy(View): | |
@staticmethod | |
def get(request, *args, **kwargs): | |
uri = request.GET.get("source") | |
if not uri: | |
raise Http404("Missing param'uri'") | |
try: | |
image_response = requests.get(uri, stream=True) | |
image_response.raise_for_status() | |
content_type = image_response.headers.get("Content-Type", "application/octet-stream") | |
return HttpResponse(image_response.content, content_type=content_type) | |
except requests.RequestException as e: | |
print(str(e)) | |
return ImageProxy.default_image() | |
except Exception as e: | |
print(str(e)) | |
return ImageProxy.default_image() | |
@staticmethod | |
def default_image(): | |
try: | |
default_image_path = f"{BASE_DIR}/static/not-found.png" | |
with open(default_image_path, "rb") as image_file: | |
return HttpResponse(image_file.read(), content_type="image/jpeg") | |
except FileNotFoundError: | |
return HttpResponse("No image found.", status=500) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment