Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

豆瓣移动端api示例 #1

Closed
screenl opened this issue Jun 18, 2022 · 2 comments
Closed

豆瓣移动端api示例 #1

screenl opened this issue Jun 18, 2022 · 2 comments
Labels
todo 计划在不久后根据issue进行更改

Comments

@screenl
Copy link

screenl commented Jun 18, 2022

import asyncio
import aiohttp
import hmac
import base64
import time
from urllib.parse import urlparse, quote, urlencode

headers = {
    "User-Agent": "api-client/1 com.douban.frodo/7.27.0.6(231) Android/25 product/DT1901A vendor/smartisan model/DT1901A brand/smartisan  rom/android  network/wifi  udid/5fe86d1e414d8417ff3ec84c369e28c97a7d9d45  platform/AndroidPad",
}

dm = {
    "channel": "Douban",
    "os_rom": "android",
}

def encrypt(api, method, data):
    path = urlparse(api).path
    secret = b"bf7dddc7c9cfe6f7"
    timestamp = int(time.time())
    message = ("&".join([method, quote(path, safe=""), str(timestamp)]))
    h = hmac.new(secret, message.encode(), digestmod="sha1")
    b = base64.b64encode(h.digest())
    return {
        **data,
        "apikey": "0dad551ec0f84ed02907ff5c42e8ec70",
        "_ts": timestamp,
        "_sig": b.decode(),
    }
# 生成签名函数


async def get(api, params=None):
    async with aiohttp.ClientSession() as session:
        async with session.get(api, params=params, headers=headers) as resp:
            data = await resp.json()
            return data


async def getTopic(tid):
    api = "https://frodo.douban.com/api/v2/group/topic/{}".format(tid)
    params = encrypt(api, "GET", dm)
    return await get(api, params)
# 获取主贴内容


async def getComment(tid, start=0, count=20):
    api = "https://frodo.douban.com/api/v2/group/topic/{}/comments".format(tid)
    params = encrypt(api, "GET", {**dm, "start": start, "count": count})
    return await get(api, params)
# 获取评论(一次最多20条)


async def getAllComments(tid):
    comments = []
    i = 0
    r = await getComment(tid)
    while r["comments"]:
        i += 20
        comments += r["comments"]
        r = await getComment(tid, i)
        print(i)
        await asyncio.sleep(0.5)
    return comments
# 获取全部评论

仅供参考

@WendaoLee WendaoLee added the todo 计划在不久后根据issue进行更改 label Jun 18, 2022
@WendaoLee
Copy link
Owner

非常感谢!!!😘😘😘
我会在之后得空(大概是半个月后)的时候进行修改(现在手头还有其他活,抱歉)。
非常感谢你提供的帮助。

@WendaoLee WendaoLee pinned this issue Jun 18, 2022
@WendaoLee
Copy link
Owner

感谢您之前提供的示例。
尽管后面因为现实忙碌等原因最后还是没完成枝江存档姬的部署改版,但是它运用到了我对魂组的数据的备份爬取中

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
todo 计划在不久后根据issue进行更改
Projects
None yet
Development

No branches or pull requests

2 participants