All pastes #754195 Raw Edit

juancarlos

public text v1 · immutable
#754195 ·published 2007-10-29 21:12 UTC
rendered paste body
#!/usr/bin/env pythonimport getopt, sys, urllib, urllib2, re, pwd, os		TYPES = ["raw", "asterisk", "c", "cpp", "php", "perl", "java", "vb", "csharp","ruby", "python", "pascal", "mirc", "pli", "xml", "sql", "scheme","ascript", "ada", "apache", "nasm", "asp", "bash", "css", "delphi", "html", "js","lisp", "lua", "asm", "objc", "vbnet" ]NAME_TYPE_DICT = {"HTML"   : "html",   "CSS" : "css",    "JavaScript" : "js",    "sh"  : "bash", "Python" : "python", "PHP" : "php",    "Perl"       : "perl",  "SQL" : "sql","C"      : "c",      "Ada" : "ada",    "Lua"        : "lua",   "C++" : "cpp","Pascal" : "pascal", "C#"  : "csharp", "XML"        : "xml","VB.NET" : "vbnet",  "Java": "java",   "Ruby"       : "ruby" }EXPIRATIONS = ["", "5 minutes", "10 minutes", "15 minutes", "30 minutes", "45 minutes","1 hour", "2 hours", "4 hours", "8 hours", "12 hours", "1 day", "2 days","3 days", "1 week", "2 weeks", "3 weeks", "1 month", "2 months", "3 months","4 months", "5 months", "6 months", "1 year" ]POSTDATA = "content=%(content)s&description=%(description)s&type=%(type)s&expiry=%(expiry)s&name=%(name)s&save=0&s=Submit+Post"URL = "http://en.pastebin.ca/index.php"URL_PATTERN = re.compile('http://en.pastebin.ca/\d+')RAW_MODE = Falsedef got_pastebin_url(url):	print url		def paste(content, description="", name="", expiry="", type="raw"):	payload = POSTDATA % {		"content" : urllib.quote_plus(content),		"description" : urllib.quote_plus(description),		"type" : TYPES.index(type)+1,		"expiry" : urllib.quote_plus(expiry),		"name" : urllib.quote_plus(name)		}	req = urllib2.Request(URL, payload, {		"Referer": "http://en.pastebin.ca/",		"Content-Type": "application/x-www-form-urlencoded",		"User-Agent": "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.10) Gecko/20050813 Epiphany/1.7.6",		"Accept-Language": "en",		"Accept-Charset": "utf-8",		})	return urllib2.urlopen(req).read()def got_error(err):	sys.stderr.write("%r\n" % err)def got_page(res):	matches = URL_PATTERN.findall(res)	if len(matches) > 0:		match = matches[0]		if RAW_MODE:			got_pastebin_url(match.replace("pastebin.ca/","pastebin.ca/raw/"))		else:			got_pastebin_url(match)	else:		sys.stderr.write("Could not retreive pastebin url\n")		def usage():	print """=== Pastebin: Usage$ pastebin [OPTIONS] [FILE]Print the pastebin.ca URL of the input fileIf FILE is omitted content is read from standard inputOPTIONS:	-h, --help			Print this help notice.	-r, --raw			Retreive raw text URL (default=off).	-d, --description	The description of the content (default=empty).	-m, --message		Same as description	-n, --name			Your name (default=your unix username).	-l, --lines			The line numbers to send starting at 1 (default=1,0)						Ex: 1,12 or 12,36 or 45 or 45,0	-t, --type			The type of the content (default="raw")		Type can be one of:	raw, asterisk, c, cpp, php, perl, java, vb, csharp,	ruby, python, pascal, mirc, pli, xml, sql, scheme,	ascript, ada, apache, nasm, asp, bash, css, delphi, html, js,	lisp, lua, asm, objc, vbnet		If type is not specified, an attempt to auto-detect type is made,	based on file data/extension. Detection is more reliable when giving FILE	argument than piping directly. This needs modules gnomevfs and gtksourceview		If you have module gtk, the pastebin url will be placed in your clipboard,	you can then paste it with ctrl-v as usual."""if __name__ == "__main__":	man = None	 	content = ""	description = ""	name = pwd.getpwuid(os.getuid())[0]	expiry = ""	type = "raw"	start = 1	end = 0	try:		opts, args = getopt.getopt(sys.argv[1:], "hrd:m:n:t:l:", ["help", "raw", "description=", "message=", "name=", "type=", "lines="])	except getopt.GetoptError:		usage()		sys.exit()		for o, a in opts:		if o in ("-h", "--help"):			usage()			sys.exit()		if o in ("-l", "--lines"):			try:				lines = a.split(",")				if len(lines) == 2:					if lines[0] != "" and lines[1] != "":						start = int(lines[0])						end = int(lines[1])					elif lines[0] == "" and lines[1] != "":						start = 1						end = int(lines[1])					elif lines[0] != "" and lines[1] == "":						start = int(lines[0])						end = 0				elif len(lines) == 1:					start = int(lines[0])			except Exception:				start = 1				end = 0							if start > end and end != 0 or start < 1 or end < 0:				sys.stderr.write('Error with lines numbers, start:%d end:%d\n' % (start, end))				sys.exit()		f = None	try:		if len(args) == 0:			f = sys.stdin		else:			f = file(args[0], 'r')		lines = None		if end == 0:			lines = f.readlines()[start-1:]		else:			lines = f.readlines()[start-1:end]					content = ''.join(lines)		f.close()	except Exception, msg:		sys.stderr.write('Error while reading the file: %s\n' % msg)		sys.exit()	if content.strip() == "":		sys.stderr.write('Content Empty: "%s"\n' % content)		sys.exit()	elif not type in TYPES:		sys.stderr.write('Unknown type: %s\n' % type)		sys.exit()	try:		res = paste(content, description, name, expiry, type)		got_page(res)	except Exception, msg:		got_error(msg)