LEO Node Button to Upload Node to Snipplr as a Snippet


/ Published in: Python
Save to your folder(s)

This is for the LEO editor. Create a node containing this script. Add your API key at the bottom. Then click the script button button when it is selected. Now you can upload any node to Snipplr as a snippet.


Copy this code and paste it in your HTML
  1. import xmlrpclib
  2. import os
  3. import urllib2
  4.  
  5. class SnipplrPy:
  6. """
  7. Wrapper around xml-rpc methods exposed by
  8. Snipplr.
  9. """
  10.  
  11. def __init__(self):
  12. """
  13. Constructor
  14. """
  15. self.api_key = None
  16. self.server_url = "http://snipplr.com/xml-rpc.php"
  17. self.server = None
  18. self.initialized = False
  19. self.languages = {}
  20.  
  21. def __try_connect__(self):
  22. """
  23. Private method, try to connect with xml-rpc endpoint if
  24. still not connected
  25. """
  26. if self.server is None:
  27. try:
  28. self.server = xmlrpclib.Server(self.server_url)
  29. return True
  30. except xmlrpclib.Fault:
  31. return False
  32. return True
  33.  
  34. def check_api_key(self, key):
  35. """
  36. Checks if the key suministrated is valid or not
  37. """
  38. if self.__try_connect__() == False:
  39. return False
  40.  
  41. try:
  42. if self.server.user.checkkey(key) == 0:
  43. return False
  44. return True
  45. except xmlrpclib.Fault:
  46. return False
  47.  
  48. def list_languages(self):
  49. """
  50. List snipplr languages
  51. """
  52. if len(self.languages.keys()) != 0:
  53. return self.languages
  54.  
  55. if self.__try_connect__() == False:
  56. return None
  57.  
  58. try:
  59. self.languages = self.server.languages.list()
  60. return self.languages
  61. except xmlrpclib.Fault:
  62. return None
  63.  
  64. def setup(self, api_key):
  65. """
  66. Setup the object with a valid api_key
  67. """
  68. if self.check_api_key(api_key) == False:
  69. self.initialized = False
  70. return False;
  71.  
  72. self.api_key = api_key
  73. self.initialized = True
  74.  
  75. return True
  76.  
  77. def get(self, id, parse_source = False):
  78. """
  79. Get the snippet specified by id
  80. None if the id is not valid.
  81. We dont need the api key here.
  82. If parse_source is True, replace html html encoding with
  83. the implicit value
  84. """
  85. if self.__try_connect__() == False:
  86. return None
  87.  
  88. try:
  89. result = self.server.snippet.get(id)
  90. if parse_source:
  91. self.__convert_snippet_source__(result)
  92. return result
  93. except xmlrpclib.Fault:
  94. return None
  95.  
  96. def get_source_plain_text(self, id):
  97. """
  98. Get the source code in plain
  99. text format
  100. """
  101.  
  102. url = "http://snipplr.com/view.php?id=" + str(id) + "&plaintext"
  103.  
  104. try:
  105. handler = urllib2.urlopen(url)
  106. source = handler.read()
  107. handler.close()
  108. return source
  109. except Exception, e:
  110. return None
  111.  
  112. def list(self, tags = None, sort = None, limit = None):
  113. """
  114. List the user snnipets.
  115. tags is a space separated string with the tags names.
  116. sort can be one of these three values: title, date, random. DEACTIVATED
  117. limit is the max number of snippet returned. DEACTIVATED
  118. """
  119. if self.initialized == False:
  120. return None
  121.  
  122. try:
  123. if tags == None:
  124. t = ""
  125. else:
  126. t = tags
  127. return self.server.snippet.list(self.api_key, t)
  128. except xmlrpclib.Fault:
  129. return None
  130.  
  131. def post(self, title, code, tags = None, language = None):
  132. """
  133. Post a new snippet. Returns True if proccess ok
  134. or False in other case
  135. """
  136.  
  137. if self.initialized == False:
  138. return False
  139.  
  140. if language:
  141. if not language in self.list_languages():
  142. error_str = "Language not included into Snipplr language list " + " ".join(self.languages.keys())
  143. raise Exception(error_str)
  144.  
  145.  
  146. try:
  147. #Default values
  148. t = tags
  149. if t == None:
  150. t = ""
  151. l = language
  152. if l == None:
  153. l = ""
  154. self.server.snippet.post(self.api_key, title, code, t, l)
  155. return True
  156. except xmlrpclib.Fault:
  157. return False
  158.  
  159. def post_file(self, file, title, tags = None, language = None):
  160. """
  161. Post a snippet taken the code from a file.
  162. Returns True if succedded, or False in other case
  163. """
  164. if os.access(file, os.R_OK):
  165. handler = open(file, "r")
  166. code = handler.read()
  167. handler.close()
  168.  
  169. return self.post(title, code, tags, language)
  170.  
  171. return False
  172.  
  173. def delete(self, id):
  174. """
  175. Try to delete the snippet specified by id.
  176. Returns true if action is taken or false in other case
  177. """
  178. if self.initialized == False:
  179. return False
  180.  
  181. try:
  182. if self.server.snippet.delete(self.api_key, id) == 0:
  183. return False
  184. return True
  185. except xmlrpclib.Fault:
  186. return False
  187.  
  188. def __convert_snippet_source__(self, snippet):
  189. """
  190. Replace all html chars with their implicit
  191. values
  192. """
  193. #Yes i know this is actually soooo ugly and we should use regexp :P
  194. snippet["source"] = snippet["source"].replace("&quot;","\"").replace("&lt;","<").replace("&gt;",">")
  195.  
  196. return snippet
  197. if __name__ == "__main__":
  198. from grun import grun
  199. mysnipplr = SnipplrPy()
  200. mysnipplr.setup("REPLACEWITHAPIKEY")
  201.  
  202. #get tags
  203. @grun
  204. def ask_tags(tags):
  205. """ Any Tags for this Snippet? """
  206. return tags
  207.  
  208. tags = ask_tags()
  209. print mysnipplr.post(p.h,p.b,tags,"python")

URL: http://webpages.charter.net/edreamleo/front.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.