2011年3月18日 星期五

Blogger Search and Replace

Well, I've transfer my blog couple times,
so it's has some image link from other static web hosting,
but someday, I got a mail that told me the service has expired,
because my adsl account has expired long time ago.

So, I thought, maybe I can move all file to other web hosting,
and replace all string in blogger.
then I try to find a solution or any way to replace a string in all article,
but I can't after I google it.

So I try to wrote it.

refer to Blogger API Developer's Guide: Python

Source:
def ReciveAll(self):
    # Request the feed.
    query = gdata.blogger.client.Query(max_results=9999)    
    feed = self.client.get_posts(self.blog_id ,query=query)
    return feed

  def SearchAll(self,str):
      result = []
      feed = self.ReciveAll()

      for entry in feed.entry:
        if (string.find(entry.content.text, str) != -1):
          result.append(entry)
      return result

  def UpdatePost(self, entry_to_update):
    return self.client.update(entry_to_update)

def main():
    global email
    global password

    """The main function runs the BloggerExample application.   
    NOTE:  It is recommended that you run this sample using a test account.
    """

    email = easygui.enterbox('Email:','Login')
    password = easygui.passwordbox('Password:','Login')
    sample = BloggerExample()
    while 1:
      array = []
      menu_array = ['Search','Exit','Search and replace']
      menu_choice = easygui.choicebox('Main menu', 'Main menu', menu_array)
      #select = raw_input("1:Search\n2:Search and replace\n3:exit\n")
      if menu_choice == 'Search':
        #user_input = raw_input("Search:").decode(sys.stdin.encoding)
        user_input = easygui.enterbox('Search:','Search')
        
        results = sample.SearchAll(user_input)
        for entry in results:
          array.append(entry.title.text.encode("utf8"))

        entry_choice = easygui.choicebox("test","test",array)
        while(entry_choice!= None):
          re_split=re.compile("[<>]")
          for entry in results:
            if entry.title.text == entry_choice:
              line_array = re_split.split(entry.content.text.encode("utf8"))
              line_array_contain = []
              for line in line_array:
                if line.find(user_input.encode("utf8")) != -1:
                  line_array_contain.append(line)
              easygui.choicebox("Search Result","Search",line_array_contain)
              break
          entry_choice = easygui.choicebox("test","test",array)

      elif menu_choice == 'Search and replace':
        #src = raw_input("Search:").decode(sys.stdin.encoding)
        src = easygui.enterbox('Search:','Search and Replace')
        #dest = raw_input("Replace:").decode(sys.stdin.encoding)
        dest = easygui.enterbox('Replace:','Search and Replace')
        if easygui.ccbox():
          results = sample.SearchAll(src) 
          for entry in results:
            entry.content.text = entry.content.text.replace(src,dest)
            #print entry.content.text
            sample.UpdatePost(entry)
            #print entry.title.text + "-> done"
            easygui.msgbox(entry.title.text + "-> done")

      elif menu_choice == 'Exit':
        break
    
if __name__ == '__main__':
  main()

GUI Version:
Source: http://dl.dropbox.com/u/3357679/Blog/Src_Blogger_search_replace_gui.7z
Prebuilt binary: http://dl.dropbox.com/u/3357679/Blog/Blogger_search_replace_gui.7z

Console Version:
Source: http://dl.dropbox.com/u/3357679/Blog/Src_Blogger_search_replace.7z
Prebuilt binary: http://dl.dropbox.com/u/3357679/Blog/Blogger_search_replace.7z

because that's practice program(it's mean beta release, use it at your own risk!)
please backup or export the blog before.
I've try replace in English and Traditional Chinese,
it's work, so, it's should support multi language.

Finally, it's worked!
but I found that maybe I need modify article by manually,
because it's look like upload image to Picasa will be much better?

2 則留言:

Rae 提到...

Well, I've "transferred" my blog couple of times.
And "it has" some links from other static web hosting. But one day I got an email that told me the service has expired because my adsl account has expired long time ago.

"I've tried to" replace in English and Traditional Chinese.
"It worked", so "it should" support multi language.

Finally, "it worked!"
but I found that maybe I need "to" modify article manually, because it "looks" like that "uploading" image to Picasa is much better?

一些文法上的小建議 :)
這篇留言可以不用審核,您看完即可以刪除了!

Mosquito 提到...

沒關係 就留著
反正我文法差也不是一天兩天的事情啦XD