Peter's Blog

Redefining the Impossible

Posting to Drupal from VIM revisited.


I was doing some stuff on the Drupal blogapi and noticed that they have changed the interfaces slightly in a way that broke an old favorite posting. The newPost and editPost functions now take a node type as a first argument instead of a blank string.

Here is updated code:

   1  def PostBlog():
   2      import xmlrpclib
   3      import re
   4  
   5      #
   6      # If first line contains a blog entry ID then edit existing post,
   7      # otherwise write a new one.
   8      #
   9      nFirstLine = 0
  10      strID = vim.current.buffer[0]
  11      if not re.match( '^\d+$', strID):
  12          strID = ''
  13      else:
  14          nFirstLine = 1
  15  
  16      strTitle = vim.current.buffer[nFirstLine]
  17      strText = "\n".join( vim.current.buffer[nFirstLine+1:])
  18  
  19      oDrupal = xmlrpclib.ServerProxy( strDrupal + '/xmlrpc.php')
  20  
  21      oPost = { 'title': strTitle,
  22                  'description': strText}
  23  
  24      if strID == '':
  25          strID = oDrupal.metaWeblog.newPost( 'blog', strUserName, strPassword, oPost, 1)
  26      else:
  27          bSuccess = oDrupal.metaWeblog.editPost( strID, strUserName, strPassword, oPost, 1)
  28  
  29      print "Posted entry %s" % strID
  30  
  31      #
  32      # Don't intend to write posts to disk so unmodify the buffer and
  33      # allow easy quit from VIM.
  34      #
  35      vim.command( 'set nomodified')
  36  
  37  def ReadBlog( strID = None):
  38      import xmlrpclib
  39  
  40      oDrupal = xmlrpclib.ServerProxy( strDrupal + '/xmlrpc.php')
  41      if strID:
  42          oBlog = oDrupal.metaWeblog.getPost( strID, strUserName, strPassword)
  43      else:
  44          oBlogs = oDrupal.metaWeblog.getRecentPosts( 'blog', strUserName, strPassword, 1)
  45          oBlog = oBlogs[0]
  46          strID = oBlog['postid']
  47  
  48      vim.current.buffer[:] = []
  49      vim.current.buffer[0] = strID
  50      vim.current.buffer.append( oBlog['title'])
  51      vim.current.buffer.append( '')
  52      for strLine in oBlog['description'].split('\n'):
  53          vim.current.buffer.append( strLine)
Toggle Line Numbers

Instructions here.

If you can read this then it is working again.


Filed under: blogging drupal python vim

2 Comments

Anonymous Says:

over 4 years ago

Hi, I'm just here to test your Captcha module. Feel free to delete this comment.

Peter Says:

over 4 years ago

I don't use the captcha module any more. I have a different plan. I've altered the comment module to reject any comments holding html tags or the letters 'http:'. I figure that comment spam is useless if it cannot post comments with links in. I have no problem with manually editing urls in legitimate comments to make links work, that is a lot less work than deleting comment spam: even the drupal spam module is a pain, you have to select all the spam comments before you can delete them, no 'select all' button.

The captcha mod had weird problems due to the session management in drupal. The good thing about my new scheme is that it does not rely on session management and is totally invisible if you don't try to post links. It only amounts to about 10 lines of code.

I might post the changes someday, only I don't want to give comment spammers any clues. I don't think there is a workaround for them, I've even removed drupals 'homepage' entry in comments in case they try to use that. If you want the code, email me and I might just send it.

Peter

Sorry but comments on this post are now closed.