2017-02-20 22:02:49 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2023-02-05 16:05:13 +01:00
|
|
|
# Copyright 2017-2023 Mike Fährmann
|
2017-02-20 22:02:49 +01: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.
|
|
|
|
|
2017-03-28 13:12:44 +02:00
|
|
|
"""Utility functions and classes"""
|
2017-02-20 22:02:49 +01:00
|
|
|
|
2017-10-09 22:12:58 +02:00
|
|
|
import re
|
2017-03-28 13:12:44 +02:00
|
|
|
import os
|
2017-02-23 21:51:29 +01:00
|
|
|
import sys
|
2019-05-09 16:22:06 +02:00
|
|
|
import json
|
2022-02-22 23:02:13 +01:00
|
|
|
import time
|
2020-10-24 02:55:42 +02:00
|
|
|
import random
|
2023-12-06 21:31:31 +01:00
|
|
|
import getpass
|
2023-02-20 22:29:26 +01:00
|
|
|
import hashlib
|
2021-01-14 03:40:08 +01:00
|
|
|
import binascii
|
2017-10-03 22:38:48 +02:00
|
|
|
import datetime
|
2021-06-03 22:34:58 +02:00
|
|
|
import functools
|
2017-12-03 01:38:24 +01:00
|
|
|
import itertools
|
2022-11-04 17:35:47 +01:00
|
|
|
import subprocess
|
2017-06-16 21:01:40 +02:00
|
|
|
import urllib.parse
|
2020-01-21 21:59:36 +01:00
|
|
|
from http.cookiejar import Cookie
|
2022-02-22 23:02:13 +01:00
|
|
|
from email.utils import mktime_tz, parsedate_tz
|
2023-02-22 11:09:17 +01:00
|
|
|
from . import text, version, exception
|
2017-02-20 22:02:49 +01:00
|
|
|
|
|
|
|
|
2018-03-14 13:17:34 +01:00
|
|
|
def bencode(num, alphabet="0123456789"):
|
|
|
|
"""Encode an integer into a base-N encoded string"""
|
|
|
|
data = ""
|
|
|
|
base = len(alphabet)
|
|
|
|
while num:
|
|
|
|
num, remainder = divmod(num, base)
|
|
|
|
data = alphabet[remainder] + data
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
2017-06-01 18:14:33 +02:00
|
|
|
def bdecode(data, alphabet="0123456789"):
|
|
|
|
"""Decode a base-N encoded string ( N = len(alphabet) )"""
|
|
|
|
num = 0
|
|
|
|
base = len(alphabet)
|
|
|
|
for c in data:
|
|
|
|
num *= base
|
|
|
|
num += alphabet.index(c)
|
|
|
|
return num
|
|
|
|
|
|
|
|
|
2017-12-03 01:38:24 +01:00
|
|
|
def advance(iterable, num):
|
2020-10-29 23:26:11 +01:00
|
|
|
""""Advance 'iterable' by 'num' steps"""
|
2017-12-03 01:38:24 +01:00
|
|
|
iterator = iter(iterable)
|
|
|
|
next(itertools.islice(iterator, num, num), None)
|
|
|
|
return iterator
|
|
|
|
|
|
|
|
|
2023-12-11 23:32:28 +01:00
|
|
|
def repeat(times):
|
|
|
|
"""Return an iterator that returns None"""
|
|
|
|
if times < 0:
|
|
|
|
return itertools.repeat(None)
|
|
|
|
return itertools.repeat(None, times)
|
|
|
|
|
|
|
|
|
2020-10-29 23:26:11 +01:00
|
|
|
def unique(iterable):
|
|
|
|
"""Yield unique elements from 'iterable' while preserving order"""
|
|
|
|
seen = set()
|
|
|
|
add = seen.add
|
|
|
|
for element in iterable:
|
|
|
|
if element not in seen:
|
|
|
|
add(element)
|
|
|
|
yield element
|
|
|
|
|
|
|
|
|
2021-03-02 23:01:38 +01:00
|
|
|
def unique_sequence(iterable):
|
|
|
|
"""Yield sequentially unique elements from 'iterable'"""
|
|
|
|
last = None
|
|
|
|
for element in iterable:
|
|
|
|
if element != last:
|
|
|
|
last = element
|
|
|
|
yield element
|
|
|
|
|
|
|
|
|
2022-04-08 14:40:26 +02:00
|
|
|
def contains(values, elements, separator=" "):
|
2022-03-30 10:36:52 +02:00
|
|
|
"""Returns True if at least one of 'elements' is contained in 'values'"""
|
|
|
|
if isinstance(values, str):
|
2022-04-08 14:40:26 +02:00
|
|
|
values = values.split(separator)
|
|
|
|
|
|
|
|
if not isinstance(elements, (tuple, list)):
|
|
|
|
return elements in values
|
2022-03-30 10:36:52 +02:00
|
|
|
|
2022-04-08 14:40:26 +02:00
|
|
|
for e in elements:
|
|
|
|
if e in values:
|
|
|
|
return True
|
2022-03-30 10:36:52 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
2019-10-28 15:06:17 +01:00
|
|
|
def raises(cls):
|
|
|
|
"""Returns a function that raises 'cls' as exception"""
|
|
|
|
def wrap(*args):
|
|
|
|
raise cls(*args)
|
2018-04-12 17:07:12 +02:00
|
|
|
return wrap
|
|
|
|
|
|
|
|
|
2024-08-13 09:41:12 +02:00
|
|
|
def identity(x, _=None):
|
2021-05-04 18:00:38 +02:00
|
|
|
"""Returns its argument"""
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
2024-06-05 16:24:35 +02:00
|
|
|
def true(_, __=None):
|
2021-11-23 19:23:02 +01:00
|
|
|
"""Always returns True"""
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2024-06-05 16:24:35 +02:00
|
|
|
def false(_, __=None):
|
2021-11-23 19:23:02 +01:00
|
|
|
"""Always returns False"""
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2021-05-04 18:00:38 +02:00
|
|
|
def noop():
|
|
|
|
"""Does nothing"""
|
|
|
|
|
|
|
|
|
2023-02-20 22:29:26 +01:00
|
|
|
def md5(s):
|
|
|
|
"""Generate MD5 hexdigest of 's'"""
|
|
|
|
if not s:
|
|
|
|
s = b""
|
|
|
|
elif isinstance(s, str):
|
|
|
|
s = s.encode()
|
|
|
|
return hashlib.md5(s).hexdigest()
|
|
|
|
|
|
|
|
|
|
|
|
def sha1(s):
|
|
|
|
"""Generate SHA1 hexdigest of 's'"""
|
|
|
|
if not s:
|
|
|
|
s = b""
|
|
|
|
elif isinstance(s, str):
|
|
|
|
s = s.encode()
|
|
|
|
return hashlib.sha1(s).hexdigest()
|
|
|
|
|
|
|
|
|
2021-01-11 22:12:40 +01:00
|
|
|
def generate_token(size=16):
|
|
|
|
"""Generate a random token with hexadecimal digits"""
|
2021-01-14 03:40:08 +01:00
|
|
|
data = random.getrandbits(size * 8).to_bytes(size, "big")
|
|
|
|
return binascii.hexlify(data).decode()
|
2020-10-15 00:43:26 +02:00
|
|
|
|
|
|
|
|
2021-09-28 23:07:55 +02:00
|
|
|
def format_value(value, suffixes="kMGTPEZY"):
|
2021-07-26 01:55:02 +02:00
|
|
|
value = format(value)
|
|
|
|
value_len = len(value)
|
|
|
|
index = value_len - 4
|
|
|
|
if index >= 0:
|
|
|
|
offset = (value_len - 1) % 3 + 1
|
|
|
|
return (value[:offset] + "." + value[offset:offset+2] +
|
2021-09-28 23:07:55 +02:00
|
|
|
suffixes[index // 3])
|
|
|
|
return value
|
2021-07-26 01:55:02 +02:00
|
|
|
|
|
|
|
|
2017-08-12 20:07:27 +02:00
|
|
|
def combine_dict(a, b):
|
2018-10-08 20:28:54 +02:00
|
|
|
"""Recursively combine the contents of 'b' into 'a'"""
|
2017-08-12 20:07:27 +02:00
|
|
|
for key, value in b.items():
|
|
|
|
if key in a and isinstance(value, dict) and isinstance(a[key], dict):
|
|
|
|
combine_dict(a[key], value)
|
|
|
|
else:
|
|
|
|
a[key] = value
|
2017-08-13 14:31:22 +02:00
|
|
|
return a
|
2017-08-12 20:07:27 +02:00
|
|
|
|
|
|
|
|
2018-10-08 20:28:54 +02:00
|
|
|
def transform_dict(a, func):
|
|
|
|
"""Recursively apply 'func' to all values in 'a'"""
|
|
|
|
for key, value in a.items():
|
|
|
|
if isinstance(value, dict):
|
|
|
|
transform_dict(value, func)
|
|
|
|
else:
|
|
|
|
a[key] = func(value)
|
|
|
|
|
|
|
|
|
2019-11-21 16:57:39 +01:00
|
|
|
def filter_dict(a):
|
|
|
|
"""Return a copy of 'a' without "private" entries"""
|
|
|
|
return {k: v for k, v in a.items() if k[0] != "_"}
|
|
|
|
|
|
|
|
|
2020-06-06 23:49:49 +02:00
|
|
|
def delete_items(obj, keys):
|
|
|
|
"""Remove all 'keys' from 'obj'"""
|
|
|
|
for key in keys:
|
|
|
|
if key in obj:
|
|
|
|
del obj[key]
|
|
|
|
|
|
|
|
|
2021-09-18 02:15:42 +02:00
|
|
|
def enumerate_reversed(iterable, start=0, length=None):
|
|
|
|
"""Enumerate 'iterable' and return its elements in reverse order"""
|
|
|
|
if length is None:
|
|
|
|
length = len(iterable)
|
2022-08-01 14:08:44 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
iterable = zip(range(start-1+length, start-1, -1), reversed(iterable))
|
|
|
|
except TypeError:
|
|
|
|
iterable = list(zip(range(start, start+length), iterable))
|
|
|
|
iterable.reverse()
|
|
|
|
|
|
|
|
return iterable
|
2021-09-18 02:15:42 +02:00
|
|
|
|
|
|
|
|
2019-02-14 11:15:19 +01:00
|
|
|
def number_to_string(value, numbers=(int, float)):
|
2018-10-08 20:28:54 +02:00
|
|
|
"""Convert numbers (int, float) to string; Return everything else as is."""
|
2019-02-14 11:15:19 +01:00
|
|
|
return str(value) if value.__class__ in numbers else value
|
2018-10-08 20:28:54 +02:00
|
|
|
|
|
|
|
|
2019-03-04 21:13:34 +01:00
|
|
|
def to_string(value):
|
|
|
|
"""str() with "better" defaults"""
|
|
|
|
if not value:
|
|
|
|
return ""
|
|
|
|
if value.__class__ is list:
|
|
|
|
try:
|
|
|
|
return ", ".join(value)
|
|
|
|
except Exception:
|
|
|
|
return ", ".join(map(str, value))
|
|
|
|
return str(value)
|
|
|
|
|
|
|
|
|
2022-03-23 22:20:37 +01:00
|
|
|
def datetime_to_timestamp(dt):
|
2024-09-19 20:09:03 +02:00
|
|
|
"""Convert naive UTC datetime to Unix timestamp"""
|
2022-03-23 22:20:37 +01:00
|
|
|
return (dt - EPOCH) / SECOND
|
|
|
|
|
|
|
|
|
|
|
|
def datetime_to_timestamp_string(dt):
|
2024-09-19 20:09:03 +02:00
|
|
|
"""Convert naive UTC datetime to Unix timestamp string"""
|
2021-06-25 22:35:45 +02:00
|
|
|
try:
|
|
|
|
return str((dt - EPOCH) // SECOND)
|
|
|
|
except Exception:
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
2024-09-19 20:09:03 +02:00
|
|
|
if sys.hexversion < 0x30c0000:
|
|
|
|
# Python <= 3.11
|
|
|
|
datetime_utcfromtimestamp = datetime.datetime.utcfromtimestamp
|
|
|
|
datetime_utcnow = datetime.datetime.utcnow
|
|
|
|
datetime_from_timestamp = datetime_utcfromtimestamp
|
|
|
|
else:
|
|
|
|
# Python >= 3.12
|
|
|
|
def datetime_from_timestamp(ts=None):
|
|
|
|
"""Convert Unix timestamp to naive UTC datetime"""
|
|
|
|
Y, m, d, H, M, S, _, _, _ = time.gmtime(ts)
|
|
|
|
return datetime.datetime(Y, m, d, H, M, S)
|
|
|
|
|
|
|
|
datetime_utcfromtimestamp = datetime_from_timestamp
|
|
|
|
datetime_utcnow = datetime_from_timestamp
|
|
|
|
|
|
|
|
|
2023-11-02 15:23:28 +01:00
|
|
|
def json_default(obj):
|
|
|
|
if isinstance(obj, CustomNone):
|
|
|
|
return None
|
|
|
|
return str(obj)
|
|
|
|
|
|
|
|
|
2023-02-07 23:14:53 +01:00
|
|
|
json_loads = json._default_decoder.decode
|
2024-10-01 17:03:13 +02:00
|
|
|
json_dumps = json.JSONEncoder(
|
|
|
|
check_circular=False,
|
|
|
|
separators=(",", ":"),
|
|
|
|
default=json_default,
|
|
|
|
).encode
|
2023-02-07 23:14:53 +01:00
|
|
|
|
|
|
|
|
2019-05-09 16:22:06 +02:00
|
|
|
def dump_json(obj, fp=sys.stdout, ensure_ascii=True, indent=4):
|
|
|
|
"""Serialize 'obj' as JSON and write it to 'fp'"""
|
|
|
|
json.dump(
|
|
|
|
obj, fp,
|
|
|
|
ensure_ascii=ensure_ascii,
|
|
|
|
indent=indent,
|
2023-11-02 15:23:28 +01:00
|
|
|
default=json_default,
|
2019-05-09 16:22:06 +02:00
|
|
|
sort_keys=True,
|
|
|
|
)
|
|
|
|
fp.write("\n")
|
|
|
|
|
|
|
|
|
2023-09-13 21:38:31 +02:00
|
|
|
def dump_response(response, fp, headers=False, content=True, hide_auth=True):
|
2020-05-24 17:27:53 +02:00
|
|
|
"""Write the contents of 'response' into a file-like object"""
|
|
|
|
|
|
|
|
if headers:
|
|
|
|
request = response.request
|
|
|
|
req_headers = request.headers.copy()
|
2020-06-18 15:07:30 +02:00
|
|
|
res_headers = response.headers.copy()
|
2020-05-24 17:27:53 +02:00
|
|
|
outfmt = """\
|
|
|
|
{request.method} {request.url}
|
|
|
|
Status: {response.status_code} {response.reason}
|
|
|
|
|
|
|
|
Request Headers
|
|
|
|
---------------
|
|
|
|
{request_headers}
|
2023-01-14 22:33:59 +01:00
|
|
|
"""
|
|
|
|
if request.body:
|
|
|
|
outfmt += """
|
|
|
|
Request Body
|
|
|
|
------------
|
|
|
|
{request.body}
|
|
|
|
"""
|
|
|
|
outfmt += """
|
2020-05-24 17:27:53 +02:00
|
|
|
Response Headers
|
|
|
|
----------------
|
|
|
|
{response_headers}
|
|
|
|
"""
|
|
|
|
if hide_auth:
|
|
|
|
authorization = req_headers.get("Authorization")
|
|
|
|
if authorization:
|
2023-12-06 23:03:34 +01:00
|
|
|
atype, sep, _ = str(authorization).partition(" ")
|
2020-05-24 17:27:53 +02:00
|
|
|
req_headers["Authorization"] = atype + " ***" if sep else "***"
|
|
|
|
|
2020-06-18 15:07:30 +02:00
|
|
|
cookie = req_headers.get("Cookie")
|
|
|
|
if cookie:
|
2020-05-24 17:27:53 +02:00
|
|
|
req_headers["Cookie"] = ";".join(
|
2020-06-18 15:07:30 +02:00
|
|
|
c.partition("=")[0] + "=***"
|
|
|
|
for c in cookie.split(";")
|
|
|
|
)
|
|
|
|
|
|
|
|
set_cookie = res_headers.get("Set-Cookie")
|
|
|
|
if set_cookie:
|
|
|
|
res_headers["Set-Cookie"] = re.sub(
|
|
|
|
r"(^|, )([^ =]+)=[^,;]*", r"\1\2=***", set_cookie,
|
2020-05-24 17:27:53 +02:00
|
|
|
)
|
|
|
|
|
2023-12-06 23:03:34 +01:00
|
|
|
fmt_nv = "{}: {}".format
|
|
|
|
|
2020-05-24 17:27:53 +02:00
|
|
|
fp.write(outfmt.format(
|
|
|
|
request=request,
|
|
|
|
response=response,
|
|
|
|
request_headers="\n".join(
|
2023-12-06 23:03:34 +01:00
|
|
|
fmt_nv(name, value)
|
2020-05-24 17:27:53 +02:00
|
|
|
for name, value in req_headers.items()
|
|
|
|
),
|
|
|
|
response_headers="\n".join(
|
2023-12-06 23:03:34 +01:00
|
|
|
fmt_nv(name, value)
|
2020-06-18 15:07:30 +02:00
|
|
|
for name, value in res_headers.items()
|
2020-05-24 17:27:53 +02:00
|
|
|
),
|
|
|
|
).encode())
|
|
|
|
|
|
|
|
if content:
|
|
|
|
if headers:
|
|
|
|
fp.write(b"\nContent\n-------\n")
|
|
|
|
fp.write(response.content)
|
|
|
|
|
|
|
|
|
2022-11-07 16:33:26 +01:00
|
|
|
def extract_headers(response):
|
|
|
|
headers = response.headers
|
|
|
|
data = dict(headers)
|
|
|
|
|
|
|
|
hcd = headers.get("content-disposition")
|
|
|
|
if hcd:
|
|
|
|
name = text.extr(hcd, 'filename="', '"')
|
|
|
|
if name:
|
|
|
|
text.nameext_from_url(name, data)
|
|
|
|
|
|
|
|
hlm = headers.get("last-modified")
|
|
|
|
if hlm:
|
|
|
|
data["date"] = datetime.datetime(*parsedate_tz(hlm)[:6])
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
2022-11-04 17:35:47 +01:00
|
|
|
@functools.lru_cache(maxsize=None)
|
|
|
|
def git_head():
|
|
|
|
try:
|
2024-04-06 03:24:51 +02:00
|
|
|
out, err = Popen(
|
2022-11-04 17:35:47 +01:00
|
|
|
("git", "rev-parse", "--short", "HEAD"),
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
cwd=os.path.dirname(os.path.abspath(__file__)),
|
|
|
|
).communicate()
|
|
|
|
if out and not err:
|
|
|
|
return out.decode().rstrip()
|
|
|
|
except (OSError, subprocess.SubprocessError):
|
|
|
|
pass
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2017-10-26 00:04:28 +02:00
|
|
|
def expand_path(path):
|
|
|
|
"""Expand environment variables and tildes (~)"""
|
|
|
|
if not path:
|
|
|
|
return path
|
2017-12-21 21:56:24 +01:00
|
|
|
if not isinstance(path, str):
|
|
|
|
path = os.path.join(*path)
|
2017-10-26 00:04:28 +02:00
|
|
|
return os.path.expandvars(os.path.expanduser(path))
|
|
|
|
|
|
|
|
|
2020-01-17 23:51:07 +01:00
|
|
|
def remove_file(path):
|
|
|
|
try:
|
|
|
|
os.unlink(path)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def remove_directory(path):
|
|
|
|
try:
|
|
|
|
os.rmdir(path)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2022-02-22 23:02:13 +01:00
|
|
|
def set_mtime(path, mtime):
|
|
|
|
try:
|
|
|
|
if isinstance(mtime, str):
|
|
|
|
mtime = mktime_tz(parsedate_tz(mtime))
|
|
|
|
os.utime(path, (time.time(), mtime))
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2024-10-12 16:32:29 +02:00
|
|
|
def cookiestxt_load(fp):
|
|
|
|
"""Parse a Netscape cookies.txt file and add return its Cookies"""
|
|
|
|
cookies = []
|
2020-01-21 21:59:36 +01:00
|
|
|
|
2020-01-25 22:57:08 +01:00
|
|
|
for line in fp:
|
|
|
|
|
2022-03-19 15:14:55 +01:00
|
|
|
line = line.lstrip(" ")
|
2020-01-25 22:57:08 +01:00
|
|
|
# strip '#HttpOnly_'
|
|
|
|
if line.startswith("#HttpOnly_"):
|
|
|
|
line = line[10:]
|
|
|
|
# ignore empty lines and comments
|
2022-03-19 15:14:55 +01:00
|
|
|
if not line or line[0] in ("#", "$", "\n"):
|
2020-01-25 22:57:08 +01:00
|
|
|
continue
|
|
|
|
# strip trailing '\n'
|
|
|
|
if line[-1] == "\n":
|
|
|
|
line = line[:-1]
|
|
|
|
|
|
|
|
domain, domain_specified, path, secure, expires, name, value = \
|
|
|
|
line.split("\t")
|
2022-05-06 13:21:29 +02:00
|
|
|
|
2020-01-25 22:57:08 +01:00
|
|
|
if not name:
|
|
|
|
name = value
|
|
|
|
value = None
|
|
|
|
|
2024-10-12 16:32:29 +02:00
|
|
|
cookies.append(Cookie(
|
2020-01-25 22:57:08 +01:00
|
|
|
0, name, value,
|
|
|
|
None, False,
|
|
|
|
domain,
|
|
|
|
domain_specified == "TRUE",
|
2024-10-15 08:24:06 +02:00
|
|
|
domain[0] == "." if domain else False,
|
2020-01-25 22:57:08 +01:00
|
|
|
path, False,
|
|
|
|
secure == "TRUE",
|
|
|
|
None if expires == "0" or not expires else expires,
|
|
|
|
False, None, None, {},
|
|
|
|
))
|
2020-01-21 21:59:36 +01:00
|
|
|
|
2024-10-12 16:32:29 +02:00
|
|
|
return cookies
|
|
|
|
|
2020-01-21 21:59:36 +01:00
|
|
|
|
2022-05-06 13:21:29 +02:00
|
|
|
def cookiestxt_store(fp, cookies):
|
2020-01-25 22:57:08 +01:00
|
|
|
"""Write 'cookies' in Netscape cookies.txt format to 'fp'"""
|
2022-05-06 13:21:29 +02:00
|
|
|
write = fp.write
|
|
|
|
write("# Netscape HTTP Cookie File\n\n")
|
2020-01-25 22:57:08 +01:00
|
|
|
|
|
|
|
for cookie in cookies:
|
2022-03-19 15:14:55 +01:00
|
|
|
if not cookie.domain:
|
|
|
|
continue
|
|
|
|
|
2020-01-25 22:57:08 +01:00
|
|
|
if cookie.value is None:
|
|
|
|
name = ""
|
|
|
|
value = cookie.name
|
|
|
|
else:
|
|
|
|
name = cookie.name
|
|
|
|
value = cookie.value
|
|
|
|
|
2024-10-15 08:24:06 +02:00
|
|
|
domain = cookie.domain
|
2022-05-06 13:21:29 +02:00
|
|
|
write("\t".join((
|
2024-10-15 08:24:06 +02:00
|
|
|
domain,
|
|
|
|
"TRUE" if domain and domain[0] == "." else "FALSE",
|
2020-01-25 22:57:08 +01:00
|
|
|
cookie.path,
|
|
|
|
"TRUE" if cookie.secure else "FALSE",
|
|
|
|
"0" if cookie.expires is None else str(cookie.expires),
|
|
|
|
name,
|
2022-05-06 13:21:29 +02:00
|
|
|
value + "\n",
|
|
|
|
)))
|
2020-01-21 21:59:36 +01:00
|
|
|
|
|
|
|
|
2017-08-08 19:22:04 +02:00
|
|
|
def code_to_language(code, default=None):
|
2017-03-28 13:12:44 +02:00
|
|
|
"""Map an ISO 639-1 language code to its actual name"""
|
2017-08-08 19:22:04 +02:00
|
|
|
return CODES.get((code or "").lower(), default)
|
2017-03-28 13:12:44 +02:00
|
|
|
|
|
|
|
|
2017-08-08 19:22:04 +02:00
|
|
|
def language_to_code(lang, default=None):
|
2017-03-28 13:12:44 +02:00
|
|
|
"""Map a language name to its ISO 639-1 code"""
|
2017-08-04 15:01:10 +02:00
|
|
|
if lang is None:
|
2017-08-08 19:22:04 +02:00
|
|
|
return default
|
2017-03-28 13:12:44 +02:00
|
|
|
lang = lang.capitalize()
|
2017-06-16 21:01:40 +02:00
|
|
|
for code, language in CODES.items():
|
2017-03-28 13:12:44 +02:00
|
|
|
if language == lang:
|
|
|
|
return code
|
|
|
|
return default
|
|
|
|
|
|
|
|
|
2017-06-16 21:01:40 +02:00
|
|
|
CODES = {
|
2017-03-28 13:12:44 +02:00
|
|
|
"ar": "Arabic",
|
2018-03-05 18:37:21 +01:00
|
|
|
"bg": "Bulgarian",
|
|
|
|
"ca": "Catalan",
|
2017-03-28 13:12:44 +02:00
|
|
|
"cs": "Czech",
|
|
|
|
"da": "Danish",
|
|
|
|
"de": "German",
|
|
|
|
"el": "Greek",
|
|
|
|
"en": "English",
|
|
|
|
"es": "Spanish",
|
|
|
|
"fi": "Finnish",
|
|
|
|
"fr": "French",
|
|
|
|
"he": "Hebrew",
|
|
|
|
"hu": "Hungarian",
|
|
|
|
"id": "Indonesian",
|
|
|
|
"it": "Italian",
|
2021-05-22 16:07:04 +02:00
|
|
|
"ja": "Japanese",
|
2017-03-28 13:12:44 +02:00
|
|
|
"ko": "Korean",
|
|
|
|
"ms": "Malay",
|
|
|
|
"nl": "Dutch",
|
|
|
|
"no": "Norwegian",
|
|
|
|
"pl": "Polish",
|
|
|
|
"pt": "Portuguese",
|
|
|
|
"ro": "Romanian",
|
|
|
|
"ru": "Russian",
|
|
|
|
"sv": "Swedish",
|
|
|
|
"th": "Thai",
|
|
|
|
"tr": "Turkish",
|
|
|
|
"vi": "Vietnamese",
|
|
|
|
"zh": "Chinese",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-06 23:39:23 +01:00
|
|
|
class HTTPBasicAuth():
|
|
|
|
__slots__ = ("authorization",)
|
|
|
|
|
|
|
|
def __init__(self, username, password):
|
|
|
|
self.authorization = b"Basic " + binascii.b2a_base64(
|
|
|
|
username.encode("latin1") + b":" + str(password).encode("latin1")
|
|
|
|
)[:-1]
|
|
|
|
|
|
|
|
def __call__(self, request):
|
|
|
|
request.headers["Authorization"] = self.authorization
|
|
|
|
return request
|
|
|
|
|
|
|
|
|
2024-10-17 21:01:34 +02:00
|
|
|
class ModuleProxy():
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def __getitem__(self, key, modules=sys.modules):
|
|
|
|
try:
|
|
|
|
return modules[key]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
__import__(key)
|
|
|
|
except ImportError:
|
|
|
|
modules[key] = NONE
|
|
|
|
return NONE
|
|
|
|
return modules[key]
|
|
|
|
|
|
|
|
__getattr__ = __getitem__
|
|
|
|
|
|
|
|
|
2023-12-06 21:31:31 +01:00
|
|
|
class LazyPrompt():
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return getpass.getpass()
|
|
|
|
|
|
|
|
|
2024-09-09 20:28:18 +02:00
|
|
|
class NullContext():
|
2024-10-17 21:01:34 +02:00
|
|
|
__slots__ = ()
|
2024-09-09 20:28:18 +02:00
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
return None
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, traceback):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2022-12-06 18:44:46 +01:00
|
|
|
class CustomNone():
|
|
|
|
"""None-style type that supports more operations than regular None"""
|
2019-02-14 11:15:19 +01:00
|
|
|
__slots__ = ()
|
|
|
|
|
2024-08-13 15:57:17 +02:00
|
|
|
__getattribute__ = identity
|
|
|
|
__getitem__ = identity
|
|
|
|
__iter__ = identity
|
2022-12-06 18:44:46 +01:00
|
|
|
|
|
|
|
def __call__(self, *args, **kwargs):
|
|
|
|
return self
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def __next__():
|
|
|
|
raise StopIteration
|
|
|
|
|
2024-07-26 20:56:01 +02:00
|
|
|
def __eq__(self, other):
|
|
|
|
return self is other
|
|
|
|
|
|
|
|
def __ne__(self, other):
|
|
|
|
return self is not other
|
|
|
|
|
2024-06-05 16:24:35 +02:00
|
|
|
__lt__ = true
|
|
|
|
__le__ = true
|
|
|
|
__gt__ = false
|
|
|
|
__ge__ = false
|
2024-08-13 15:57:17 +02:00
|
|
|
__bool__ = false
|
2024-06-05 16:24:35 +02:00
|
|
|
|
2024-08-13 09:41:12 +02:00
|
|
|
__add__ = identity
|
|
|
|
__sub__ = identity
|
|
|
|
__mul__ = identity
|
|
|
|
__matmul__ = identity
|
|
|
|
__truediv__ = identity
|
|
|
|
__floordiv__ = identity
|
|
|
|
__mod__ = identity
|
|
|
|
|
|
|
|
__radd__ = identity
|
|
|
|
__rsub__ = identity
|
|
|
|
__rmul__ = identity
|
|
|
|
__rmatmul__ = identity
|
|
|
|
__rtruediv__ = identity
|
|
|
|
__rfloordiv__ = identity
|
|
|
|
__rmod__ = identity
|
|
|
|
|
|
|
|
__lshift__ = identity
|
|
|
|
__rshift__ = identity
|
|
|
|
__and__ = identity
|
|
|
|
__xor__ = identity
|
|
|
|
__or__ = identity
|
|
|
|
|
|
|
|
__rlshift__ = identity
|
|
|
|
__rrshift__ = identity
|
|
|
|
__rand__ = identity
|
|
|
|
__rxor__ = identity
|
|
|
|
__ror__ = identity
|
|
|
|
|
|
|
|
__neg__ = identity
|
|
|
|
__pos__ = identity
|
|
|
|
__abs__ = identity
|
|
|
|
__invert__ = identity
|
|
|
|
|
2022-12-06 18:44:46 +01:00
|
|
|
@staticmethod
|
|
|
|
def __len__():
|
|
|
|
return 0
|
|
|
|
|
2024-08-15 18:16:55 +02:00
|
|
|
__int__ = __len__
|
2024-08-13 15:57:17 +02:00
|
|
|
__hash__ = __len__
|
|
|
|
__index__ = __len__
|
2024-06-05 16:24:35 +02:00
|
|
|
|
2022-12-06 18:44:46 +01:00
|
|
|
@staticmethod
|
|
|
|
def __format__(_):
|
|
|
|
return "None"
|
|
|
|
|
2019-02-13 17:39:43 +01:00
|
|
|
@staticmethod
|
|
|
|
def __str__():
|
|
|
|
return "None"
|
|
|
|
|
|
|
|
__repr__ = __str__
|
|
|
|
|
|
|
|
|
2022-12-06 18:44:46 +01:00
|
|
|
NONE = CustomNone()
|
2021-06-25 22:35:45 +02:00
|
|
|
EPOCH = datetime.datetime(1970, 1, 1)
|
|
|
|
SECOND = datetime.timedelta(0, 1)
|
2020-05-19 21:42:11 +02:00
|
|
|
WINDOWS = (os.name == "nt")
|
2020-05-19 21:47:18 +02:00
|
|
|
SENTINEL = object()
|
2023-02-22 11:09:17 +01:00
|
|
|
USERAGENT = "gallery-dl/" + version.__version__
|
2023-02-28 23:10:23 +01:00
|
|
|
EXECUTABLE = getattr(sys, "frozen", False)
|
2024-02-27 01:37:57 +01:00
|
|
|
SPECIAL_EXTRACTORS = {"oauth", "recursive", "generic"}
|
2021-06-03 22:34:58 +02:00
|
|
|
GLOBALS = {
|
2022-03-30 10:36:52 +02:00
|
|
|
"contains" : contains,
|
2021-06-03 22:34:58 +02:00
|
|
|
"parse_int": text.parse_int,
|
|
|
|
"urlsplit" : urllib.parse.urlsplit,
|
|
|
|
"datetime" : datetime.datetime,
|
2022-02-23 00:02:10 +01:00
|
|
|
"timedelta": datetime.timedelta,
|
2021-06-03 22:34:58 +02:00
|
|
|
"abort" : raises(exception.StopExtraction),
|
|
|
|
"terminate": raises(exception.TerminateExtraction),
|
2023-02-11 21:06:14 +01:00
|
|
|
"restart" : raises(exception.RestartExtraction),
|
2023-02-20 22:29:26 +01:00
|
|
|
"hash_sha1": sha1,
|
|
|
|
"hash_md5" : md5,
|
2024-10-17 21:01:34 +02:00
|
|
|
"std" : ModuleProxy(),
|
2021-06-03 22:34:58 +02:00
|
|
|
"re" : re,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-04-06 03:24:51 +02:00
|
|
|
if EXECUTABLE and hasattr(sys, "_MEIPASS"):
|
|
|
|
# https://github.com/pyinstaller/pyinstaller/blob/develop/doc
|
|
|
|
# /runtime-information.rst#ld_library_path--libpath-considerations
|
|
|
|
_popen_env = os.environ.copy()
|
|
|
|
|
|
|
|
orig = _popen_env.get("LD_LIBRARY_PATH_ORIG")
|
|
|
|
if orig is None:
|
|
|
|
_popen_env.pop("LD_LIBRARY_PATH", None)
|
|
|
|
else:
|
|
|
|
_popen_env["LD_LIBRARY_PATH"] = orig
|
|
|
|
|
|
|
|
orig = _popen_env.get("DYLD_LIBRARY_PATH_ORIG")
|
|
|
|
if orig is None:
|
|
|
|
_popen_env.pop("DYLD_LIBRARY_PATH", None)
|
|
|
|
else:
|
|
|
|
_popen_env["DYLD_LIBRARY_PATH"] = orig
|
|
|
|
|
|
|
|
del orig
|
|
|
|
|
|
|
|
class Popen(subprocess.Popen):
|
|
|
|
def __init__(self, args, **kwargs):
|
|
|
|
kwargs["env"] = _popen_env
|
|
|
|
subprocess.Popen.__init__(self, args, **kwargs)
|
|
|
|
else:
|
|
|
|
Popen = subprocess.Popen
|
|
|
|
|
|
|
|
|
2024-07-11 01:16:06 +02:00
|
|
|
def compile_expression_raw(expr, name="<expr>", globals=None):
|
2021-06-03 22:34:58 +02:00
|
|
|
code_object = compile(expr, name, "eval")
|
2023-02-28 18:18:55 +01:00
|
|
|
return functools.partial(eval, code_object, globals or GLOBALS)
|
|
|
|
|
|
|
|
|
2024-07-11 01:16:06 +02:00
|
|
|
def compile_expression_tryexcept(expr, name="<expr>", globals=None):
|
|
|
|
code_object = compile(expr, name, "eval")
|
|
|
|
|
|
|
|
def _eval(locals=None, globals=(globals or GLOBALS), co=code_object):
|
|
|
|
try:
|
|
|
|
return eval(co, globals, locals)
|
|
|
|
except exception.GalleryDLException:
|
|
|
|
raise
|
|
|
|
except Exception:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return _eval
|
|
|
|
|
|
|
|
|
|
|
|
compile_expression = compile_expression_tryexcept
|
|
|
|
|
|
|
|
|
2023-02-28 18:18:55 +01:00
|
|
|
def import_file(path):
|
|
|
|
"""Import a Python module from a filesystem path"""
|
|
|
|
path, name = os.path.split(path)
|
|
|
|
|
|
|
|
name, sep, ext = name.rpartition(".")
|
|
|
|
if not sep:
|
|
|
|
name = ext
|
|
|
|
|
|
|
|
if path:
|
|
|
|
path = expand_path(path)
|
|
|
|
sys.path.insert(0, path)
|
|
|
|
try:
|
|
|
|
return __import__(name)
|
|
|
|
finally:
|
|
|
|
del sys.path[0]
|
|
|
|
else:
|
|
|
|
return __import__(name)
|
2019-02-13 17:39:43 +01:00
|
|
|
|
|
|
|
|
2021-09-14 17:40:05 +02:00
|
|
|
def build_duration_func(duration, min=0.0):
|
|
|
|
if not duration:
|
2022-04-27 20:28:14 +02:00
|
|
|
if min:
|
|
|
|
return lambda: min
|
2021-09-14 17:40:05 +02:00
|
|
|
return None
|
|
|
|
|
2021-12-18 23:04:33 +01:00
|
|
|
if isinstance(duration, str):
|
|
|
|
lower, _, upper = duration.partition("-")
|
|
|
|
lower = float(lower)
|
2021-09-14 17:40:05 +02:00
|
|
|
else:
|
2021-12-18 23:04:33 +01:00
|
|
|
try:
|
|
|
|
lower, upper = duration
|
|
|
|
except TypeError:
|
|
|
|
lower, upper = duration, None
|
|
|
|
|
|
|
|
if upper:
|
|
|
|
upper = float(upper)
|
2021-09-14 17:40:05 +02:00
|
|
|
return functools.partial(
|
|
|
|
random.uniform,
|
|
|
|
lower if lower > min else min,
|
|
|
|
upper if upper > min else min,
|
|
|
|
)
|
2021-12-18 23:04:33 +01:00
|
|
|
else:
|
|
|
|
if lower < min:
|
|
|
|
lower = min
|
|
|
|
return lambda: lower
|
2021-09-14 17:40:05 +02:00
|
|
|
|
|
|
|
|
2021-11-23 19:23:02 +01:00
|
|
|
def build_extractor_filter(categories, negate=True, special=None):
|
|
|
|
"""Build a function that takes an Extractor class as argument
|
|
|
|
and returns True if that class is allowed by 'categories'
|
|
|
|
"""
|
|
|
|
if isinstance(categories, str):
|
|
|
|
categories = categories.split(",")
|
|
|
|
|
|
|
|
catset = set() # set of categories / basecategories
|
|
|
|
subset = set() # set of subcategories
|
|
|
|
catsub = [] # list of category-subcategory pairs
|
|
|
|
|
|
|
|
for item in categories:
|
|
|
|
category, _, subcategory = item.partition(":")
|
|
|
|
if category and category != "*":
|
|
|
|
if subcategory and subcategory != "*":
|
|
|
|
catsub.append((category, subcategory))
|
|
|
|
else:
|
|
|
|
catset.add(category)
|
|
|
|
elif subcategory and subcategory != "*":
|
|
|
|
subset.add(subcategory)
|
|
|
|
|
|
|
|
if special:
|
|
|
|
catset |= special
|
|
|
|
elif not catset and not subset and not catsub:
|
|
|
|
return true if negate else false
|
|
|
|
|
|
|
|
tests = []
|
|
|
|
|
|
|
|
if negate:
|
|
|
|
if catset:
|
|
|
|
tests.append(lambda extr:
|
|
|
|
extr.category not in catset and
|
|
|
|
extr.basecategory not in catset)
|
|
|
|
if subset:
|
|
|
|
tests.append(lambda extr: extr.subcategory not in subset)
|
|
|
|
else:
|
|
|
|
if catset:
|
|
|
|
tests.append(lambda extr:
|
|
|
|
extr.category in catset or
|
|
|
|
extr.basecategory in catset)
|
|
|
|
if subset:
|
|
|
|
tests.append(lambda extr: extr.subcategory in subset)
|
|
|
|
|
|
|
|
if catsub:
|
|
|
|
def test(extr):
|
|
|
|
for category, subcategory in catsub:
|
2024-09-07 22:41:02 +02:00
|
|
|
if subcategory == extr.subcategory and (
|
|
|
|
category == extr.category or
|
|
|
|
category == extr.basecategory):
|
2021-11-23 19:23:02 +01:00
|
|
|
return not negate
|
|
|
|
return negate
|
|
|
|
tests.append(test)
|
|
|
|
|
|
|
|
if len(tests) == 1:
|
|
|
|
return tests[0]
|
|
|
|
if negate:
|
|
|
|
return lambda extr: all(t(extr) for t in tests)
|
|
|
|
else:
|
|
|
|
return lambda extr: any(t(extr) for t in tests)
|
|
|
|
|
|
|
|
|
2022-03-10 23:32:16 +01:00
|
|
|
def build_proxy_map(proxies, log=None):
|
|
|
|
"""Generate a proxy map"""
|
|
|
|
if not proxies:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if isinstance(proxies, str):
|
|
|
|
if "://" not in proxies:
|
|
|
|
proxies = "http://" + proxies.lstrip("/")
|
|
|
|
return {"http": proxies, "https": proxies}
|
|
|
|
|
|
|
|
if isinstance(proxies, dict):
|
|
|
|
for scheme, proxy in proxies.items():
|
|
|
|
if "://" not in proxy:
|
|
|
|
proxies[scheme] = "http://" + proxy.lstrip("/")
|
|
|
|
return proxies
|
|
|
|
|
|
|
|
if log:
|
|
|
|
log.warning("invalid proxy specifier: %s", proxies)
|
|
|
|
|
|
|
|
|
2017-09-06 17:08:50 +02:00
|
|
|
def build_predicate(predicates):
|
|
|
|
if not predicates:
|
2021-07-19 02:23:20 +02:00
|
|
|
return lambda url, kwdict: True
|
2017-09-06 17:08:50 +02:00
|
|
|
elif len(predicates) == 1:
|
|
|
|
return predicates[0]
|
2021-07-19 02:43:23 +02:00
|
|
|
return functools.partial(chain_predicates, predicates)
|
|
|
|
|
|
|
|
|
|
|
|
def chain_predicates(predicates, url, kwdict):
|
|
|
|
for pred in predicates:
|
|
|
|
if not pred(url, kwdict):
|
|
|
|
return False
|
|
|
|
return True
|
2017-09-06 17:08:50 +02:00
|
|
|
|
|
|
|
|
2017-02-23 21:51:29 +01:00
|
|
|
class RangePredicate():
|
2022-12-23 22:39:32 +01:00
|
|
|
"""Predicate; True if the current index is in the given range(s)"""
|
|
|
|
|
2018-10-07 21:34:25 +02:00
|
|
|
def __init__(self, rangespec):
|
2022-12-23 22:39:32 +01:00
|
|
|
self.ranges = ranges = self._parse(rangespec)
|
2017-02-23 21:51:29 +01:00
|
|
|
self.index = 0
|
2018-10-07 21:34:25 +02:00
|
|
|
|
2022-12-23 22:39:32 +01:00
|
|
|
if ranges:
|
|
|
|
# technically wrong, but good enough for now
|
2022-12-27 18:21:12 +01:00
|
|
|
# and evaluating min/max for a large range is slow
|
2022-12-23 22:39:32 +01:00
|
|
|
self.lower = min(r.start for r in ranges)
|
|
|
|
self.upper = max(r.stop for r in ranges) - 1
|
2017-03-03 17:26:50 +01:00
|
|
|
else:
|
2023-02-10 17:05:20 +01:00
|
|
|
self.lower = 0
|
|
|
|
self.upper = 0
|
2017-02-23 21:51:29 +01:00
|
|
|
|
2022-12-23 22:39:32 +01:00
|
|
|
def __call__(self, _url, _kwdict):
|
|
|
|
self.index = index = self.index + 1
|
2017-02-23 21:51:29 +01:00
|
|
|
|
2022-12-23 22:39:32 +01:00
|
|
|
if index > self.upper:
|
2017-02-23 21:51:29 +01:00
|
|
|
raise exception.StopExtraction()
|
|
|
|
|
2022-12-23 22:39:32 +01:00
|
|
|
for range in self.ranges:
|
|
|
|
if index in range:
|
2017-02-23 21:51:29 +01:00
|
|
|
return True
|
|
|
|
return False
|
2017-03-28 13:12:44 +02:00
|
|
|
|
2018-10-07 21:34:25 +02:00
|
|
|
@staticmethod
|
2022-12-23 22:39:32 +01:00
|
|
|
def _parse(rangespec):
|
2018-10-07 21:34:25 +02:00
|
|
|
"""Parse an integer range string and return the resulting ranges
|
|
|
|
|
|
|
|
Examples:
|
2022-12-27 18:21:12 +01:00
|
|
|
_parse("-2,4,6-8,10-") -> [(1,3), (4,5), (6,9), (10,INTMAX)]
|
|
|
|
_parse(" - 3 , 4- 4, 2-6") -> [(1,4), (4,5), (2,7)]
|
|
|
|
_parse("1:2,4:8:2") -> [(1,1), (4,7,2)]
|
2018-10-07 21:34:25 +02:00
|
|
|
"""
|
|
|
|
ranges = []
|
2022-12-23 22:39:32 +01:00
|
|
|
append = ranges.append
|
|
|
|
|
|
|
|
if isinstance(rangespec, str):
|
|
|
|
rangespec = rangespec.split(",")
|
2018-10-07 21:34:25 +02:00
|
|
|
|
2022-12-23 22:39:32 +01:00
|
|
|
for group in rangespec:
|
2018-10-07 21:34:25 +02:00
|
|
|
if not group:
|
|
|
|
continue
|
2022-12-23 22:39:32 +01:00
|
|
|
|
2022-12-27 18:21:12 +01:00
|
|
|
elif ":" in group:
|
|
|
|
start, _, stop = group.partition(":")
|
|
|
|
stop, _, step = stop.partition(":")
|
2022-12-23 22:39:32 +01:00
|
|
|
append(range(
|
2022-12-27 18:21:12 +01:00
|
|
|
int(start) if start.strip() else 1,
|
|
|
|
int(stop) if stop.strip() else sys.maxsize,
|
|
|
|
int(step) if step.strip() else 1,
|
2022-12-23 22:39:32 +01:00
|
|
|
))
|
2018-10-07 21:34:25 +02:00
|
|
|
|
2022-12-27 18:21:12 +01:00
|
|
|
elif "-" in group:
|
|
|
|
start, _, stop = group.partition("-")
|
|
|
|
append(range(
|
|
|
|
int(start) if start.strip() else 1,
|
|
|
|
int(stop) + 1 if stop.strip() else sys.maxsize,
|
|
|
|
))
|
2018-10-07 21:34:25 +02:00
|
|
|
|
2022-12-27 18:21:12 +01:00
|
|
|
else:
|
|
|
|
start = int(group)
|
|
|
|
append(range(start, start+1))
|
2018-10-07 21:34:25 +02:00
|
|
|
|
2022-12-27 18:21:12 +01:00
|
|
|
return ranges
|
2018-10-07 21:34:25 +02:00
|
|
|
|
2017-03-28 13:12:44 +02:00
|
|
|
|
2017-09-06 17:08:50 +02:00
|
|
|
class UniquePredicate():
|
|
|
|
"""Predicate; True if given URL has not been encountered before"""
|
|
|
|
def __init__(self):
|
|
|
|
self.urls = set()
|
|
|
|
|
2021-07-19 02:23:20 +02:00
|
|
|
def __call__(self, url, _):
|
2018-02-20 18:14:27 +01:00
|
|
|
if url.startswith("text:"):
|
|
|
|
return True
|
2017-09-06 17:08:50 +02:00
|
|
|
if url not in self.urls:
|
|
|
|
self.urls.add(url)
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2017-09-08 17:52:00 +02:00
|
|
|
class FilterPredicate():
|
|
|
|
"""Predicate; True if evaluating the given expression returns True"""
|
|
|
|
|
2021-06-03 22:34:58 +02:00
|
|
|
def __init__(self, expr, target="image"):
|
2022-12-21 20:36:46 +01:00
|
|
|
if not isinstance(expr, str):
|
|
|
|
expr = "(" + ") and (".join(expr) + ")"
|
2018-10-07 21:34:25 +02:00
|
|
|
name = "<{} filter>".format(target)
|
2021-06-03 22:34:58 +02:00
|
|
|
self.expr = compile_expression(expr, name)
|
2017-09-08 17:52:00 +02:00
|
|
|
|
2021-06-03 22:34:58 +02:00
|
|
|
def __call__(self, _, kwdict):
|
2017-09-08 17:52:00 +02:00
|
|
|
try:
|
2021-06-03 22:34:58 +02:00
|
|
|
return self.expr(kwdict)
|
2018-04-12 17:07:12 +02:00
|
|
|
except exception.GalleryDLException:
|
|
|
|
raise
|
2017-09-08 17:52:00 +02:00
|
|
|
except Exception as exc:
|
|
|
|
raise exception.FilterError(exc)
|