diff --git a/gallery_dl/text.py b/gallery_dl/text.py index 1f8424c5..c276dabe 100644 --- a/gallery_dl/text.py +++ b/gallery_dl/text.py @@ -8,7 +8,9 @@ """Collection of functions that work in strings/text""" +import sys import re +import os.path import html.parser import urllib.parse import platform @@ -40,6 +42,17 @@ def clean_path_posix(path): except AttributeError: return path +def shorten_path(path, limit=255, encoding=sys.getfilesystemencoding()): + """Shorten a path segment to at most 'limit' bytes""" + return (path.encode(encoding)[:limit]).decode(encoding, "ignore") + +def shorten_filename(filename, limit=255, encoding=sys.getfilesystemencoding()): + """Shorten a filename to at most 'limit' bytes while preserving extension""" + name, extension = os.path.splitext(filename) + bext = extension.encode(encoding) + bname = name.encode(encoding)[:limit-len(bext)] + return bname.decode(encoding, "ignore") + extension + def extract(txt, begin, end, pos=0): try: first = txt.index(begin, pos) + len(begin)