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 | class="st">"""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 | class="st">""" |
| 82 | import re |
| 83 | from urllib.error import HTTPError |
| 84 | from urllib.parse import urlencode |
| 85 | from urllib.request import urlopen |
| 86 | |
| 87 | params = {class="st">"code": contents, class="st">"lexer": class="st">"text", class="st">"expiry": class="st">"1week"} |
| 88 | url = class="st">"https://bpa.st" |
| 89 | try: |
| 90 | response: str = ( |
| 91 | urlopen(url, data=urlencode(params).encode(class="st">"ascii")).read().decode(class="st">"utf-8") |
| 92 | ) |
| 93 | except HTTPError as e: |
| 94 | with e: class="cm"># HTTPErrors are also http responses that must be closed! |
| 95 | return fclass="st">"bad response: {e}" |
| 96 | except OSError as e: class="cm"># eg urllib.error.URLError |
| 97 | return fclass="st">"bad response: {e}" |
| 98 | m = re.search(r&class="cm">#x27;href=class="st">"/raw/(\w+)"', response) |
| 99 | if m: |
| 100 | return fclass="st">"{url}/show/{m.group(1)}" |
| 101 | else: |
| 102 | return class="st">"bad response: invalid format (&class="cm">#x27;" + response + class="st">"')" |
| 103 | |
| 104 | |
| 105 | def pytest_terminal_summary(terminalreporter: TerminalReporter) -> None: |
no test coverage detected