All pastes #21719 Raw Edit

MacSlow

public python v1 · immutable
#21719 ·published 2005-08-31 13:48 UTC
rendered paste body
#!/usr/bin/env python## pastebin, a script to upload a file to pastebin.ca from the console.## Copyright (C) 2005 Raphael Slinckx <raphael@slinckx.net>## This program is free software; you can redistribute it and/or# modify it under the terms of the GNU General Public License# as published by the Free Software Foundation; either version 2# of the License, or (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with this program; if not, write to the Free Software# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.#import getopt, sys, urllib, re, pwd, ostry:	import pygtk	pygtk.require("2.0")	import gtk	WITH_GTK = True	from twisted.internet import gtk2reactor	gtk2reactor.install()except ImportError:	WITH_GTK = Falsetry:	import gnomevfs, gtksourceview	WITH_GNOMEVFS = Trueexcept ImportError:	WITH_GNOMEVFS = False		from twisted.internet import reactorfrom twisted.web import client# General ConstantsTYPES = ["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 = False# The Meatdef got_pastebin_url(url):	if WITH_GTK:		gtk.clipboard_get().set_text(url)		gtk.clipboard_get(gtk.gdk.SELECTION_CLIPBOARD).set_text(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)		}		return client.getPage(URL, postdata=payload, method="POST", headers={		"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",		})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).	-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__":	if WITH_GNOMEVFS:		man = gtksourceview.SourceLanguagesManager()	else:		man = None	 	# Default Values	content = ""	description = ""	name = pwd.getpwuid(os.getuid())[0]	expiry = ""	type = "raw"	try:		opts, args = getopt.getopt(sys.argv[1:], "hrd:m:n:t:", ["help", "raw", "description=", "message=", "name=", "type="])	except getopt.GetoptError:		usage()		sys.exit()		for o, a in opts:		if o in ("-h", "--help"):			usage()			sys.exit()				# Open the file, and read content.	f = None	try:		if len(args) == 0:			f = sys.stdin		else:			f = file(args[0], 'r')		content = ''.join(f.readlines())		f.close()	except Exception, msg:		sys.stderr.write('Error while reading the file: %s\n' % msg)		sys.exit()	# Try to guess file content type if given, and we have GNOMEVFS	if WITH_GNOMEVFS:		mimes = [gnomevfs.get_mime_type_for_data(content)]		if len(args) > 0:			mimes.append(gnomevfs.get_mime_type(os.path.abspath(args[0])))					for mime in mimes:			lang = man.get_language_from_mime_type(mime)			if lang != None:				name = lang.get_name()				try:					type = NAME_TYPE_DICT[name]				except KeyError:					sys.stderr.write('No pastebin langage matching name "%s"\n' % name)					# Parse options	for o, a in opts:		if o in ("-r", "--raw"):			RAW_MODE = True		elif o in ("-d", "--description") or o in ("-m", "--message"):			description = a		elif o in ("-n", "--name"):			name = a		elif o in ("-t", "--type"):			type = a	#Parameter validation	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()		paste(content, description, name, expiry, type).addCallbacks(got_page, got_error).addBoth(lambda x: reactor.stop())	reactor.run()