1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-25 12:12:34 +01:00
gallery-dl/gallery_dl/extractor/directlink.py

44 lines
1.5 KiB
Python
Raw Normal View History

2017-05-24 12:51:18 +02:00
# -*- coding: utf-8 -*-
# Copyright 2017-2023 Mike Fährmann
2017-05-24 12:51:18 +02:00
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
"""Direct link handling"""
from .common import Extractor, Message
from .. import text
class DirectlinkExtractor(Extractor):
"""Extractor for direct links to images and other media files"""
2017-05-24 12:51:18 +02:00
category = "directlink"
filename_fmt = "{domain}/{path}/{filename}.{extension}"
archive_fmt = filename_fmt
pattern = (r"(?i)https?://(?P<domain>[^/?#]+)/(?P<path>[^?#]+\."
r"(?:jpe?g|jpe|png|gif|bmp|svg|web[mp]|avif|heic|psd"
r"|mp4|m4v|mov|mkv|og[gmv]|wav|mp3|opus|zip|rar|7z|pdf|swf))"
2022-10-02 19:02:05 +02:00
r"(?:\?(?P<query>[^#]*))?(?:#(?P<fragment>.*))?$")
example = "https://en.wikipedia.org/static/images/project-logos/enwiki.png"
2017-05-24 12:51:18 +02:00
def __init__(self, match):
Extractor.__init__(self, match)
2017-08-02 21:06:49 +02:00
self.data = match.groupdict()
2017-05-24 12:51:18 +02:00
def items(self):
data = self.data
for key, value in data.items():
2018-02-26 02:12:47 +01:00
if value:
data[key] = text.unquote(value)
data["path"], _, name = data["path"].rpartition("/")
data["filename"], _, ext = name.rpartition(".")
data["extension"] = ext.lower()
data["_http_headers"] = {
"Referer": self.url.encode("latin-1", "ignore")}
2018-02-26 02:12:47 +01:00
yield Message.Directory, data
yield Message.Url, self.url, data