boxmoe_header_banner_img

Hello! 欢迎来到盒子萌!

加载中

文章导读

利用go-cqhttp搭建qq机器人推送hostloc新帖


avatar
专收爆米花 2021年12月6日 4.71k

周末无聊的时候在家里折腾了一个qq机器人推送api,结果在github上找到了一些资料,终于实现了新帖qq机器人推送到qq群
插件必须在python3环境下操作,如果你没有就安装,如果不会那就无缘。
这个脚本是利用了一位大佬制作的api,文章结尾有提及相关github

go-cqhttp和脚本都支持在linux运行,所以你也可以在linux平台上挂机器人和执行脚本,我是在腾讯云的3年130的真香鸡上用windows搭建的

利用go-cqhttp搭建qq机器人推送hostloc新帖

利用go-cqhttp搭建qq机器人推送hostloc新帖

安装go-cqhttp

go-cqhttp安装教程

创建python脚本 -推送到qq群

复制代码
  1. import requests
  2. import time
  3. from urllib import parse
  4.  
  5. hostloc_list = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", ]
  6. hostloc_title = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", ]
  7. url_1 = "https://www.hostloc.com/"
  8. #这里输入qq群号码,和go-cqhttp端口
  9. qq='817933066'
  10. port='5700'
  11. while True:
  12. try:
  13. with requests.get('https://hostloc.cherbim.ml/', stream=True, timeout=5) as r:
  14. print(time.strftime("%m-%d %H:%M:%S", time.localtime()))
  15. for i in r.json()["new_data"][0][15:]:
  16. if i['主题ID'] in hostloc_list or i['主题'] in hostloc_title:
  17. pass
  18. else:
  19. hostloc_list = hostloc_list[1::]
  20. hostloc_list.append(i['主题ID'])
  21. hostloc_title = hostloc_title[1::]
  22. hostloc_title.append(i['主题'])
  23. a = "https://www.hostloc.com/thread-{0}-1-1.html".format(i['主题ID'])
  24. time_1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  25. if "论坛bug,此贴内容无法查看~" not in i['主题内容'][0:100]:
  26. a = a
  27. else:
  28. a = f"<s>{a}</s>"
  29. text = '主 题:' + "{}".format(i['主题'].replace("&", "%26").replace("<", "%26lt%3b").replace(">", "%26gt%3b").replace("#", " ")) + '\n' + '发 布 者:' + '''{1}'''.format(i['发布者链接'], i['发布者']) + '\n' + '时 间:' + time_1 + '\n' + '内容预览:' + '''{0}'''.format(i['主题内容'][0:100].replace("&", "%26").replace("<", "%26lt%3b").replace(">", "%26gt%3b").replace("#", " ")) + "\n" + "直达链接: " + a
  30. print(text)
  31. requests.get('http://127.0.0.1:' + port + '/send_group_msg?group_id=' + qq + '&message=' + text)
  32. time.sleep(2)
  33. except Exception:
  34. print("网络错误,请稍后重试")
  35. time.sleep(5)

创建python脚本 -推送到个人qq

复制代码
  1. import requests
  2. import time
  3. from urllib import parse
  4.  
  5. hostloc_list = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", ]
  6. hostloc_title = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", ]
  7. url_1 = "https://www.hostloc.com/"
  8. #这里输入qq号码,和go-cqhttp端口
  9. qq='504888738'
  10. port='5700'
  11. while True:
  12. try:
  13. with requests.get('https://hostloc.cherbim.ml/', stream=True, timeout=5) as r:
  14. print(time.strftime("%m-%d %H:%M:%S", time.localtime()))
  15. for i in r.json()["new_data"][0][15:]:
  16. if i['主题ID'] in hostloc_list or i['主题'] in hostloc_title:
  17. pass
  18. else:
  19. hostloc_list = hostloc_list[1::]
  20. hostloc_list.append(i['主题ID'])
  21. hostloc_title = hostloc_title[1::]
  22. hostloc_title.append(i['主题'])
  23. a = "https://www.hostloc.com/thread-{0}-1-1.html".format(i['主题ID'])
  24. time_1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  25. if "论坛bug,此贴内容无法查看~" not in i['主题内容'][0:100]:
  26. a = a
  27. else:
  28. a = f"<s>{a}</s>"
  29. text = '主 题:' + "{}".format(i['主题'].replace("&", "%26").replace("<", "%26lt%3b").replace(">", "%26gt%3b").replace("#", " ")) + '\n' + '发 布 者:' + '''{1}'''.format(i['发布者链接'], i['发布者']) + '\n' + '时 间:' + time_1 + '\n' + '内容预览:' + '''{0}'''.format(i['主题内容'][0:100].replace("&", "%26").replace("<", "%26lt%3b").replace(">", "%26gt%3b").replace("#", " ")) + "\n" + "直达链接: " + a
  30. print(text)
  31. requests.get('http://127.0.0.1:' + port + '/send_private_msg?user_id=' + qq + '&message=' + text)
  32. time.sleep(2)
  33. except Exception:
  34. print("网络错误,请稍后重试")
  35. time.sleep(5)

运行脚本开始推送

cd到你的脚本位置,运行 python 脚本名.py 就可以开始推送了,脚本下载

下载信息

部分资源来源于网络如有侵权,请联系删除

github相关资料
https://github.com/MoeList/Hostloc-Q-Bot
https://github.com/w2r/hostloc2tg



评论(3)

查看评论列表
评论头像
欧乐安 2021年12月07日
很好的内容,可以用起来
评论头像
九凌网络 2022年09月19日
可以尝试一下
评论头像
ttian 2022年10月23日
这个是不是少一些东西啊,我看没有配置发送qq账号的相关信息啊

发表评论

表情 颜文字
😀😁😂🤣😃😄😅😆😉😊😋😎😍🥰😘😗😙😚😛😝🤗🤔🤨😐😑😶🙄😏😣😥😮🤤😴😪😵😵😵🤯🤠🤡🤥🤫🤔🤨😐😑😶🙄