在先前的 CMSiMDE 架構中, 曾經設法讓 Pelican 與 Wordpress 的內容同步, 相同的概念, 也可以在 Leo Editor 中, 讓 Pelican 的網誌文章與 Google Blogger 保持同步.
按鈕與節點標題
Leo Editor 中可以設置按鈕執行 Python 程式, 其中搭配節點的標題內容存取, 可以應用在 Pelican 與 Blogger 的網誌內容同步.
由於目前使用的 Pelican, 在 markdown 目錄中編寫 .md 檔案, 然後再設法以 Pelican 指令與設定檔, 將所有的 .md 檔案轉為 blog 目錄中的網誌內容. 其中, 若能將個別的 .md 檔案先轉為 html 後, 再利用 Google Blogger API 的 Python 程式將各網誌 html 檔案送至對應帳號下的 Blogger 網誌系統, 將可以將一份內容分別同步到 Pelican 與 Blogger.
新增 Blogger 文章
add_to_blogger 按鈕程式:
from markdown import markdown
from oauth2client import client
from googleapiclient import sample_tools
import os
argv = ""
# 認證並建立服務
# name of the api is "blogger", version is "v3"
# description of the api is __doc__
# file name of the application: location of client_secrets.json
service, flags = sample_tools.init(
argv, 'blogger', 'v3', __doc__, "./../../client_secrets.json",
scope='https://www.googleapis.com/auth/blogger')
def get_cat_tag_content(data):
# 請注意, 因為 data 來自 .md 的檔案 內容, 第1行為 ---
# 用跳行符號分割
data_list = data.split("\n")
#第 2 行為 title
title= data_list[1]
#第 4 行為 category
category = data_list[3]
#第 5 行為 tags
tags = data_list[4]
# 有多項資料的 content 型別為數列
# 再將第 9 行之後的資料數列串回成以跳行隔開的資料
content = "\n".join(data_list[8:])
# 先將截斷摘要與內文的 pelican md 檔按符號, 換成 Blogger 的
# 但是只換第一個
content = content.replace('', '', 1)
# 接著若內容有 ~~~python 與 ~~~ 則換成 Wordpress 格式
#content = content.replace('~~~python', '[code lang="python"]')
#content = content.replace('~~~', '[/code]')
return title, category, tags, content
# 從目前所在節點的 body pan 中取出類別, tags 以及文章內容
# p.h 為 @clean filename.md
# 因為要使用 @clean 節點掛上為後的 blogger post_id, 因此改為讀 .md 檔案
md_filename = p.h.split(" ")[1]
with open(md_filename, 'r', encoding="utf-8") as content_file:
md_content = content_file.read()
# title_str, category_str, tags_str, content = get_cat_tag_content(p.b)
title_str, category_str, tags_str, content = get_cat_tag_content(md_content)
category = category_str.split(":")[1]
tags = tags_str.split(":")[1].split(",")
tags.append(category)
# title 是一個單獨的字串
title = title_str.split(":")[1]
# 將 markdown 格式 content 轉為 html
content = markdown(content)
# 以下處理 content 的 <h2>標題
content = content.replace("<h2>", "<h2><font size='4'>")
content = content.replace("</h2>", "</font></h2>")
# g.es(content)
try:
'''
users = service.users()
# 取得使用者 profile 資料
user = users.get(userId='self').execute()
print('網誌名稱: %s' % user['displayName'])
'''
blogs = service.blogs()
# 取得使用者所建立網誌名稱
blogs = blogs.listByUser(userId='self').execute()
# post_id is now blogs["items"][0]["id"]
blog_id = blogs["items"][0]["id"]
#for blog in blogs['items']:
#print(blog['name'], blog['url'])
posts = service.posts()
# 新增網誌 post 時, 需要 post_id
body = {
"kind": "blogger#post",
"id": blog_id,
"title": title,
# 利用 markdown 函式, 將 .md 的內文轉為 html, 作為 Blogger 的文章內容
"content": content,
"labels": tags
}
insert = posts.insert(blogId=blog_id, body=body)
posts_doc = insert.execute()
post_id = posts_doc["id"]
#print(posts_doc)
os.remove("blogger.dat")
# 利用最後的 child 節點來儲存 post_id
to_save_post_id = p.insertAsLastChild()
# 改為內文為空的節點, id 直接標在 head 標題
to_save_post_id.b = ""
to_save_post_id.h = post_id
# 因為新增節點, commander 必須 redraw
c.redraw()
g.es("post_id 為", post_id)
g.es("已經將資料送往 Blogger!")
except(client.AccessTokenRefreshError):
g.es("error")
編輯 Blogger 文章
edit_to_blogger 按鈕程式:
from markdown import markdown
from oauth2client import client
from googleapiclient import sample_tools
import os
argv = ""
# 認證並建立服務
# name of the api is "blogger", version is "v3"
# description of the api is __doc__
# file name of the application: location of client_secrets.json
service, flags = sample_tools.init(
argv, 'blogger', 'v3', __doc__, "./../../client_secrets.json",
scope='https://www.googleapis.com/auth/blogger')
def get_cat_tag_content(data):
# 請注意, 因為 data 來自 .md 的檔案 內容, 第1行為 ---
# 用跳行符號分割
data_list = data.split("\n")
#第 2 行為 title
title= data_list[1]
#第 4 行為 category
category = data_list[3]
#第 5 行為 tags
tags = data_list[4]
# 有多項資料的 content 型別為數列
# 再將第 9 行之後的資料數列串回成以跳行隔開的資料
content = "\n".join(data_list[8:])
# 先將截斷摘要與內文的 pelican md 檔按符號, 換成 Blogger 的
content = content.replace('', '')
# 接著若內容有 ~~~python 與 ~~~ 則換成 Wordpress 格式
#content = content.replace('~~~python', '[code lang="python"]')
#content = content.replace('~~~', '[/code]')
return title, category, tags, content
# 從目前所在節點的 body pan 中取出類別, tags 以及文章內容
# p.h 為 @clean filename.md
# 因為要使用 @clean 節點掛上為後的 blogger post_id, 因此改為讀 .md 檔案
md_filename = p.h.split(" ")[1]
with open(md_filename, 'r', encoding="utf-8") as content_file:
md_content = content_file.read()
# title_str, category_str, tags_str, content = get_cat_tag_content(p.b)
title_str, category_str, tags_str, content = get_cat_tag_content(md_content)
category = category_str.split(":")[1]
tags = tags_str.split(":")[1].split(",")
tags.append(category)
# title 是一個單獨的字串
title = title_str.split(":")[1]
# 將 markdown 格式 content 轉為 html
content = markdown(content)
# 以下處理 content 的 <h2>標題
content = content.replace("<h2>", "<h2><font size='4'>")
content = content.replace("</h2>", "</font></h2>")
# g.es(content)
try:
blogs = service.blogs()
# 取得使用者所建立網誌名稱
blogs = blogs.listByUser(userId='self').execute()
blog_id = blogs["items"][0]["id"]
# 設法取得原 post 的 id
postid_outline = p.getLastChild()
# 直接從標題取得 post 的 id 號碼
post_id = postid_outline.h
posts = service.posts()
# 更新網誌文章時的 body
body = {
"kind": "blogger#post",
"title": title,
"content": content
}
# need to save postId to outline head
update = posts.update(blogId=blog_id, postId=post_id, body=body, publish=True)
update_doc = update.execute()
os.remove("blogger.dat")
g.es("post_id 為", post_id)
g.es("已經將更新資料送往 Blogger!")
except(client.AccessTokenRefreshError):
g.es("error")
從 Blogger 取回內容
在 Pelican 與 Wordpress 的內容同步 中, 可以從 Wordpress 取回網誌內容, 然後新增到 Pelican, 在此因為網誌文章的建立以 CMSiMDE 倉儲中的 Pelican 網誌為主, Blogger 只是附屬備份網誌, 所以就不再從新增的 Google Blogger 取回網誌文章.
No comments:
Post a Comment