Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mikf
GitHub Repository: mikf/gallery-dl
Path: blob/master/gallery_dl/extractor/directlink.py
5399 views
1
# -*- coding: utf-8 -*-
2
3
# Copyright 2017-2025 Mike Fährmann
4
#
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License version 2 as
7
# published by the Free Software Foundation.
8
9
"""Direct link handling"""
10
11
from .common import Extractor, Message
12
from .. import text
13
14
15
class DirectlinkExtractor(Extractor):
16
"""Extractor for direct links to images and other media files"""
17
category = "directlink"
18
filename_fmt = "{domain}/{path}/{filename}.{extension}"
19
archive_fmt = filename_fmt
20
pattern = (r"(?i)https?://(?P<domain>[^/?#]+)/(?P<path>[^?#]+\."
21
r"(?:jpe?g|jpe|png|gif|bmp|svg|web[mp]|avif|heic|psd"
22
r"|mp4|m4v|mov|mkv|og[gmv]|wav|mp3|opus|zip|rar|7z|pdf|swf))"
23
r"(?:\?(?P<query>[^#]*))?(?:#(?P<fragment>.*))?$")
24
example = "https://en.wikipedia.org/static/images/project-logos/enwiki.png"
25
26
def __init__(self, match):
27
self.data = data = match.groupdict()
28
self.subcategory = ".".join(data["domain"].rsplit(".", 2)[-2:])
29
Extractor.__init__(self, match)
30
31
def items(self):
32
data = self.data
33
for key, value in data.items():
34
if value:
35
data[key] = text.unquote(value)
36
37
data["path"], _, name = data["path"].rpartition("/")
38
data["filename"], _, ext = name.rpartition(".")
39
data["extension"] = ext.lower()
40
data["_http_headers"] = {
41
"Referer": self.url.encode("latin-1", "ignore")}
42
43
yield Message.Directory, data
44
yield Message.Url, self.url, data
45
46