##// END OF EJS Templates
Backport PR #12712: dpaste.com API correction
Matthias Bussonnier -
Show More
@@ -20,7 +20,7 b' import re'
20 import sys
20 import sys
21 import ast
21 import ast
22 from itertools import chain
22 from itertools import chain
23 from urllib.request import urlopen
23 from urllib.request import Request, urlopen
24 from urllib.parse import urlencode
24 from urllib.parse import urlencode
25
25
26 # Our own packages
26 # Our own packages
@@ -28,6 +28,7 b' from IPython.core.error import TryNext, StdinNotImplementedError, UsageError'
28 from IPython.core.macro import Macro
28 from IPython.core.macro import Macro
29 from IPython.core.magic import Magics, magics_class, line_magic
29 from IPython.core.magic import Magics, magics_class, line_magic
30 from IPython.core.oinspect import find_file, find_source_lines
30 from IPython.core.oinspect import find_file, find_source_lines
31 from IPython.core.release import version
31 from IPython.testing.skipdoctest import skip_doctest
32 from IPython.testing.skipdoctest import skip_doctest
32 from IPython.utils.contexts import preserve_keys
33 from IPython.utils.contexts import preserve_keys
33 from IPython.utils.path import get_py_filename
34 from IPython.utils.path import get_py_filename
@@ -244,7 +245,7 b' class CodeMagics(Magics):'
244
245
245 @line_magic
246 @line_magic
246 def pastebin(self, parameter_s=''):
247 def pastebin(self, parameter_s=''):
247 """Upload code to dpaste's paste bin, returning the URL.
248 """Upload code to dpaste.com, returning the URL.
248
249
249 Usage:\\
250 Usage:\\
250 %pastebin [-d "Custom description"] 1-7
251 %pastebin [-d "Custom description"] 1-7
@@ -254,7 +255,7 b' class CodeMagics(Magics):'
254
255
255 Options:
256 Options:
256
257
257 -d: Pass a custom description for the gist. The default will say
258 -d: Pass a custom description. The default will say
258 "Pasted from IPython".
259 "Pasted from IPython".
259 """
260 """
260 opts, args = self.parse_options(parameter_s, 'd:')
261 opts, args = self.parse_options(parameter_s, 'd:')
@@ -265,13 +266,19 b' class CodeMagics(Magics):'
265 print(e.args[0])
266 print(e.args[0])
266 return
267 return
267
268
268 post_data = urlencode({
269 post_data = urlencode(
269 "title": opts.get('d', "Pasted from IPython"),
270 {
270 "syntax": "python3",
271 "title": opts.get("d", "Pasted from IPython"),
271 "content": code
272 "syntax": "python",
272 }).encode('utf-8')
273 "content": code,
273
274 }
274 response = urlopen("http://dpaste.com/api/v2/", post_data)
275 ).encode("utf-8")
276
277 request = Request(
278 "http://dpaste.com/api/v2/",
279 headers={"User-Agent": "IPython v{}".format(version)},
280 )
281 response = urlopen(request, post_data)
275 return response.headers.get('Location')
282 return response.headers.get('Location')
276
283
277 @line_magic
284 @line_magic
General Comments 0
You need to be logged in to leave comments. Login now