My Email: yeshengzou@gmail.com

<我的博客解决方案>中,最后有一些待解决的问题,昨天看了下Google Date API,算是解决了(主要是插图和标签的问题)。

标签问题

标签使用Blogger Data API是比较好搞定的,本身就有一个AddLabel函数。
import gdata.blogger.service
import atom

def postBlog(subject=None, body=None, labels=None, account=None,
           password=None):
  'to post blog via GData Blogger API'

  print 'To Post ...'

  client = gdata.blogger.service.BloggerService()
  client.ssl = True

  post = gdata.blogger.BlogPostEntry()
  post.title = atom.Title(text = subject)
  post.content = atom.Content(content_type = 'html', text = body)
  #post.AddLabel('Computer')
  if labels[0]:
      map(post.AddLabel, labels)

  try:
      client.ClientLogin(account, password)
  except gdata.service.BadAuthentication:
      print account, password
      print "ERROR: Login failed!!. (Wrong username/password?)"
      return False

  try:
      BLOG_ID = client.Get('/feeds/default/blogs').entry[0].
                GetSelfLink().href.split('/')[-1]
  except:
      print "ERROR: Can not get the BLOG_ID!!"
      return False

  try:
      client.AddPost(post, blog_id = BLOG_ID)
  except:
      print "ERROR: Can not add the blog!!"
      print "Is the format all right?"
      return False

  print "Blog `%s` post successfully." % subject2

注意,使用Blogger的API时,一定要使用安全链接(ssl = True),否则是接不上的。传说与GFW有关,昏!

插图问题

插图的问题是这样解决的:

  1. 通过正则表达式,从正文中获取所有插图的本地地址。
  2. 把这些图片通过Picasa Web Albums Data API上传到我的Picasa中指定的相册。
  3. 获取上传后,在Picasa中的这些图片的URL。
  4. 把正文中,所有图片的本地地址替换成Picasa中的URL。

-

import gdata.photos.service
import gdata.media
import gdata.geo

def uploadImages(filelist, account, password):
  '''Upload all images.
    And thd filelist is the re.findall() in blog.'''

  print "Upload All Images ..."

  try:
      gd_client = gdata.photos.service.PhotosService()
      gd_client.email = account
      gd_client.password = password
      gd_client.source = 'zys-blogimage-1'
      gd_client.ProgrammaticLogin()
  except:
      print 'ERROR: Could not Login in Picasa!!'
      return False

  album = gd_client.GetUserFeed().entry[1]
  album_url = '/data/feed/api/user/%s/albumid/%s' % (account,
              album.gphoto_id.text)

  replaceList = []

  for f in filelist:
      print "Uploading `%s` ..." % f[1]
      try:
          photo = gd_client.InsertPhotoSimple(album_url,
                  getFileName(), f[0], f[1],
                                              content_type='image/j
                                                             peg')
      except:
          print 'ERROR: upload %s failed!!' % f[1]
          return False

      replaceList.append((f[1], photo.GetMediaURL()))
      print "Completed. --> `%s`" % photo.GetMediaURL()

  print "All images have Uploaded."
  return replaceList

2 Comments:

  1. abc123 said...
    风格杂发生了那么大的变化呢?
    ffan said...
    我实在受不了以前那个主题,在22寸宽屏显示器下的样子。同时在IE下也有小小的错差,虽然我在自己的14寸+firefox下看着还好。
    所以,现在的目标是无图+自适应宽度(液态宽度)。

Post a Comment



较新的博文 较早的博文 主页