2 |
3 | # WeiboBot
4 |
5 | _基于微博H5 API开发的机器人框架_
6 |
7 |

8 |

9 |

10 |
11 |

12 |
13 |
14 |
15 |
16 |
17 | WeiboBot 是一个基于微博H5 API开发的机器人框架,提供了一个简单的接口,可以让你的机器人更加简单的接入微博,并且提供了一些简单的指令,比如:转评赞,回复消息等
18 |
19 | ## 安装
20 |
21 | `pip install WeiboBot`
22 |
23 | ## 开始使用(事件驱动模式)
24 |
25 | ```python
26 | from WeiboBot import Bot
27 | from WeiboBot.message import Chat
28 | from WeiboBot.weibo import Weibo
29 | from WeiboBot.comment import Comment
30 |
31 | from datetime import datetime
32 |
33 | cookies = "your cookies"
34 | myBot = Bot(cookies=cookies)
35 |
36 |
37 | @myBot.onNewMsg # 被私信的时候触发
38 | async def on_msg(chat: Chat):
39 | for msg in chat.msg_list: # 消息列表
40 | print(f"{msg.sender_screen_name}:{msg.text}")
41 |
42 |
43 | @myBot.onNewWeibo # 首页刷到新微博时触发
44 | async def on_weibo(weibo: Weibo):
45 | if weibo.original_weibo is None: # 是原创微博
46 | print(f"{weibo.text}")
47 |
48 |
49 | @myBot.onMentionCmt # 提及我的评论时触发
50 | async def on_mention_cmt(cmt: Comment):
51 | print(f"{cmt.text}")
52 |
53 |
54 | @myBot.onTick # 每次循环触发
55 | async def on_tick():
56 | print(datetime.now())
57 |
58 |
59 | if __name__ == '__main__':
60 | myBot.run()
61 |
62 | ```
63 |
64 | ## 开始使用(主动模式)
65 |
66 | ```python
67 | from WeiboBot import Bot
68 | from WeiboBot.const import *
69 | import asyncio
70 |
71 | cookies = "your cookies"
72 | myBot = Bot(cookies=cookies)
73 |
74 |
75 | async def main():
76 | await asyncio.wait_for(myBot.login(), timeout=10) # 先登录
77 | weibo_example1 = myBot.get_weibo(123456789) # 获取微博
78 | weibo_example2 = myBot.post_weibo("发一条微博", visible=VISIBLE.ALL)
79 | # ...... 其他操作
80 |
81 |
82 | if __name__ == '__main__':
83 | asyncio.run(main())
84 |
85 | ```
86 |
87 | ## 如何获取cookie
88 |
89 | 登录m.weibo.cn
90 |
91 | 按F12查看请求头
92 |
93 | 
94 |
95 |
96 | ## 示例
97 |
98 | [好康Bot](https://github.com/MerlinCN/WeiboWatchdog)
99 |
100 | > 一个转发小姐姐的Bot
101 |
102 |
--------------------------------------------------------------------------------
/WeiboBot/__init__.py:
--------------------------------------------------------------------------------
1 | from .bot import *
2 | from .const import *
3 |
4 | name = "WeiboBot"
5 |
--------------------------------------------------------------------------------
/WeiboBot/action/__init__.py:
--------------------------------------------------------------------------------
1 | import traceback
2 |
3 | from WeiboBot.const import ACTION
4 | from WeiboBot.util import *
5 | from WeiboBot.exception import *
6 |
7 |
8 | class Action:
9 | def __init__(self, func, *args, **kwargs):
10 | self.func = func
11 | self.args = args
12 | self.kwargs = kwargs
13 | self.status = ACTION.UNDONE
14 | self.run_time = 0
15 | self.logger = get_logger(__name__)
16 |
17 | async def run(self):
18 | if self.run_time > 5:
19 | self.status = ACTION.MAX_TRY
20 | return None, self.status
21 | self.status = ACTION.RUNNING
22 | self.run_time += 1
23 | try:
24 | result = await self.func(*self.args, **self.kwargs)
25 | except RequestError:
26 | self.status = ACTION.FAILED
27 | self.logger.error(traceback.format_exc())
28 | return None, self.status
29 | except Exception as e:
30 | self.status = ACTION.MAX_TRY
31 | self.logger.error(e)
32 | return None, self.status
33 | self.status = ACTION.DONE
34 | return result, self.status
35 |
36 | def __str__(self):
37 | return f'