-
Notifications
You must be signed in to change notification settings - Fork 1
/
pasteee.py
112 lines (98 loc) · 3.48 KB
/
pasteee.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
pasteee module
Allows pasting to https://paste.ee
https://github.com/i-ghost/pasteee
"""
# 2 <-> 3
try:
from urllib.request import urlopen
from urllib.request import Request as urlrequest
from urllib.parse import urlencode
from urllib import error as urlerror
except ImportError:
from urllib2 import urlopen
from urllib2 import Request as urlrequest
from urllib import urlencode
import urllib2 as urlerror
import json
class PasteError(Exception):
"""Exception class for this module"""
pass
class Paste(object):
"""A paste.ee dictionary object
Returns a dictionary containing the following on a successful paste:
{
"id":"foobar",
"link":"https://paste.ee/p/foobar",
"min":"https://min.paste.ee/foobar",
"raw":"https://paste.ee/r/foobar",
"download":"https://paste.ee/d/foobar"
}
Raises a PasteError on an unsuccessful paste.
Options:
----
paste - str, paste data to send.
private - bool, indicates if paste should be private or public. \
Default: True
lang - str, indicates the syntax highlighting.
key - str, API key. Default: "public".
desc - str, paste description. Default: ""
expire - int, expiration time in seconds.
views - int, expire after this many views.
encrypted - bool, Doesn't seem to return anything meaningful.
http://paste.ee/wiki/API:Basics
----
Doctests:
>>> from pasteee import Paste
>>> paste = Paste(u"Foo bar\\nBaz")
>>> print(sorted(paste.keys())) # doctest: +ELLIPSIS
[...'download', ...'id', ...'link', ...'raw']
Exception doctest:
>>> paste = Paste(u"Foo bar\\nBaz", lang=123456789) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
File "<stdin>", line 1, in ?
PasteError: Invalid paste option: error_invalid_language
>>> paste = Paste(u"Foo bar\\nBaz", expire=15, views=10) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
File "<stdin>", line 1, in ?
PasteError: Options 'expire' and 'views' are mutually exclusive
"""
def __new__(cls, paste,
private=True, lang="plain",
key="public", desc="",
expire=0, views=0, encrypted=False):
if not paste:
raise PasteError("No paste provided")
if expire and views:
# API incorrectly returns success so we raise error locally
raise PasteError("Options 'expire' and 'views' are mutually exclusive")
request = urlrequest(
"https://paste.ee/api",
data=urlencode(
{
'paste': paste,
'private': bool(private),
'language': lang,
'key': key,
'description': desc,
'expire': expire,
'views': views,
'encrypted': bool(encrypted),
'format': "json"
}
).encode("utf-8"),
headers={'User-Agent': 'Mozilla/5.0'}
)
try:
result = json.loads(urlopen(request).read().decode("utf-8"))
return result["paste"]
except urlerror.HTTPError:
print("Couldn't send paste")
raise
except KeyError:
raise PasteError("Invalid paste option: %s" % (result["error"]))
if __name__ == "__main__":
import doctest
doctest.testmod()