Advertisement
tata2772

Untitled

Jan 2nd, 2019
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.43 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #
  3. # pastebin, a script to upload a file to pastebin.ca from the console.
  4. #
  5. # Copyright (C) 2005 Raphael Slinckx <raphael@slinckx.net>
  6. # Thanks to Gustavo J. A. M. Carneiro <gjc@gnome.org>
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. #
  22.  
  23. import getopt, sys, urllib, urllib2, re, pwd, os
  24. try:
  25. import pygtk
  26. pygtk.require("2.0")
  27. import gtk
  28. import gobject
  29. WITH_GTK = True
  30. except ImportError:
  31. WITH_GTK = False
  32.  
  33. try:
  34. import gnomevfs, gtksourceview
  35. WITH_GNOMEVFS = True
  36. except ImportError:
  37. WITH_GNOMEVFS = False
  38.  
  39. # General Constants
  40. TYPES = [
  41. "raw", "asterisk", "c", "cpp", "php", "perl", "java", "vb", "csharp",
  42. "ruby", "python", "pascal", "mirc", "pli", "xml", "sql", "scheme",
  43. "ascript", "ada", "apache", "nasm", "asp", "bash", "css", "delphi", "html", "js",
  44. "lisp", "lua", "asm", "objc", "vbnet" ]
  45.  
  46. NAME_TYPE_DICT = {
  47. "HTML" : "html", "CSS" : "css", "JavaScript" : "js", "sh" : "bash",
  48. "Python" : "python", "PHP" : "php", "Perl" : "perl", "SQL" : "sql",
  49. "C" : "c", "Ada" : "ada", "Lua" : "lua", "C++" : "cpp",
  50. "Pascal" : "pascal", "C#" : "csharp", "XML" : "xml",
  51. "VB.NET" : "vbnet", "Java": "java", "Ruby" : "ruby" }
  52.  
  53. EXPIRATIONS = [
  54. "", "5 minutes", "10 minutes", "15 minutes", "30 minutes", "45 minutes",
  55. "1 hour", "2 hours", "4 hours", "8 hours", "12 hours", "1 day", "2 days",
  56. "3 days", "1 week", "2 weeks", "3 weeks", "1 month", "2 months", "3 months",
  57. "4 months", "5 months", "6 months", "1 year" ]
  58.  
  59. POSTDATA = "content=%(content)s&description=%(description)s&type=%(type)s&expiry=%(expiry)s&name=%(name)s&save=0&s=Submit+Post"
  60. URL = "http://en.pastebin.ca/index.php"
  61. URL_PATTERN = re.compile('http://en.pastebin.ca/\d+')
  62.  
  63. RAW_MODE = False
  64.  
  65. # The Meat
  66. def got_pastebin_url(url):
  67. print url
  68.  
  69. if WITH_GTK:
  70. gtk.clipboard_get(gtk.gdk.SELECTION_CLIPBOARD).set_text(url)
  71. gobject.timeout_add(1000, gtk.main_quit)
  72. gtk.main()
  73.  
  74. def paste(content, description="", name="", expiry="", type="raw"):
  75. payload = POSTDATA % {
  76. "content" : urllib.quote_plus(content),
  77. "description" : urllib.quote_plus(description),
  78. "type" : TYPES.index(type)+1,
  79. "expiry" : urllib.quote_plus(expiry),
  80. "name" : urllib.quote_plus(name)
  81. }
  82. req = urllib2.Request(URL, payload, {
  83. "Referer": "http://en.pastebin.ca/",
  84. "Content-Type": "application/x-www-form-urlencoded",
  85. "User-Agent": "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.10) Gecko/20050813 Epiphany/1.7.6",
  86. "Accept-Language": "en",
  87. "Accept-Charset": "utf-8",
  88. })
  89. return urllib2.urlopen(req).read()
  90.  
  91. def got_error(err):
  92. sys.stderr.write("%r\n" % err)
  93.  
  94. def got_page(res):
  95. matches = URL_PATTERN.findall(res)
  96. if len(matches) > 0:
  97. match = matches[0]
  98. if RAW_MODE:
  99. got_pastebin_url(match.replace("pastebin.ca/","pastebin.ca/raw/"))
  100. else:
  101. got_pastebin_url(match)
  102. else:
  103. sys.stderr.write("Could not retreive pastebin url\n")
  104.  
  105. def usage():
  106. print """=== Pastebin: Usage
  107. $ pastebin [OPTIONS] [FILE]
  108.  
  109. Print the pastebin.ca URL of the input file
  110.  
  111. If FILE is omitted content is read from standard input
  112. OPTIONS:
  113. -h, --help Print this help notice.
  114. -r, --raw Retreive raw text URL (default=off).
  115. -d, --description The description of the content (default=empty).
  116. -m, --message Same as description
  117. -n, --name Your name (default=your unix username).
  118. -l, --lines The line numbers to send starting at 1 (default=1,0)
  119. Ex: 1,12 or 12,36 or 45 or 45,0
  120. -t, --type The type of the content (default="raw")
  121.  
  122. Type can be one of:
  123. raw, asterisk, c, cpp, php, perl, java, vb, csharp,
  124. ruby, python, pascal, mirc, pli, xml, sql, scheme,
  125. ascript, ada, apache, nasm, asp, bash, css, delphi, html, js,
  126. lisp, lua, asm, objc, vbnet
  127.  
  128. If type is not specified, an attempt to auto-detect type is made,
  129. based on file data/extension. Detection is more reliable when giving FILE
  130. argument than piping directly. This needs modules gnomevfs and gtksourceview
  131.  
  132. If you have module gtk, the pastebin url will be placed in your clipboard,
  133. you can then paste it with ctrl-v as usual.
  134. """
  135.  
  136. if __name__ == "__main__":
  137. if WITH_GNOMEVFS:
  138. man = gtksourceview.SourceLanguagesManager()
  139. else:
  140. man = None
  141.  
  142. # Default Values
  143. content = ""
  144. description = ""
  145. name = pwd.getpwuid(os.getuid())[0]
  146. expiry = ""
  147. type = "raw"
  148. start = 1
  149. end = 0
  150.  
  151. try:
  152. opts, args = getopt.getopt(sys.argv[1:], "hrd:m:n:t:l:", ["help", "raw", "description=", "message=", "name=", "type=", "lines="])
  153. except getopt.GetoptError:
  154. usage()
  155. sys.exit()
  156.  
  157. for o, a in opts:
  158. if o in ("-h", "--help"):
  159. usage()
  160. sys.exit()
  161. if o in ("-l", "--lines"):
  162. #Parse the line numbers
  163. try:
  164. lines = a.split(",")
  165. if len(lines) == 2:
  166. if lines[0] != "" and lines[1] != "":
  167. start = int(lines[0])
  168. end = int(lines[1])
  169. elif lines[0] == "" and lines[1] != "":
  170. start = 1
  171. end = int(lines[1])
  172. elif lines[0] != "" and lines[1] == "":
  173. start = int(lines[0])
  174. end = 0
  175. elif len(lines) == 1:
  176. start = int(lines[0])
  177. except Exception:
  178. start = 1
  179. end = 0
  180.  
  181. if start > end and end != 0 or start < 1 or end < 0:
  182. sys.stderr.write('Error with lines numbers, start:%d end:%d\n' % (start, end))
  183. sys.exit()
  184.  
  185. # Open the file, and read content.
  186. f = None
  187. try:
  188. if len(args) == 0:
  189. f = sys.stdin
  190. else:
  191. f = file(args[0], 'r')
  192.  
  193. lines = None
  194. if end == 0:
  195. lines = f.readlines()[start-1:]
  196. else:
  197. lines = f.readlines()[start-1:end]
  198.  
  199. content = ''.join(lines)
  200. f.close()
  201. except Exception, msg:
  202. sys.stderr.write('Error while reading the file: %s\n' % msg)
  203. sys.exit()
  204.  
  205. # Try to guess file content type if given, and we have GNOMEVFS
  206. if WITH_GNOMEVFS:
  207. mimes = [gnomevfs.get_mime_type_for_data(content)]
  208. if len(args) > 0:
  209. mimes.append(gnomevfs.get_mime_type(os.path.abspath(args[0])))
  210.  
  211. for mime in mimes:
  212. lang = man.get_language_from_mime_type(mime)
  213. if lang != None:
  214. name = lang.get_name()
  215. try:
  216. type = NAME_TYPE_DICT[name]
  217. sys.stderr.write('Using source type "%s"\n' % type)
  218. except KeyError:
  219. sys.stderr.write('No pastebin langage matching name "%s"\n' % name)
  220.  
  221. # Parse options
  222. for o, a in opts:
  223. if o in ("-r", "--raw"):
  224. RAW_MODE = True
  225. elif o in ("-d", "--description") or o in ("-m", "--message"):
  226. description = a
  227. elif o in ("-n", "--name"):
  228. name = a
  229. elif o in ("-t", "--type"):
  230. type = a
  231.  
  232. #Parameter validation
  233. if content.strip() == "":
  234. sys.stderr.write('Content Empty: "%s"\n' % content)
  235. sys.exit()
  236. elif not type in TYPES:
  237. sys.stderr.write('Unknown type: %s\n' % type)
  238. sys.exit()
  239.  
  240. try:
  241. res = paste(content, description, name, expiry, type)
  242. got_page(res)
  243. except Exception, msg:
  244. got_error(msg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement