dPaste.com “API”
2011/11/14 Deja un comentario
“API” for http://dpaste.com/
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# THE WISKEY-WARE LICENSE
# -----------------------
#
# "THE WISKEY-WARE LICENSE":
# <jbc.develop@gmail.com> wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a wiskey in return JuanBC
#
# ==============================================================================
# DOC
# ==============================================================================
"""A Simple Client for http://dpaste.com/
"""
# ==============================================================================
#
# ==============================================================================
__author__ = "JuanBC"
__mail__ = "jbc.develop@gmail.com"
__version__ = "0.1.1"
__license__ = "WISKEY_WARE"
__date__ = "2011/11/14"
# ==============================================================================
# IMPORTS
# ==============================================================================
import os
import urllib
import urllib2
# ==============================================================================
# CONSTANTS
# ==============================================================================
DPASTE_URL = "http://dpaste.com/"
FORMAT_2_EXT = {
"Python": ["py", "pyw"],
"PythonConsole": [],
"Sql": ["sql"],
"DjangoTemplate": [],
"JScript": ["js"],
"Css": ["css"],
"Xml": ["xml"],
"Diff": ["diff"],
"Ruby": ["rb"],
"Rhtml": ["rhtml"],
"Haskell": ["hs"],
"Apache": [],
"Bash": ["sh"],
"Plain": ["txt"]
}
EXT_2_FORMAT = {}
for k, vs in FORMAT_2_EXT.items():
for v in vs:
EXT_2_FORMAT[v] = k
# ==============================================================================
# FUNCTIONS
# ==============================================================================
def filename2format(filename):
"""Retrieves the format of a given filename
"""
basename = os.path.basename(filename)
if "." in basename:
ext = basename.rsplit(".", 1)[1].lower()
return EXT_2_FORMAT.get(ext, "Plain")
return "Plain"
def paste(source, file_format="Plain", title="", poster=""):
"""Paste a given source code into dpaste.com with a given format
"""
file_format = "" if file_format == "Plain" else file_format
data = urllib.urlencode({"content": source,
"language": file_format,
"title": title,
"poster": poster})
conn = urllib2.urlopen(DPASTE_URL, data)
return conn.geturl()
def copy(dpaste_id):
"""Retrieve a code from a given dpaste id
"""
return urllib2.urlopen(DPASTE_URL + str(dpaste_id) + "/plain").read()
# ==============================================================================
# MAIN
# ==============================================================================
if __name__ == "__main__":
print(__doc__)

