Create a new paste using the bpaste.net service. :contents: Paste contents string. :returns: URL to the pasted contents, or an error message.
(contents: str | bytes)
| 74 | |
| 75 | |
| 76 | def create_new_paste(contents: str | bytes) -> str: |
| 77 | """Create a new paste using the bpaste.net service. |
| 78 | |
| 79 | :contents: Paste contents string. |
| 80 | :returns: URL to the pasted contents, or an error message. |
| 81 | """ |
| 82 | import re |
| 83 | from urllib.error import HTTPError |
| 84 | from urllib.parse import urlencode |
| 85 | from urllib.request import urlopen |
| 86 | |
| 87 | params = {"code": contents, "lexer": "text", "expiry": "1week"} |
| 88 | url = "https://bpa.st" |
| 89 | try: |
| 90 | response: str = ( |
| 91 | urlopen(url, data=urlencode(params).encode("ascii")).read().decode("utf-8") |
| 92 | ) |
| 93 | except HTTPError as e: |
| 94 | with e: # HTTPErrors are also http responses that must be closed! |
| 95 | return f"bad response: {e}" |
| 96 | except OSError as e: # eg urllib.error.URLError |
| 97 | return f"bad response: {e}" |
| 98 | m = re.search(r'href="/raw/(\w+)"', response) |
| 99 | if m: |
| 100 | return f"{url}/show/{m.group(1)}" |
| 101 | else: |
| 102 | return "bad response: invalid format ('" + response + "')" |
| 103 | |
| 104 | |
| 105 | def pytest_terminal_summary(terminalreporter: TerminalReporter) -> None: |
no test coverage detected
searching dependent graphs…