41 lines
1.1 KiB
Python
41 lines
1.1 KiB
Python
from datetime import datetime
|
|
from os.path import expandvars
|
|
from pathlib import Path
|
|
from re import compile as re_compile
|
|
from urllib.parse import urlparse
|
|
from xdg.BaseDirectory import xdg_config_home
|
|
|
|
|
|
def get_url_slug(url: str, suffix=None) -> str:
|
|
parsed_url = urlparse(url)
|
|
url_slug = parsed_url.hostname
|
|
|
|
url_path = parsed_url.path
|
|
if url_path is not None:
|
|
url_slug += url_path
|
|
|
|
if suffix is not None:
|
|
url_slug += suffix
|
|
|
|
return url_slug
|
|
|
|
|
|
def get_default_screenshot_directory() -> Path:
|
|
pictures_dir_re = re_compile(r'\w*XDG_PICTURES_DIR\w*\="(?P<path>.*)"\w*$')
|
|
pictures_dir = None
|
|
with open(Path(xdg_config_home) / 'user-dirs.dirs') as user_dirs:
|
|
for line in user_dirs.readlines():
|
|
match = pictures_dir_re.match(line)
|
|
if match is None:
|
|
continue
|
|
|
|
path_string = match.group('path')
|
|
path_string = expandvars(path_string)
|
|
pictures_dir = Path(path_string)
|
|
break
|
|
|
|
if pictures_dir is None:
|
|
pictures_dir = Path.home()
|
|
|
|
return pictures_dir / datetime.now().strftime('%Y-%m-%d - %H-%m-%S')
|