def fpaste_it(inputdata, lang='text', author=None, password=None, private='no', expire=28, project=None, url='http://paste.fedoraproject.org'):
"""Submit a new paste to fedora project pastebin."""
# Establish critical params
params = {
'paste_data': inputdata,
'paste_lang': lang,
'api_submit': 'true',
'mode': 'json',
'paste_private': private,
'paste_expire': str(expire*24*60*60),
}
# Add optional params
if password:
params['paste_password'] = password
if project:
params['paste_project'] = project
if author:
# If author is too long, truncate
if len(author) > 50:
author = author[0:47] + "..."
params['paste_user'] = author
# Check size of what we're about to post and raise exception if too big
# FIXME: Figure out how to do this in requests without wasteful call to urllib.urlencode()
from urllib import urlencode
p = urlencode(params)
pasteSizeKiB = len(p)/1024.0
if pasteSizeKiB >= 512:
raise ValueError("Fedora Pastebin client WARN: paste size ({0:.1f} KiB) too large (max size: 512 KiB)".format(pasteSizeKiB))
# Print status, then connect
logger.log(25, "Fedora Pastebin client uploading {0:.1f} KiB...".format(pasteSizeKiB))
r = requests.post(url, params)
r.raise_for_status()
try:
j = r.json()
except:
# If no json returned, we've hit some weird error
from tempfile import NamedTemporaryFile
tmp = NamedTemporaryFile(delete=False)
print(r.content, file=tmp)
tmp.flush()
raise ValueError("Fedora Pastebin client ERROR: Didn't receive expected JSON response (saved to '{0}' for debugging)".format(tmp.name))
# Error keys adapted from Jason Farrell's fpaste
if j.has_key('error'):
err = j['error']
if err == 'err_spamguard_php':
raise ValueError("Fedora Pastebin server ERROR: Poster's IP rejected as malicious")
elif err == 'err_spamguard_noflood':
raise ValueError("Fedora Pastebin server ERROR: Poster's IP rejected as trying to flood")
elif err == 'err_spamguard_stealth':
raise ValueError("Fedora Pastebin server ERROR: Paste input triggered spam filter")
elif err == 'err_spamguard_ipban':
raise ValueError("Fedora Pastebin server ERROR: Poster's IP rejected as permanently banned")
elif err == 'err_author_numeric':
raise ValueError("Fedora Pastebin server ERROR: Poster's author should be alphanumeric")
else:
raise ValueError("Fedora Pastebin server ERROR: '{0}'".format(err))
# Put together URL with optional hash if requested
pasteUrl = '{0}/{1}'.format(url, j['result']['id'])
if 'yes' in private and j['result'].has_key('hash'):
pasteUrl += '/{0}'.format(j['result']['hash'])
return pasteUrl
评论列表
文章目录