Guest User

Untitled

a guest
Jul 15th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import sys
  4. import ssl
  5. import subprocess
  6.  
  7. from xmlrpclib import ServerProxy
  8. from markdown import markdown
  9.  
  10.  
  11. applescript_prompt_for_title = """
  12. tell application "BBEdit"
  13. set theDocument to the active document of the front text window
  14. end tell
  15.  
  16. set theDialogTitle to "Post " & "\\"" & the name of theDocument & "\\""
  17.  
  18. set theTitle to the text of (display dialog "Enter the post title" with title theDialogTitle buttons {"Cancel", "Post"} default button "Post" default answer "")
  19. """
  20.  
  21. applescript_get_text = """
  22. tell application "BBEdit"
  23. set theDocument to the active document of the front text window
  24. set theContent to the text of theDocument
  25. end tell
  26. """
  27.  
  28. applescript_display_dialog = 'display dialog "%s" with title "%s" buttons "Ok default button "Ok""'
  29.  
  30. class Server():
  31. def __init__(self):
  32. self.blog_id = 1 # arbitrary, unused
  33. # edit url, username, password before use
  34. self.url = ''
  35. self.username = ''
  36. self.password = ''
  37. self.proxy = ServerProxy(self.url , context=ssl.SSLContext(ssl.PROTOCOL_TLSv1_2))
  38. self.filter = {'orderby': 'post_id'}
  39. self.fields = ['post_title', 'post_id', 'guid', 'link']
  40. self.content_template = {'post_type'}
  41. def getPosts(self, filter=None, fields=None):
  42. wp_filter = filter or self.filter
  43. wp_fields = fields or self.fields
  44. return self.proxy.wp.getPosts(self.blog_id, self.username, self.password, wp_filter, wp_fields)
  45. def newPost(self, post):
  46. return self.proxy.wp.newPost(self.blog_id, self.username, self.password, post.content())
  47.  
  48.  
  49. class Post():
  50. def __init__(self, title, body):
  51. self.title = title
  52. self.body = body
  53. def content(self):
  54. return {'post_title': self.title,
  55. 'post_content': self.body}
  56.  
  57.  
  58. def render(text):
  59. extensions = ['markdown.extensions.extra',
  60. 'markdown.extensions.codehilite',
  61. 'markdown.extensions.smarty']
  62. extension_config = {'markdown.extensions.codehilite':
  63. {'linenums': False, 'guess_lang': False}}
  64. return markdown(text, extensions=extensions, extension_configs=extension_config,
  65. outputformat='html5')
  66.  
  67.  
  68. def run_applescript(script):
  69. process = subprocess.Popen(['osascript', '-'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  70. try:
  71. outdata, errdata = process.communicate(script)
  72. except:
  73. pass
  74. if outdata == "":
  75. return None
  76. return outdata
  77.  
  78. def prompt_for_title():
  79. result = run_applescript(applescript_prompt_for_title)
  80. title = ','.join(result.split(',')[1:]).strip()
  81. return title
  82.  
  83. def get_document_text():
  84. return run_applescript(applescript_get_text)
  85.  
  86. def display_dialog(message, title="Post To Blog"):
  87. script = applescript_display_dialog % (message, title)
  88. _ = run_applescript('display dialog "%s" with title "%s" buttons "Ok" default button "Ok"' % (message, title))
  89.  
  90. def main(args=None):
  91. try:
  92. title = prompt_for_title()
  93. text = get_document_text().decode('utf8')
  94. html = render(text)
  95. post = Post(title, html)
  96. Server().newPost(post)
  97. display_dialog("Success")
  98. except Exception as e:
  99. display_dialog("Failed with error %s" % e)
  100. return 1
  101. return 0
  102.  
  103. if __name__ == '__main__':
  104. sys.exit(main())
Add Comment
Please, Sign In to add comment