2017-05-24 12:51:18 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-02-26 03:01:23 +01:00
|
|
|
# Copyright 2017-2018 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"
|
2017-05-30 17:33:09 +02:00
|
|
|
filename_fmt = "{domain}/{path}"
|
2018-02-12 23:09:34 +01:00
|
|
|
archive_fmt = "{domain}/{path}"
|
2017-08-02 21:06:49 +02:00
|
|
|
pattern = [r"https?://(?P<domain>[^/]+)/(?P<path>[^?&#]+\."
|
|
|
|
r"(?:jpe?g|jpe|png|gif|web[mp]|mp4|mkv|og[gmv]|opus))"
|
|
|
|
r"(?:\?(?P<query>[^/?#]*))?(?:#(?P<fragment>.*))?$"]
|
|
|
|
test = [
|
2018-04-29 21:27:25 +02:00
|
|
|
(("https://en.wikipedia.org/static/images/project-logos/enwiki.png"), {
|
|
|
|
"url": "18c5d00077332e98e53be9fed2ee4be66154b88d",
|
|
|
|
"keyword": "66bce3a0a6872d8497e1984eb49d54a3ed0d3d5e",
|
2017-08-02 21:06:49 +02:00
|
|
|
}),
|
2018-02-26 02:12:47 +01:00
|
|
|
# more complex example
|
2017-08-02 21:06:49 +02:00
|
|
|
("https://example.org/path/file.webm?que=1&ry=2#fragment", {
|
|
|
|
"url": "fd4aec8a32842343394e6078a06c3e6b647bf671",
|
|
|
|
"keyword": "ed008f35fc18dddb2f448a18d160c949bb3b054c",
|
|
|
|
}),
|
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",
|
|
|
|
"keyword": "c658b8b6213e46be15a25e492df385ece5771bdf",
|
2018-02-26 02:12:47 +01:00
|
|
|
}),
|
2017-08-02 21:06:49 +02:00
|
|
|
]
|
2017-05-24 12:51:18 +02:00
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
Extractor.__init__(self)
|
2017-08-02 21:06:49 +02:00
|
|
|
self.data = match.groupdict()
|
2017-05-24 12:51:18 +02:00
|
|
|
self.url = match.string
|
|
|
|
|
|
|
|
def items(self):
|
2017-08-02 21:06:49 +02:00
|
|
|
text.nameext_from_url(self.url, self.data)
|
2018-02-26 02:12:47 +01:00
|
|
|
for key, value in self.data.items():
|
|
|
|
if value:
|
|
|
|
self.data[key] = text.unquote(value)
|
|
|
|
|
2017-05-24 12:51:18 +02:00
|
|
|
yield Message.Version, 1
|
2017-08-02 21:06:49 +02:00
|
|
|
yield Message.Directory, self.data
|
|
|
|
yield Message.Url, self.url, self.data
|