import markdown from wordpress_xmlrpc import Client, WordPressPost from wordpress_xmlrpc.methods.posts import GetPosts, NewPost from wordpress_xmlrpc.methods import taxonomies import gntp.notifier import os import sys #Connection details for the WordPress xmlrpc connection wpUrl = 'http://http://192.168.50.100:8051/xmlrpc.php' wpUser = 'admin' wpPass = '12' # Hold the server instance. Needed to make the post and add properties wpServer = Client(wpUrl, wpUser, wpPass) # The default category postCategoryList = ['uncategorized'] title = '' postLink = '' uploadCategoryList = [] ## For Hazel filePath = sys.argv[1] growlMSG = "" ## This is the business end. It makes the post to the site. def postToWordPress(title, html, postCategoryList, postLink): post = WordPressPost() post.title = title post.content = html #If there is a post cagtegory, then append it as a term if postCategoryList != '': post.terms_names = { 'category' : postCategoryList } #Prepare custom field for Link List type post if postLink != '': # Just seems safer to start with an empty cutom_field list post.custom_fields = [] post.custom_fields.append({ 'key' : 'linked_list_url', 'value' : postLink }) # The new API requires the post_status and comment_status to be set. # Otherwise the post is added as a draft with comments disabled. post.post_status = 'publish' post.comment_status = 'open' post_id = wpServer.call(NewPost(post)) return post_id # Instantiate my own Growl notification. This is totally unnecessary but nice to have def growlInitialize(): growl = gntp.notifier.GrowlNotifier( applicationName = "Simple DropBlog", notifications = ["New Messages"], defaultNotifications = ["New Messages"]) growl.register() return growl # The body of the script starts here. Hazel passes in the file path. # Read the file and and get the filename to use as a title. # Then get the file contentents. try: myFile = open(filePath, "r") rawText = myFile.read() myFile.close() fileName = os.path.basename(filePath) # Start off using the file name as the post title title = os.path.splitext(fileName)[0] # Handle the odd characters. Just kill them. rawText = rawText.decode('utf-8') # Process with MD Extras and meta data support md = markdown.Markdown(extensions = ['extra', 'meta']) # Get the html text html = md.convert(rawText) ## If post_id exists then already posted. We're done. ## FUTURE: USE POST ID TO UPDATE EDITED POST if 'post_id' not in md.Meta: # extract the title from the meta data if it exists. # If there is no title attribute, keep the file name as the post title if 'title' in md.Meta: title = md.Meta['title'] # title is actualy a list title = title[0] ## extract the categories but keep them as a list # If no categories exist then the default is uncategorized. if 'category' in md.Meta: postCategoryList = md.Meta['category'] # Extracts the url to use as the linked-list url if 'url' in md.Meta: postLink = md.Meta['url'] postLink = postLink[0] # Modify the title to indicate that it is a linked article. title = title + " [Link]" ## Only post if there is a title. If there's no title, then what are you even doing? if title != '': newPost_id = postToWordPress(title, html, postCategoryList, postLink) ## Append the post_id to the beginning of the file #Get the file contents myFile = open(filePath, "r") myFileContent = myFile.read() myFile.close() # Write the post id and append the rest of the text myFile = open(filePath, "w") myFile.write('post_id: ' + newPost_id + '\n' + myFileContent) myFile.close() postGrowl = growlInitialize() postMSG = newPost_id + ": " + title + " Posted to Macdrifter" growlMSG = growlMSG + "\n"+ postMSG postGrowl.notify( noteType = "New Messages", title = growlMSG, description = "The Blog Post was successful", icon = None, sticky = False, priority = 1) except Exception, e: postGrowl = growlInitialize() postMSG = "Post Failed!" postGrowl.notify( noteType = "New Messages", title = postMSG, description = e, icon = None, sticky = False, priority = 2)