Skip to content

Commit

Permalink
添加启动脚本
Browse files Browse the repository at this point in the history
  • Loading branch information
wayne committed May 21, 2024
1 parent c4302d5 commit fa862a7
Show file tree
Hide file tree
Showing 20 changed files with 164 additions and 105 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.pytest_cache/
.idea/
__pycache__/
last_published.txt
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
只需要编写好Markdown格式的博客即可,同时能够根据各平台的规则自动调整格式,确保内容在不同平台上的展示效果一致。

# 更新列表
* 2024-05-21 现在自动发布支持从博客文件夹选择要发布的博客了,不需要一条条来设置在配置文件里面了。
* 2024-05-20 添加了自动设置环境脚本,自动运行发布脚本。这样就不用打开python开发工具,通过运行publish.bat 或者publish.sh即可自动发布啦。
* 2024-05-19 增加对公众号平台的支持
* 2024-05-18 知乎平台优化
Expand Down
3 changes: 3 additions & 0 deletions config/common.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ driver_type: "chrome"

# 是否包含自定义footer
include_footer: true
# 要发布的文章目录
content_dir : /Users/wayne/Downloads/blogthings/blogs/blog/projects/

# 文章的标题
title: 一键自动化博客发布工具,用过的人都说好(腾讯云篇)
# 文章的内容
Expand Down
6 changes: 0 additions & 6 deletions open.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,4 @@ else
echo "venv folder does not exist. Not activating..."
fi

#Set STARTUP_CMD as normal python if not specified
if [[ -z "$STARTUP_CMD" ]]
then
STARTUP_CMD=python
fi

python open_all.py
6 changes: 0 additions & 6 deletions publish.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,4 @@ else
echo "venv folder does not exist. Not activating..."
fi

#Set STARTUP_CMD as normal python if not specified
if [[ -z "$STARTUP_CMD" ]]
then
STARTUP_CMD=python
fi

python publish_all.py
137 changes: 82 additions & 55 deletions publish_all.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os.path
import traceback

import selenium
Expand All @@ -16,10 +17,15 @@
from publisher.jianshu_publisher import jianshu_publisher
from publisher.juejin_publisher import juejin_publisher
from publisher.mpweixin_publisher import mpweixin_publisher
from utils.file_utils import list_all_files, list_files, write_to_file, read_head
from utils.yaml_file_utils import read_common

last_published_file_name = 'last_published.txt'

common_config = read_common()
driver_type= common_config['driver_type']
driver_type = common_config['driver_type']
content_dir = common_config['content_dir']

if driver_type == 'chrome':
# 启动浏览器驱动服务
service = selenium.webdriver.chrome.service.Service(common_config['service_location'])
Expand Down Expand Up @@ -58,97 +64,118 @@
'mpweixin']


def publish_to_platform(platform, driver):
def publish_to_platform(platform, driver, content=None):
"""
发布到指定平台的封装函数
"""
try:
globals()[platform + '_publisher'](driver) # 动态调用对应平台的发布函数
globals()[platform + '_publisher'](driver, content) # 动态调用对应平台的发布函数
except Exception as e:
print(platform,"got error")
print(platform, "got error")
traceback.print_exc() # 打印完整的异常跟踪信息
print(e)

if content:
save_last_published_file_name(os.path.basename(content))


def publish_to_all_platforms(driver):
def publish_to_all_platforms(driver, content=None):
"""
发布到所有平台的封装函数
"""
for platform in all_sites:
if platform in common_config['enable'] and common_config['enable'][platform]:
publish_to_platform(platform, driver)
publish_to_platform(platform, driver, content)
# 在需要的时候关闭浏览器,不要关闭浏览器进程
driver.quit()


def save_last_published_file_name(filename):
write_to_file(filename, last_published_file_name)


if __name__ == '__main__':
while True:
print("选择你要发布的平台:\n")
print("1. 全部")
print("2. csdn")
print("3. jianshu")
print("4. juejin")
print("5. segmentfault")
print("6. oschina")
print("7. cnblogs")
print("8. zhihu")
print("9. cto51")
print("10. infoq")
print("11. txcloud")
print("12. alicloud")
print("13. toutiao")
print("14. mpweixin")
print("0. 退出")

choice = input("\n请选择: ")
# 选择要发布的内容
print("选择你要发布的博客,输入序号:")
# file_list = list_all_files(content_dir, ".md")
file_list = list_files(content_dir, ".md")
for index, file_name in enumerate(file_list):
print(str(index) + ":" + os.path.basename(file_name))
print("上次发布的博客是: " + read_head(last_published_file_name))
file_choice = input("\n请选择: ")
print("")

if choice == '1':
publish_to_all_platforms(driver)
if len(file_list) > int(file_choice) >= 0:
file_path = file_list[int(file_choice)]
print("你要发布的文章是:", file_path)
while True:
print("选择你要发布的平台:\n")
print("1. 全部")
print("2. csdn")
print("3. jianshu")
print("4. juejin")
print("5. segmentfault")
print("6. oschina")
print("7. cnblogs")
print("8. zhihu")
print("9. cto51")
print("10. infoq")
print("11. txcloud")
print("12. alicloud")
print("13. toutiao")
print("14. mpweixin")
print("0. 退出")

choice = input("\n请选择: ")
print("")

if choice == '1':
publish_to_all_platforms(driver, file_path)

elif choice == '2':
publish_to_platform('csdn', driver)
elif choice == '2':
publish_to_platform('csdn', driver, file_path)

elif choice == '3':
publish_to_platform('jianshu', driver)
elif choice == '3':
publish_to_platform('jianshu', driver, file_path)

elif choice == '4':
publish_to_platform('juejin', driver)
elif choice == '4':
publish_to_platform('juejin', driver, file_path)

elif choice == '5':
publish_to_platform('segmentfault', driver)
elif choice == '5':
publish_to_platform('segmentfault', driver, file_path)

elif choice == '6':
publish_to_platform('oschina', driver)
elif choice == '6':
publish_to_platform('oschina', driver, file_path)

elif choice == '7':
publish_to_platform('cnblogs', driver)
elif choice == '7':
publish_to_platform('cnblogs', driver, file_path)

elif choice == '8':
publish_to_platform('zhihu', driver)
elif choice == '8':
publish_to_platform('zhihu', driver, file_path)

elif choice == '9':
publish_to_platform('cto51', driver)
elif choice == '9':
publish_to_platform('cto51', driver, file_path)

elif choice == '10':
publish_to_platform('infoq', driver)
elif choice == '10':
publish_to_platform('infoq', driver, file_path)

elif choice == '11':
publish_to_platform('txcloud', driver)
elif choice == '11':
publish_to_platform('txcloud', driver, file_path)

elif choice == '12':
publish_to_platform('alicloud', driver)
elif choice == '12':
publish_to_platform('alicloud', driver, file_path)

elif choice == '13':
publish_to_platform('toutiao', driver)
elif choice == '13':
publish_to_platform('toutiao', driver, file_path)

elif choice == '14':
publish_to_platform('mpweixin', driver)
elif choice == '14':
publish_to_platform('mpweixin', driver, file_path)

elif choice == '0':
break
elif choice == '0':
break

else:
print("无效的输入,请重新输入。")
else:
print("无效的输入,请重新输入。")
print("无效的选择")
8 changes: 5 additions & 3 deletions publisher/alicloud_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
import time


def alicloud_publisher(driver):
def alicloud_publisher(driver, content=None):
alicloud_config = read_alcloud()
common_config = read_common()
if content:
common_config['content'] = content
auto_publish = common_config['auto_publish']
# 提取markdown文档的front matter内容:
front_matter = parse_front_matter(common_config['content'])
Expand All @@ -33,7 +35,7 @@ def alicloud_publisher(driver):
# 文章标题
title = driver.find_element(By.XPATH, '//input[@placeholder="请填写标题"]')
title.clear()
if 'title' in front_matter['title'] and front_matter['title']:
if 'title' in front_matter and front_matter['title']:
title.send_keys(front_matter['title'])
else:
title.send_keys(common_config['title'])
Expand All @@ -46,7 +48,7 @@ def alicloud_publisher(driver):
time.sleep(3) # 等待3秒

# 摘要
if 'description' in front_matter['description'] and front_matter['description']:
if 'description' in front_matter and front_matter['description']:
summary = front_matter['description']
else:
summary = common_config['summary']
Expand Down
9 changes: 6 additions & 3 deletions publisher/cnblogs_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
import time


def cnblogs_publisher(driver):
def cnblogs_publisher(driver, content=None):
cnblogs_config = read_cnblogs()
common_config = read_common()
if content:
common_config['content'] = content
print("content is :", common_config['content'])
auto_publish = common_config['auto_publish']
# 提取markdown文档的front matter内容:
front_matter = parse_front_matter(common_config['content'])
Expand All @@ -32,7 +35,7 @@ def cnblogs_publisher(driver):
# 文章标题
title = driver.find_element(By.ID, 'post-title')
title.clear()
if 'title' in front_matter['title'] and front_matter['title']:
if 'title' in front_matter and front_matter['title']:
title.send_keys(front_matter['title'])
else:
title.send_keys(common_config['title'])
Expand Down Expand Up @@ -95,7 +98,7 @@ def cnblogs_publisher(driver):
time.sleep(2)

# 摘要
if 'description' in front_matter['description'] and front_matter['description']:
if 'description' in front_matter and front_matter['description']:
summary = front_matter['description']
else:
summary = common_config['summary']
Expand Down
10 changes: 7 additions & 3 deletions publisher/csdn_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@
import time


def csdn_publisher(driver):
def csdn_publisher(driver, content=None):
csdn_config = read_csdn()
common_config = read_common()
if content:
common_config['content'] = content
# print("content is :", common_config['content'])
auto_publish = common_config['auto_publish']
# 提取markdown文档的front matter内容:
front_matter = parse_front_matter(common_config['content'])
# print("front_matter is :", front_matter)

# 打开新标签页并切换到新标签页
driver.switch_to.new_window('tab')
Expand All @@ -34,7 +38,7 @@ def csdn_publisher(driver):
# 文章标题
title = driver.find_element(By.XPATH, '//div[contains(@class,"article-bar")]//input[contains(@placeholder,"请输入文章标题")]')
title.clear()
if 'title' in front_matter['title'] and front_matter['title']:
if 'title' in front_matter and front_matter['title']:
title.send_keys(front_matter['title'])
else:
title.send_keys(common_config['title'])
Expand Down Expand Up @@ -89,7 +93,7 @@ def csdn_publisher(driver):
time.sleep(2)

# 摘要
if 'description' in front_matter['description'] and front_matter['description']:
if 'description' in front_matter and front_matter['description']:
summary = front_matter['description']
else:
summary = common_config['summary']
Expand Down
8 changes: 5 additions & 3 deletions publisher/cto51_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
import time


def cto51_publisher(driver):
def cto51_publisher(driver,content=None):
cto51_config = read_51cto()
common_config = read_common()
if content:
common_config['content'] = content
auto_publish = common_config['auto_publish']
# 提取markdown文档的front matter内容:
front_matter = parse_front_matter(common_config['content'])
Expand All @@ -34,7 +36,7 @@ def cto51_publisher(driver):
# 文章标题
title = driver.find_element(By.ID, 'title')
title.clear()
if 'title' in front_matter['title'] and front_matter['title']:
if 'title' in front_matter and front_matter['title']:
title.send_keys(front_matter['title'])
else:
title.send_keys(common_config['title'])
Expand Down Expand Up @@ -90,7 +92,7 @@ def cto51_publisher(driver):
tag_input.send_keys(Keys.ENTER)

# 摘要
if 'description' in front_matter['description'] and front_matter['description']:
if 'description' in front_matter and front_matter['description']:
summary = front_matter['description']
else:
summary = common_config['summary']
Expand Down
Loading

0 comments on commit fa862a7

Please sign in to comment.