|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from werobot import WeRoBot |
| 3 | +import os |
| 4 | +import json |
| 5 | +import requests |
| 6 | +from selenium import webdriver |
| 7 | + |
| 8 | + |
| 9 | +class WebDriver: |
| 10 | + def __init__(self, driver_path): |
| 11 | + self.brower = webdriver.Chrome(executable_path=driver_path) |
| 12 | + website = self.brower.get("https://whaoa.github.io/markdown-nice/") |
| 13 | + page_source = self.brower.page_source |
| 14 | + title = self.brower.title |
| 15 | + # print(website) |
| 16 | + print(page_source) |
| 17 | + print(title) |
| 18 | + |
| 19 | + |
| 20 | +class WeRoBotClint: |
| 21 | + |
| 22 | + def __init__(self): |
| 23 | + robot = WeRoBot() |
| 24 | + robot.config["APP_ID"] = os.getenv("wx2dfe81f38281cfd9") |
| 25 | + robot.config["APP_SECRET"] = os.getenv("ef5e460dd2941f640aa6eaf69b97d1c4") |
| 26 | + self.we_client = robot.client |
| 27 | + self.we_token = self.we_client.grant_token() |
| 28 | + |
| 29 | + def upload_media(self, media_file): |
| 30 | + res_json = self.we_client.upload_permanent_media("image", open(media_file, "rb")) |
| 31 | + media_id = res_json["media_id"] |
| 32 | + url = res_json["url"] |
| 33 | + return media_id |
| 34 | + |
| 35 | + # 因werobot暂未支持,所以直接采用接口访问 |
| 36 | + def upload_article(self, content): |
| 37 | + articles = { |
| 38 | + "articles": [ |
| 39 | + { |
| 40 | + "thumb_media_id": "JqgEfa5SNzBYVoHTccyIRsGDDhotpIMnUnwCL2L6pp_JfvIA3qdDBc9kYJmlzGi4", |
| 41 | + "author": "zhangferry", |
| 42 | + "title": "Happy Day", |
| 43 | + "content_source_url": "https://zhangferry.com/2022/07/24/github_action_for_blog_deploy/", |
| 44 | + "content": content, |
| 45 | + # 图文信息描述 |
| 46 | + "digest": "描述信息", |
| 47 | + "need_open_comment": 1 |
| 48 | + } |
| 49 | + ] |
| 50 | + } |
| 51 | + |
| 52 | + client = NetworkClint() |
| 53 | + token = client.get_access_token() |
| 54 | + headers = {'Content-type': 'text/plain; charset=utf-8'} |
| 55 | + datas = json.dumps(articles, ensure_ascii=False).encode('utf-8') |
| 56 | + postUrl = "https://api.weixin.qq.com/cgi-bin/draft/add?access_token=%s" % token |
| 57 | + |
| 58 | + r = requests.post(postUrl, data=datas, headers=headers) |
| 59 | + resp = json.loads(r.text) |
| 60 | + print(resp) |
| 61 | + |
| 62 | + |
| 63 | +class NetworkClint: |
| 64 | + def __init__(self): |
| 65 | + self.__accessToken = "" |
| 66 | + self.__leftTime = 0 |
| 67 | + |
| 68 | + def __real_get_access_token(self): |
| 69 | + post_url = ("https://api.weixin.qq.com/cgi-bin/token?grant_type=" |
| 70 | + "client_credential&appid=%s&secret=%s" % ( |
| 71 | + os.getenv('WECHAT_APP_ID'), os.getenv('WECHAT_APP_SECRET'))) |
| 72 | + print(post_url) |
| 73 | + response = requests.request(method="post", url=post_url) |
| 74 | + response_json = json.loads(response.text) |
| 75 | + |
| 76 | + self.__accessToken = response_json["access_token"] |
| 77 | + self.__leftTime = response_json["expires_in"] |
| 78 | + |
| 79 | + def get_access_token(self): |
| 80 | + if self.__leftTime < 10: |
| 81 | + self.__real_get_access_token() |
| 82 | + return self.__accessToken |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == '__main__': |
| 86 | + local_path = "/Users/zhangferry/Desktop/wechat_content" |
| 87 | + local_img_path = "/Users/zhangferry/Desktop/WechatIMG9.jpeg" |
| 88 | + driver_path = "/Users/zhangferry/Developer/chromedriver" |
| 89 | + |
| 90 | + driver = WebDriver(driver_path) |
| 91 | + |
| 92 | + # os.environ['WECHAT_APP_ID'] = "" |
| 93 | + # os.environ['WECHAT_APP_SECRET'] = "" |
| 94 | + # with open(local_path, "r") as fp: |
| 95 | + # file_content = fp.read() |
| 96 | + # print(file_content) |
| 97 | + # |
| 98 | + # clinet = WeRoBotClint() |
| 99 | + # # res = clinet.upload_media(local_img_path) |
| 100 | + # # print(res) |
| 101 | + # clinet.upload_article(file_content) |
| 102 | + |
| 103 | + |
| 104 | +temp_token = "59_FzaJlr22Qak-Sd4XNnXGjUprHTp7xaDQV_RDv8BRpal_-TyEPyaYUlWNpLHAvJxYpZoPqtpMb-1nMzV6ZuJDVFi8QwMYJz88SKE86U-SNX_wDPiOpwaZ6D8O9UXOuR0aEjYh3rIBk4NDe8LVNFIaAGADVY" |
0 commit comments