dPaste.com “API”

“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__)

One thought on “dPaste.com “API”

  1. I seldom comment, but i did some searching and wound up
    here dPaste.com “API” | Jack & Hack. And I
    actually do have a few questions for you if it’s allright. Is it simply me or does it look like a few of the comments look like left by brain dead individuals? :-P And, if you are posting at additional places, I would like to follow anything new you have to post. Could you list of the complete urls of your community pages like your linkedin profile, Facebook page or twitter feed?

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos necesarios están marcados *

Puedes usar las siguientes etiquetas y atributos HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>