PyPtt / PyPtt

可直接連線登入的 Python PTT library
https://pyptt.cc/
GNU Lesser General Public License v3.0
691 stars 89 forks source link

呼叫 docker get_post 參數錯誤 #145

Open leofcshen opened 1 month ago

leofcshen commented 1 month ago
import PyPtt
import requests
import json
from urllib.parse import quote

def object_encode(obj):
    return quote(json.dumps(obj))

# 設置參數
args = {
    "board": "Python",
    "index": 1,
    "search_list":[[PyPtt.SearchType.KEYWORD, 'PyPtt']]
}

# 將 args 轉換為 JSON 並進行 URL 編碼
encoded_args = object_encode(args)

# 構建完整的 URL
base_url = "https://pyptt.rovingwind.synology.me/api"
full_url = f"{base_url}?api=get_post&args={encoded_args}"

# 發送請求
r = requests.get(full_url)
print(r.request.url)
#https://xxx.com/api?api=get_post&args=%7B%22board%22%3A%20%22Python%22%2C%20%22index%22%3A%201%2C%20%22search_list%22%3A%20%5B%5B%22KEYWORD%22%2C%20%22PyPtt%22%5D%5D%7D
print(r.json())
#{'api': 'get_post', 'args': {'board': 'Python', 'index': 1, 'search_list': [['KEYWORD', 'PyPtt']]}, 'error': "[PyPtt] search_list 必須為 <class 'list'>, but got None"}

請問 search_list 參數要怎麼給值才對

PttCodingMan commented 1 month ago

Let me check.

leofcshen commented 1 month ago

我也想測試 https://pyptt.cc/docker.html 下列的用法,但程式庫裡查不到 object_encode 的方法。

import PyPtt
import requests

from src.utils import object_encode
from tests import config

if __name__ == '__main__':
    params = {
        "api": "login",
        "args": object_encode({
            'ptt_id': config.PTT_ID,
            'ptt_pw': config.PTT_PW
        })
    }
    r = requests.get("http://localhost:8787/api", params=params)
    print(r.json())

    params = {
        "api": "get_time",
    }
    r = requests.get("http://localhost:8787/api", params=params)
    print(r.json())

    params = {
        "api": "get_newest_index",
        "args": object_encode({
            'board': 'Gossiping',
            'index_type': PyPtt.NewIndex.BOARD
        })
    }
    r = requests.get("http://localhost:8787/api", params=params)
    print(r.json())

    ##############################

    content = """此內容由 PyPtt image 執行 PO 文

    測試換行 123
    測試換行 456
    測試換行 789
    """

    params = {
        "api": "post",
        "args": object_encode({
            'board': 'Test',
            'title_index': 1,
            'title': 'test',
            'content': content,
        })
    }
    r = requests.get("http://localhost:8787/api", params=params)
    print(r.json())

    ##############################

    params = {
        "api": "logout",
    }
    r = requests.get("http://localhost:8787/api", params=params)
    print(r.json())
leofcshen commented 1 week ago

請問有進一步的資訊嗎

PttCodingMan commented 6 days ago

Hi @leofcshen,

最近比較忙碌一些。 我預計週末可以給進一步的更新。

CodingMan