@language
python
from
markdown
import
markdown
from
oauth2client
import
client
import
os
import
pickle
from
googleapiclient.discovery
import
build
os.environ[
'TZ'
]
=
'Asia/Taipei'
with
open
(
'./../../blogger_credentials.dat'
,
'rb'
) as credentials_dat:
credentials
=
pickle.load(credentials_dat)
service
=
build(
'blogger'
,
'v3'
, credentials
=
credentials)
def
get_cat_tag_content(data):
data_list
=
data.split(
"\n"
)
title
=
data_list[
1
]
category
=
data_list[
3
]
tags
=
data_list[
4
]
content
=
"\n"
.join(data_list[
8
:])
content
=
content.replace(
'<!-- PELICAN_END_SUMMARY -->'
,
'<!--more-->'
)
return
title, category, tags, content
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(md_content)
category
=
category_str.split(
":"
)[
1
]
tags
=
tags_str.split(
":"
)[
1
].split(
","
)
tags.append(category)
title
=
title_str.split(
":"
)[
1
]
content
=
markdown(content)
content
=
content.replace(
"<h2>"
,
"<h2><font size='4'>"
)
content
=
content.replace(
"</h2>"
,
"</font></h2>"
)
try
:
blogs
=
service.blogs()
blogs
=
blogs.listByUser(userId
=
'self'
).execute()
blog_id
=
blogs[
"items"
][
0
][
"id"
]
postid_outline
=
p.getLastChild()
post_id
=
postid_outline.h
posts
=
service.posts()
body
=
{
"kind"
:
"blogger#post"
,
"title"
: title,
"content"
: content
}
update
=
posts.update(blogId
=
blog_id, postId
=
post_id, body
=
body, publish
=
True
)
update_doc
=
update.execute()
g.es(
"post_id 為"
, post_id)
g.es(
"已經將更新資料送往 Blogger!"
)
except
(client.AccessTokenRefreshError):
g.es(
"error"
)