2017-05-24 12:51:18 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-10-02 19:02:05 +02:00
|
|
|
# Copyright 2017-2022 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):
|
2017-07-27 20:46:15 +02:00
|
|
|
"""Extractor for direct links to images and other media files"""
|
2017-05-24 12:51:18 +02:00
|
|
|
category = "directlink"
|
2019-11-28 23:39:35 +01:00
|
|
|
filename_fmt = "{domain}/{path}/{filename}.{extension}"
|
|
|
|
archive_fmt = filename_fmt
|
2020-10-22 23:12:59 +02:00
|
|
|
pattern = (r"(?i)https?://(?P<domain>[^/?#]+)/(?P<path>[^?#]+\."
|
2017-08-02 21:06:49 +02:00
|
|
|
r"(?:jpe?g|jpe|png|gif|web[mp]|mp4|mkv|og[gmv]|opus))"
|
2022-10-02 19:02:05 +02:00
|
|
|
r"(?:\?(?P<query>[^#]*))?(?:#(?P<fragment>.*))?$")
|
2019-02-08 13:45:40 +01:00
|
|
|
test = (
|
2018-04-29 21:27:25 +02:00
|
|
|
(("https://en.wikipedia.org/static/images/project-logos/enwiki.png"), {
|
|
|
|
"url": "18c5d00077332e98e53be9fed2ee4be66154b88d",
|
2019-11-28 23:39:35 +01:00
|
|
|
"keyword": "105770a3f4393618ab7b811b731b22663b5d3794",
|
|
|
|
}),
|
|
|
|
# empty path
|
|
|
|
(("https://example.org/file.webm"), {
|
|
|
|
"url": "2d807ed7059d1b532f1bb71dc24b510b80ff943f",
|
|
|
|
"keyword": "29dad729c40fb09349f83edafa498dba1297464a",
|
2017-08-02 21:06:49 +02:00
|
|
|
}),
|
2018-02-26 02:12:47 +01:00
|
|
|
# more complex example
|
2022-10-02 19:02:05 +02:00
|
|
|
("https://example.org/path/to/file.webm?que=1?&ry=2/#fragment", {
|
|
|
|
"url": "6fb1061390f8aada3db01cb24b51797c7ee42b31",
|
|
|
|
"keyword": "3d7abc31d45ba324e59bc599c3b4862452d5f29c",
|
2017-08-02 21:06:49 +02:00
|
|
|
}),
|
2018-02-26 02:12:47 +01:00
|
|
|
# percent-encoded characters
|
|
|
|
("https://example.org/%27%3C%23/%23%3E%27.jpg?key=%3C%26%3E", {
|
2018-02-26 03:01:23 +01:00
|
|
|
"url": "2627e8140727fdf743f86fe18f69f99a052c9718",
|
2019-11-28 23:39:35 +01:00
|
|
|
"keyword": "831790fddda081bdddd14f96985ab02dc5b5341f",
|
2018-02-26 02:12:47 +01:00
|
|
|
}),
|
2019-06-03 10:56:14 +02:00
|
|
|
# upper case file extension (#296)
|
|
|
|
("https://post-phinf.pstatic.net/MjAxOTA1MjlfMTQ4/MDAxNTU5MTI2NjcyNTkw"
|
|
|
|
".JUzkGb4V6dj9DXjLclrOoqR64uDxHFUO5KDriRdKpGwg.88mCtd4iT1NHlpVKSCaUpP"
|
|
|
|
"mZPiDgT8hmQdQ5K_gYyu0g.JPEG/2.JPG"),
|
2023-03-06 12:51:25 +01:00
|
|
|
# internationalized domain name
|
|
|
|
("https://räksmörgås.josefsson.org/raksmorgas.jpg", {
|
2023-03-09 22:25:23 +01:00
|
|
|
"url": "a65667f670b194afbd1e3ea5e7a78938d36747da",
|
|
|
|
"keyword": "fd5037fe86eebd4764e176cbaf318caec0f700be",
|
2023-03-06 12:51:25 +01:00
|
|
|
}),
|
2019-02-08 13:45:40 +01:00
|
|
|
)
|
2017-05-24 12:51:18 +02:00
|
|
|
|
|
|
|
def __init__(self, match):
|
2019-02-11 13:31:10 +01:00
|
|
|
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):
|
2019-11-28 23:39:35 +01:00
|
|
|
data = self.data
|
|
|
|
for key, value in data.items():
|
2018-02-26 02:12:47 +01:00
|
|
|
if value:
|
2019-11-28 23:39:35 +01:00
|
|
|
data[key] = text.unquote(value)
|
2019-12-25 17:17:07 +01:00
|
|
|
|
2019-11-28 23:39:35 +01:00
|
|
|
data["path"], _, name = data["path"].rpartition("/")
|
|
|
|
data["filename"], _, ext = name.rpartition(".")
|
|
|
|
data["extension"] = ext.lower()
|
2021-06-21 20:28:19 +02:00
|
|
|
data["_http_headers"] = {
|
|
|
|
"Referer": self.url.encode("latin-1", "ignore")}
|
2018-02-26 02:12:47 +01:00
|
|
|
|
2019-11-28 23:39:35 +01:00
|
|
|
yield Message.Directory, data
|
|
|
|
yield Message.Url, self.url, data
|