鱼C论坛

 找回密码
 立即注册
查看: 493|回复: 7

pushdeer发送markdown错误

[复制链接]
发表于 2024-11-5 13:49:27 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
如题,在使用pushdeer的时候,发现了如下的问题,先附上代码:
  1. import os
  2. from dotenv import load_dotenv
  3. from pypushdeer import PushDeer
  4. from datetime import datetime
  5. import json
  6. import requests

  7. # 加载 .env 文件
  8. load_dotenv()

  9. # 读取配置文件
  10. with open("config.json", "r", encoding="utf-8") as f:
  11.     config = json.load(f)

  12. def httpGet(url, params):
  13.     try:
  14.         response = requests.get(url, params=params)
  15.         response.raise_for_status()  # 检查请求是否成功
  16.         return json.loads(response.content)  # 返回 JSON 解析后的内容
  17.     except requests.exceptions.RequestException as e:
  18.         print(f"GET 请求失败: {e}")
  19.         return {"success": False, "message": str(e)}
  20.     except json.JSONDecodeError as e:
  21.         print(f"JSON 解析失败: {e}")
  22.         return {"success": False, "message": str(e)}


  23. # 获取抖音热点热榜
  24. def getDouyinHot():
  25.     url = "https://api.vvhan.com/api/hotlist/douyinHot"
  26.     response = httpGet(url, {"type": "json"})
  27.     if response.get("success"):
  28.         data = response.get("data")
  29.         douyinHotList = []
  30.         douyinHot = ""
  31.         for item in data:
  32.             index = item["index"]
  33.             title = item["title"]
  34.             url = item["mobil_url"]
  35.             douyinHotListItem = f"{index}. [{title}]({url})"
  36.             douyinHotList.append(douyinHotListItem)
  37.             douyinHot = "\n".join(douyinHotList)
  38.         return douyinHot
  39.     else:
  40.         print("抖音热榜获取失败")
  41.         return ""



  42. # 获取服务器地址和 pushkeys
  43. server = os.getenv("SERVER")
  44. gaoDeKey = os.getenv("GAODE_KEY")
  45. # pushkeys = os.getenv("PUSHKEYS", "").split(",")
  46. pushkey = os.getenv("PUSH_KEY")


  47. # 获取对应方法返回的数据
  48. today = datetime.now().strftime("%Y年%m月%d日")
  49. douyinHotList = getDouyinHot()



  50. def main():
  51.     print(type(douyinHotList))
  52.     print(douyinHotList)
  53.     global pushkey  # 声明使用全局变量
  54.     pushkey = pushkey.strip()  # 去除可能存在的前后空格
  55.     # 初始化 PushDeer 对象
  56.     pushdeer = PushDeer(server=server, pushkey=pushkey)
  57.     # 发送消息
  58.     try:
  59.         response = pushdeer.send_markdown(f"{today}抖音热榜", desp=douyinHotList)
  60.         print("消息发送成功:", response)
  61.     except Exception as e:
  62.         print("消息发送失败:", e)



  63. if __name__ == "__main__":
  64.     main()
复制代码


运行之后,douyinHotList打印出来是没问题的,有东西,类型也是str,就是发送的这一句,response = pushdeer.send_markdown(f"{today}抖音热榜", desp=douyinHotList)报错,消息发送失败: Expecting value: line 1 column 1 (char 0)

之前用这个pushdeer.send_markdown一点问题都没有,直接发送markdown的图片和链接都可以,这次不知道为什么,大佬帮忙看看
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-11-5 14:03:07 | 显示全部楼层
根据您的描述,`response = pushdeer.send_markdown(f"{today}抖音热榜", desp=douyinHotList)`这行代码出现了错误,提示信息是“Expecting value: line 1 column 1 (char 0)”。这是一个经典的JSON解析错误,通常表示服务器返回了一个空响应或者不合法的JSON格式,从而无法解析。

以下是一些可能导致问题以及解决方法的检查点:

1. **API返回数据检查**:确保API `https://api.vvhan.com/api/hotlist/douyinHot`返回的数据是有效的JSON格式,您可以通过直接访问此URL查看返回内容来排除API问题。

2. **PushDeer服务检查**:检查PushDeer的服务器地址、密钥等配置是否正确,并确保PushDeer服务正常工作。可以通过其他简单的测试消息验证推送服务。

3. **空响应排查**:在发生请求错误的时候,打印`response.content`,检查返回是否为空或者非JSON格式:

  
  1. try:
  2.        response = pushdeer.send_markdown(f"{today}抖音热榜", desp=douyinHotList)
  3.        print("消息发送成功:", response)
  4.    except Exception as e:
  5.        print("消息发送失败:", e)
  6.        if hasattr(e, 'response') and e.response is not None:
  7.            print("返回内容:", e.response.text)
复制代码


4. **douyinHotList的内容问题**:确保`douyinHotList`的内容是正确格式的字符串。可以在调试时打印它以确认格式是否存在问题。

5. **异常捕获范围**:可能需要增加对其他特定异常的捕获,帮助了解错误的详细信息:

  
  1. except Exception as e:
  2.        print("消息发送失败:", e)
复制代码


通过这些步骤来诊断和解决问题。如果问题依然存在,可以尝试本地测试PushDeer接口以确保其服务无误,然后根据API返回的数据进一步修正代码逻辑。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-11-5 14:08:26 | 显示全部楼层
好像找到问题了,附上测试的代码:
  1. import requests
  2. from utils import getDouyinHot


  3. def send_pushdeer_notification(pushkey, title, markdown_content):
  4.     # 构建请求URL
  5.     url = "https://api2.pushdeer.com/message/push"
  6.     params = {
  7.         "pushkey": pushkey,
  8.         "text": title,
  9.         "desp": markdown_content,
  10.         "type": "markdown",
  11.     }

  12.     try:
  13.         # 发起GET请求
  14.         response = requests.get(url, params=params)
  15.         # 检查响应状态码
  16.         response.raise_for_status()

  17.         # 打印响应内容
  18.         print(response.json())

  19.     except requests.exceptions.RequestException as e:
  20.         # 打印异常信息
  21.         print(f"请求失败: {e}")
  22.     except ValueError as e:
  23.         # JSON解码失败,打印响应文本以便调试
  24.         print(f"无法解析JSON响应: {response.text}")


  25. # 示例用法
  26. douyinHotList = getDouyinHot()
  27. pushkey = "xxx"
  28. title = "抖音热榜"
  29. markdown_content = douyinHotList

  30. send_pushdeer_notification(pushkey, title, markdown_content)
复制代码


报错是请求失败: 414 Client Error: Request-URI Too Long for url: https://api2.pushdeer.com/message/push?

这是官方版本,哎,内容太长了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-11-5 14:08:45 | 显示全部楼层
如默 发表于 2024-11-05 14:08
好像找到问题了,附上测试的代码:
import requests
...


`414 Client Error: Request-URI Too Long` 错误通常表示URL或GET请求的参数太长,从而超出了服务器可以处理的限制。PushDeer的API可能对请求长度有限制。

解决方法是改用POST请求而不是GET请求,因为POST请求可以处理更长的内容体。下面是将请求方式从GET改为POST的示例代码:

  1. import requests
  2. from utils import getDouyinHot

  3. def send_pushdeer_notification(pushkey, title, markdown_content):
  4.     # 构建请求URL
  5.     url = "https://api2.pushdeer.com/message/push"
  6.     payload = {
  7.         "pushkey": pushkey,
  8.         "text": title,
  9.         "desp": markdown_content,
  10.         "type": "markdown",
  11.     }

  12.     try:
  13.         # 发起POST请求
  14.         response = requests.post(url, data=payload)
  15.         # 检查响应状态码
  16.         response.raise_for_status()

  17.         # 打印响应内容
  18.         print(response.json())

  19.     except requests.exceptions.RequestException as e:
  20.         # 打印异常信息
  21.         print(f"请求失败: {e}")
  22.     except ValueError as e:
  23.         # JSON解码失败,打印响应文本以便调试
  24.         print(f"无法解析JSON响应: {response.text}")

  25. # 示例用法
  26. douyinHotList = getDouyinHot()
  27. pushkey = "xxx"
  28. title = "抖音热榜"
  29. markdown_content = douyinHotList

  30. send_pushdeer_notification(pushkey, title, markdown_content)
复制代码


通过使用POST请求,数据不会放在URL中,而是作为报文体传递,通常能够解决URI过长的问题。希望这能帮助您解决问题!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-11-5 18:15:13 | 显示全部楼层
5
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-11-7 08:38:48 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-11-10 09:15:05 | 显示全部楼层
666
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-11-12 08:06:27 | 显示全部楼层

回帖奖励 +10 鱼币

111
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-8 04:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表