ActiveState Code

Recipe 576805: Pastebin Upload


A little script I made for some buddies and I. We are constantly collaborating on code. This scrips takes a source code file as it's parameter and uploads it to http://pastebin.com or any sub domain of pastebin. I integrated it with the righ click window in windows. Without that integration the script wouldn't be as cool! Hope others find it useful.

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Uploads source files to pastebin.com 
# or other subdomain of pastebin. Supports
# several filetypes. Username and subdomain
# constants provided.  In order to use this 
# to it's full potential you can make some registry 
# edits to allow a right click 'Upload To Pastebin' 
# option. I will have a script that automates that for you.
# Until then follow this guide 
#     http://www.jfitz.com/tips/rclick_custom.html
#
# This is the value I used in my registry:
# C:\Python26\pythonw.exe  C:\Python26\pastebin.py "%1"
#
# Enjoy,  : Check out logickills.org for more code!
# LogicKills


import urllib
import httplib
import sys
import string
import os.path

# Constants
URL = "http://SubDomain.pastebin.com"  
USER = "darkc0de supporter"


# Returns actual source code from file
def readFile():
   fileIn = open(sys.argv[1],"r")
   content = fileIn.read()
   return content

# Returns the file's extension (ex: .cpp)
def getExtension():
   fileName = sys.argv[1]
   extension = os.path.splitext(fileName)[1]
   return extension

# Returns extensions corelated label
def getCodeType(ext):
   codeType = ""
   extensions = [
      ".py","python",
      ".cpp","cpp",
      ".sh","bash",
      ".pl","perl",
      ".php","php",
      ".LUA","lua",
      ".js", "javascript",
      ".java","java",
      ".html","html4strict",
      ".cs","csharp"
      ]
      
   x = 0
   while x < 5:
      if extensions[x] == ext:
         codeType = extensions[x + 1]
         break
      else:
         x += 2
   
   return codeType
      
   
   
   
def postIt(codeType,theCode):
   POST = "/pastebin.php parent_pid=&format=" + codeType + "&code2=" + theCode + "&poster=" + USER + "&paste=Send&expiry=f&email="
   
   urllib.urlopen(URL,POST);
   

def main():
   content = readFile()
   extension = getExtension()
   codeType = getCodeType(extension)
   postIt(codeType,content)
   

   
if __name__ == "__main__":
   main()

Discussion

I am currently working on another script that will automate the registry tweaking :D

Comments

  1. 1. At 4:07 p.m. on 13 jun 2009, Stephen Chappell said:

    While working on the other script, would you mind using the registry API provided in http://code.activestate.com/recipes/510392/ ? If you need any help using it, I would be happy to provide assistance!

Sign in to comment