Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add expiry days option to pastebin magic and change http protocol to https #13056

Merged
merged 8 commits into from Jul 15, 2021
20 changes: 17 additions & 3 deletions IPython/core/magics/code.py
Expand Up @@ -249,7 +249,7 @@ def pastebin(self, parameter_s=''):
"""Upload code to dpaste.com, returning the URL.

Usage:\\
%pastebin [-d "Custom description"] 1-7
%pastebin [-d "Custom description"][-e 24] 1-7

The argument can be an input history range, a filename, or the name of a
string or macro.
Expand All @@ -258,25 +258,39 @@ def pastebin(self, parameter_s=''):

-d: Pass a custom description. The default will say
"Pasted from IPython".
-e: Pass number of days for the link to be expired.
The default will be 7 days.
"""
opts, args = self.parse_options(parameter_s, 'd:')
opts, args = self.parse_options(parameter_s, "d:e:")

try:
code = self.shell.find_user_code(args)
except (ValueError, TypeError) as e:
print(e.args[0])
return

expiry_days = 7
MrMino marked this conversation as resolved.
Show resolved Hide resolved
if "e" in opts:
eyenpi marked this conversation as resolved.
Show resolved Hide resolved
try:
expiry_days = int(opts.get("e", 7))
except ValueError as e:
print(e.args[0])
eyenpi marked this conversation as resolved.
Show resolved Hide resolved
return
if expiry_days < 1 or expiry_days > 365:
print("Expiry days should be in range of 1 to 365.")
eyenpi marked this conversation as resolved.
Show resolved Hide resolved
return

post_data = urlencode(
{
"title": opts.get("d", "Pasted from IPython"),
"syntax": "python",
"content": code,
"expiry_days": expiry_days,
}
).encode("utf-8")

request = Request(
"http://dpaste.com/api/v2/",
"https://dpaste.com/api/v2/",
headers={"User-Agent": "IPython v{}".format(version)},
)
response = urlopen(request, post_data)
Expand Down