| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"""Extractors for https://koofr.net/""" |
| 10 |
|
| 11 |
from .common import Extractor, Message |
| 12 |
from .. import text |
| 13 |
|
| 14 |
|
| 15 |
class KoofrSharedExtractor(Extractor): |
| 16 |
"""Base class for koofr extractors""" |
| 17 |
category = "koofr" |
| 18 |
subcategory = "shared" |
| 19 |
root = "https://app.koofr.net" |
| 20 |
directory_fmt = ("{category}", "{post[title]} ({post[id]})", "{path:I}") |
| 21 |
filename_fmt = "{num:>03} {filename} ({hash|id:[:8]}).{extension}" |
| 22 |
archive_fmt = "{post[id]}_{path:J/}_{hash|id}" |
| 23 |
pattern = (r"(?:https?://)?(?:" |
| 24 |
r"(?:app\.)?koofr\.(?:net|eu)/links/([\w-]+)|" |
| 25 |
r"k00\.fr/(\w+))") |
| 26 |
example = "https://app.koofr.net/links/UUID" |
| 27 |
|
| 28 |
def items(self): |
| 29 |
uuid, code = self.groups |
| 30 |
if code is not None: |
| 31 |
uuid = self.request_location( |
| 32 |
"https://k00.fr/" + code, method="GET").rpartition("/")[2] |
| 33 |
|
| 34 |
url = f"{self.root}/api/v2/public/links/{uuid}" |
| 35 |
referer = f"{self.root}/links/{uuid}" |
| 36 |
password = self.config("password") |
| 37 |
params = {"password": password or ""} |
| 38 |
headers = { |
| 39 |
"Referer" : referer, |
| 40 |
"X-Client" : "newfrontend", |
| 41 |
"X-Koofr-Version": "2.1", |
| 42 |
} |
| 43 |
data = self.request_json(url, params=params, headers=headers) |
| 44 |
|
| 45 |
file = data["file"] |
| 46 |
file["path"] = [] |
| 47 |
if file["type"] == "dir" and self.config("recursive", True): |
| 48 |
items = self._extract_files(file, url + "/bundle", params, headers) |
| 49 |
recursive = True |
| 50 |
else: |
| 51 |
items = ((file, (file,)),) |
| 52 |
recursive = False |
| 53 |
|
| 54 |
post = { |
| 55 |
"id" : data["id"], |
| 56 |
"title": data["name"], |
| 57 |
"date" : self.parse_timestamp(file["modified"] / 1000), |
| 58 |
} |
| 59 |
|
| 60 |
base = (f"{data.get('publicUrlBase') or self.root}" |
| 61 |
f"/content/links/{uuid}/files/get/") |
| 62 |
headers = {"Referer": referer} |
| 63 |
password = "&password=" + text.quote(password) if password else "" |
| 64 |
|
| 65 |
for dir, files in items: |
| 66 |
dir["post"] = post |
| 67 |
dir["count"] = count = len(files) |
| 68 |
yield Message.Directory, "", dir |
| 69 |
|
| 70 |
num = 0 |
| 71 |
for file in files: |
| 72 |
num += 1 |
| 73 |
file["num"] = num |
| 74 |
file["count"] = count |
| 75 |
file["post"] = post |
| 76 |
file["date"] = self.parse_timestamp(file["modified"] / 1000) |
| 77 |
file["_http_headers"] = headers |
| 78 |
|
| 79 |
name = file["name"] |
| 80 |
text.nameext_from_name(name, file) |
| 81 |
|
| 82 |
if recursive: |
| 83 |
if path := file["path"]: |
| 84 |
path = f"{'/'.join(path)}/{name}" |
| 85 |
else: |
| 86 |
path = name |
| 87 |
else: |
| 88 |
path = "" |
| 89 |
password += "&force" |
| 90 |
|
| 91 |
url = (f"{base}{text.quote(name)}" |
| 92 |
f"?path=/{text.quote(path)}{password}") |
| 93 |
yield Message.Url, url, file |
| 94 |
|
| 95 |
def _extract_files(self, dir, url, params, headers): |
| 96 |
path = dir["path"] |
| 97 |
params["path"] = "/" + "/".join(path) |
| 98 |
|
| 99 |
items = self.request_json( |
| 100 |
url, params=params, headers=headers)["files"] |
| 101 |
|
| 102 |
dirs = [] |
| 103 |
files = [] |
| 104 |
for item in items: |
| 105 |
if item["type"] == "dir": |
| 106 |
item["path"] = path.copy() |
| 107 |
item["path"].append(item["name"]) |
| 108 |
dirs.append(item) |
| 109 |
else: |
| 110 |
item["path"] = path |
| 111 |
files.append(item) |
| 112 |
|
| 113 |
yield dir, files |
| 114 |
for sub in dirs: |
| 115 |
yield from self._extract_files(sub, url, params.copy(), headers) |