2019-01-02 10:49:49 +01:00
|
|
|
import os
|
2017-04-24 16:25:34 +02:00
|
|
|
import re
|
2017-12-29 14:26:40 +01:00
|
|
|
import socket
|
2019-08-22 17:10:37 +02:00
|
|
|
import subprocess
|
|
|
|
import tempfile
|
2018-01-21 16:39:40 +01:00
|
|
|
import unicodedata
|
2019-02-13 13:38:37 +01:00
|
|
|
import warnings
|
2017-04-24 16:25:34 +02:00
|
|
|
|
|
|
|
from bs4 import BeautifulSoup
|
2023-12-05 08:15:27 +01:00
|
|
|
from typing import Any, Dict
|
2017-04-24 16:25:34 +02:00
|
|
|
|
2023-12-04 18:45:40 +01:00
|
|
|
import click
|
|
|
|
|
2017-12-30 16:30:35 +01:00
|
|
|
from toot.exceptions import ConsoleError
|
2023-09-23 03:32:19 +02:00
|
|
|
from urllib.parse import urlparse, urlencode, quote, unquote
|
2017-12-30 16:30:35 +01:00
|
|
|
|
2017-04-24 16:25:34 +02:00
|
|
|
|
2019-01-24 11:18:28 +01:00
|
|
|
def str_bool(b):
|
|
|
|
"""Convert boolean to string, in the way expected by the API."""
|
|
|
|
return "true" if b else "false"
|
|
|
|
|
|
|
|
|
2023-02-21 18:10:14 +01:00
|
|
|
def str_bool_nullable(b):
|
|
|
|
"""Similar to str_bool, but leave None as None"""
|
|
|
|
return None if b is None else str_bool(b)
|
|
|
|
|
|
|
|
|
2023-11-04 07:40:56 +01:00
|
|
|
def parse_html(html: str) -> BeautifulSoup:
|
2019-02-13 13:38:37 +01:00
|
|
|
# Ignore warnings made by BeautifulSoup, if passed something that looks like
|
|
|
|
# a file (e.g. a dot which matches current dict), it will warn that the file
|
|
|
|
# should be opened instead of passing a filename.
|
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter("ignore")
|
2023-11-04 07:40:56 +01:00
|
|
|
return BeautifulSoup(html.replace("'", "'"), "html.parser")
|
2018-01-21 16:39:40 +01:00
|
|
|
|
2023-11-04 07:40:56 +01:00
|
|
|
|
|
|
|
def get_text(html):
|
|
|
|
"""Converts html to text, strips all tags."""
|
|
|
|
text = parse_html(html).get_text()
|
|
|
|
return unicodedata.normalize("NFKC", text)
|
2017-04-24 16:25:34 +02:00
|
|
|
|
|
|
|
|
2023-11-04 07:38:47 +01:00
|
|
|
def html_to_paragraphs(html):
|
2017-04-24 16:25:34 +02:00
|
|
|
"""Attempt to convert html to plain text while keeping line breaks.
|
|
|
|
Returns a list of paragraphs, each being a list of lines.
|
|
|
|
"""
|
|
|
|
paragraphs = re.split("</?p[^>]*>", html)
|
|
|
|
|
|
|
|
# Convert <br>s to line breaks and remove empty paragraphs
|
|
|
|
paragraphs = [re.split("<br */?>", p) for p in paragraphs if p]
|
|
|
|
|
|
|
|
# Convert each line in each paragraph to plain text:
|
2022-12-27 10:41:06 +01:00
|
|
|
return [[get_text(line) for line in p] for p in paragraphs]
|
2017-04-24 16:25:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
def format_content(content):
|
|
|
|
"""Given a Status contents in HTML, converts it into lines of plain text.
|
|
|
|
|
|
|
|
Returns a generator yielding lines of content.
|
|
|
|
"""
|
|
|
|
|
2023-11-04 07:38:47 +01:00
|
|
|
paragraphs = html_to_paragraphs(content)
|
2017-04-24 16:25:34 +02:00
|
|
|
|
|
|
|
first = True
|
|
|
|
|
|
|
|
for paragraph in paragraphs:
|
|
|
|
if not first:
|
|
|
|
yield ""
|
|
|
|
|
|
|
|
for line in paragraph:
|
|
|
|
yield line
|
|
|
|
|
|
|
|
first = False
|
2017-12-29 14:26:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
def domain_exists(name):
|
|
|
|
try:
|
|
|
|
socket.gethostbyname(name)
|
|
|
|
return True
|
|
|
|
except OSError:
|
|
|
|
return False
|
2017-12-30 16:30:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
def assert_domain_exists(domain):
|
|
|
|
if not domain_exists(domain):
|
|
|
|
raise ConsoleError("Domain {} not found".format(domain))
|
2018-01-04 12:36:14 +01:00
|
|
|
|
|
|
|
|
2019-01-02 10:49:49 +01:00
|
|
|
EOF_KEY = "Ctrl-Z" if os.name == 'nt' else "Ctrl-D"
|
|
|
|
|
|
|
|
|
|
|
|
def multiline_input():
|
|
|
|
"""Lets user input multiple lines of text, terminated by EOF."""
|
|
|
|
lines = []
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
lines.append(input())
|
|
|
|
except EOFError:
|
|
|
|
break
|
|
|
|
|
|
|
|
return "\n".join(lines).strip()
|
2019-08-22 17:10:37 +02:00
|
|
|
|
|
|
|
|
2022-11-12 07:29:08 +01:00
|
|
|
EDITOR_DIVIDER = "------------------------ >8 ------------------------"
|
|
|
|
|
|
|
|
EDITOR_INPUT_INSTRUCTIONS = f"""
|
|
|
|
{EDITOR_DIVIDER}
|
|
|
|
Do not modify or remove the line above.
|
|
|
|
Enter your toot above it.
|
|
|
|
Everything below it will be ignored.
|
2019-08-22 17:10:37 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
2023-12-05 08:15:27 +01:00
|
|
|
def editor_input(editor: str, initial_text: str) -> str:
|
2019-08-22 17:10:37 +02:00
|
|
|
"""Lets user input text using an editor."""
|
2023-02-20 19:53:49 +01:00
|
|
|
tmp_path = _tmp_status_path()
|
2019-08-22 17:10:37 +02:00
|
|
|
initial_text = (initial_text or "") + EDITOR_INPUT_INSTRUCTIONS
|
|
|
|
|
2023-02-20 19:53:49 +01:00
|
|
|
if not _use_existing_tmp_file(tmp_path):
|
|
|
|
with open(tmp_path, "w") as f:
|
|
|
|
f.write(initial_text)
|
|
|
|
f.flush()
|
2019-08-22 17:10:37 +02:00
|
|
|
|
2023-02-20 19:53:49 +01:00
|
|
|
subprocess.run([editor, tmp_path])
|
2019-08-22 17:10:37 +02:00
|
|
|
|
2023-02-20 19:53:49 +01:00
|
|
|
with open(tmp_path) as f:
|
|
|
|
return f.read().split(EDITOR_DIVIDER)[0].strip()
|
2019-08-22 17:10:37 +02:00
|
|
|
|
2023-02-20 19:53:49 +01:00
|
|
|
|
2023-12-05 08:15:27 +01:00
|
|
|
def delete_tmp_status_file() -> None:
|
2023-02-20 19:53:49 +01:00
|
|
|
try:
|
|
|
|
os.unlink(_tmp_status_path())
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def _tmp_status_path() -> str:
|
|
|
|
tmp_dir = tempfile.gettempdir()
|
|
|
|
return f"{tmp_dir}/.status.toot"
|
|
|
|
|
|
|
|
|
2023-12-04 18:45:40 +01:00
|
|
|
def _use_existing_tmp_file(tmp_path: str) -> bool:
|
2023-02-20 19:53:49 +01:00
|
|
|
if os.path.exists(tmp_path):
|
2023-12-04 18:45:40 +01:00
|
|
|
click.echo(f"Found draft status at: {tmp_path}")
|
|
|
|
|
|
|
|
choice = click.Choice(["O", "D"], case_sensitive=False)
|
|
|
|
char = click.prompt("Open or Delete?", type=choice, default="O")
|
|
|
|
return char == "O"
|
2023-02-20 19:53:49 +01:00
|
|
|
|
|
|
|
return False
|
2023-03-02 11:28:24 +01:00
|
|
|
|
|
|
|
|
2023-12-05 08:15:27 +01:00
|
|
|
def drop_empty_values(data: Dict[Any, Any]) -> Dict[Any, Any]:
|
2023-03-02 11:28:24 +01:00
|
|
|
"""Remove keys whose values are null"""
|
|
|
|
return {k: v for k, v in data.items() if v is not None}
|
2023-03-07 10:37:03 +01:00
|
|
|
|
|
|
|
|
2023-12-05 08:15:27 +01:00
|
|
|
def urlencode_url(url: str) -> str:
|
2023-09-23 03:32:19 +02:00
|
|
|
parsed_url = urlparse(url)
|
|
|
|
|
|
|
|
# unencode before encoding, to prevent double-urlencoding
|
|
|
|
encoded_path = quote(unquote(parsed_url.path), safe="-._~()'!*:@,;+&=/")
|
|
|
|
encoded_query = urlencode({k: quote(unquote(v), safe="-._~()'!*:@,;?/") for k, v in parsed_url.params})
|
|
|
|
encoded_url = parsed_url._replace(path=encoded_path, params=encoded_query).geturl()
|
|
|
|
|
|
|
|
return encoded_url
|