微信公众号上传素材
#weixin_wechat/upload_news.py
"""
素材
"""
from werobot import WeRoBot
from datetime import datetime ,timedelta
import re,time
import upload_media as handle_media
import xinwenlianbo.get_cctv_xinwenlianbo as cctv
robot = WeRoBot()
robot.config["APP_ID"] = "xxx"
robot.config["APP_SECRET"] = "xxx"
client = robot.client
token = client.grant_token()
print(token)
print('*'*30)
today = datetime.today()
today_weekday_name = today.strftime('%A')
yesterday = datetime.today() - timedelta(days=1)
string_yesterday = yesterday.strftime('%Y%m%d')
def get_content_html(href_list):
content = '<p></p>'
for index,href in enumerate(href_list):
if index > 0: ## 跳过第一列目录数据
part_content = get_part_html(index,href)
content = content+part_content
return content
def get_part_html(index,href):
part_content = '<p></p>'
href_title,href_content,href_source,href_pic = cctv.handle_href_html(index,href)
href_pic = 'https://'+href_pic
img_file_name = 'img_media.jpg'
media_id,media_url,media_json = handle_media.get_img_media_id(href_pic,img_file_name)
print(media_url)
img_html = '<p align="center"> <img src="{url}" alt="图片"></img></p>'.format(url=media_url)
part_content= href_title + img_html + href_content
return part_content
if __name__ == "__main__":
start_time = time.time() # 开始时间
"""
页面获取
"""
string_date = string_yesterday
url = cctv.get_url(string_date)
response_html = cctv.get_html_loop(url)
print('*'*30)
title_list, items_href= cctv.get_content(response_html)
title_toc_html_backup = cctv.handle_toc(title_list)
title_toc_html = cctv.handle_href_toc(href_toc=items_href[0])
"""
页面目录
"""
print(title_toc_html)
#print(title_toc_html_backup)
"""
内容标题、图片、内容
"""
items_href = items_href
content = get_content_html(items_href)
"""
上传到微信公众号素材
"""
print('*'*30)
TITLE = '{dt}今日新闻联播主要内容文字版完整版'.format(dt=yesterday.strftime('%Y年%m月%d日'))
THUMB_MEDIA_ID = 'yJCW-Z_VZwPZtLOTqcSltJ9MsAdoS5YKLPFxdkrd15A'
AUTHOR = '坚持公考'
show_cover_pic = 1
CONTENT = title_toc_html + content
CONTENT_SOURCE_URL = 'https://tv.cctv.com/lm/xwlb/index.shtml'
articles = [{
"title": TITLE,
"thumb_media_id": THUMB_MEDIA_ID,
"author": AUTHOR,
"digest": '',
"show_cover_pic": 1,
"content": CONTENT,
"content_source_url": CONTENT_SOURCE_URL
}
# 若新增的是多图文素材,则此处应有几段articles结构,最多8段
]
news_json = client.add_news(articles)
print(news_json)
#更新素材
update_data = {
"media_id":'yJCW-Z_VZwPZtLOTqcSltHoDkGrIrBh_IfJRvO_Krds',
"index":0,
"articles": {
"title": TITLE,
"thumb_media_id": THUMB_MEDIA_ID,
"author": AUTHOR,
"digest": '',
"show_cover_pic": 1,
"content": CONTENT,
"content_source_url": CONTENT_SOURCE_URL
}
}
#client.update_news(update_data)
#print(title_toc_html)
end_time = time.time() #结束时间
print("程序耗时%f秒." % (end_time - start_time))