| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright 2022-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 |
"""Extractors for https://uploadir.com/""" |
| 10 |
|
| 11 |
from .common import Extractor, Message |
| 12 |
from .. import text |
| 13 |
|
| 14 |
|
| 15 |
class UploadirFileExtractor(Extractor): |
| 16 |
"""Extractor for uploadir files""" |
| 17 |
category = "uploadir" |
| 18 |
subcategory = "file" |
| 19 |
root = "https://uploadir.com" |
| 20 |
filename_fmt = "{filename} ({id}).{extension}" |
| 21 |
archive_fmt = "{id}" |
| 22 |
pattern = r"(?:https?://)?uploadir\.com/(?:user/)?u(?:ploads)?/([^/?#]+)" |
| 23 |
example = "https://uploadir.com/u/ID" |
| 24 |
|
| 25 |
def __init__(self, match): |
| 26 |
Extractor.__init__(self, match) |
| 27 |
self.file_id = match[1] |
| 28 |
|
| 29 |
def items(self): |
| 30 |
url = f"{self.root}/u/{self.file_id}" |
| 31 |
response = self.request(url, method="HEAD", allow_redirects=False) |
| 32 |
|
| 33 |
if 300 <= response.status_code < 400: |
| 34 |
url = response.headers["Location"] |
| 35 |
extr = text.extract_from(self.request(url).text) |
| 36 |
|
| 37 |
name = text.unescape(extr("<h2>", "</h2>").strip()) |
| 38 |
url = self.root + extr('class="form" action="', '"') |
| 39 |
token = extr('name="authenticity_token" value="', '"') |
| 40 |
|
| 41 |
data = text.nameext_from_url(name, { |
| 42 |
"_http_method": "POST", |
| 43 |
"_http_data" : { |
| 44 |
"authenticity_token": token, |
| 45 |
"upload_id": self.file_id, |
| 46 |
}, |
| 47 |
}) |
| 48 |
|
| 49 |
else: |
| 50 |
hcd = response.headers.get("Content-Disposition") |
| 51 |
name = (hcd.partition("filename*=UTF-8''")[2] or |
| 52 |
text.extr(hcd, 'filename="', '"')) |
| 53 |
data = text.nameext_from_url(name) |
| 54 |
|
| 55 |
data["id"] = self.file_id |
| 56 |
yield Message.Directory, "", data |
| 57 |
yield Message.Url, url, data |