Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]关于Twitter DB的uniqueId、user_id的问题,很容易修复的BUG #232

Open
YanCore opened this issue Jan 9, 2025 · 2 comments
Labels
故障(bug) 代码问题导致的错误(Bugs caused by code problems) 已确认(confirmed) 该issue已被处理,将在下次推送中更新(The issue has been processed and will be updated in the next push)

Comments

@YanCore
Copy link

YanCore commented Jan 9, 2025

详细描述错误

  1. f2/apps/twitter/filter.pynickname拼写错误。
    def nicename_raw(self):
  2. Twitter DB 使用unique_id存储,却使用user_id进行查询、更新。
    f"SELECT * FROM {self.TABLE_NAME} WHERE user_id=?", (user_id,)

系统平台

  • 操作系统: Win10
  • F2版本: 0.0.1.7

错误重现

import asyncio
from f2.apps.twitter.handler import TwitterHandler
from f2.apps.twitter.db import AsyncUserDB

kwargs = {
    "headers": {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0",
        "Referer": "https://www.x.com/",
    },
    "proxies": {"http://": None, "https://": None},
    "cookie": "YOUR_COOKIE_HERE",
    # "X-Csrf-Token": "",
    "path": "Download",
    # "mode": "post",
}


async def main():
    async with AsyncUserDB("twitter_users.db") as audb:
        uniqueId = "realDonaldTrump"
        print(
            await TwitterHandler(kwargs).get_or_add_user_data(
                kwargs=kwargs, uniqueId=uniqueId, db=audb
            )
        )


if __name__ == "__main__":
    asyncio.run(main())

数据库记录截图
image

源码存在问题的部分

f2/apps/twitter/handler.py调用db.get_user_info(uniqueId),但使用user_id查询数据库。

class TwitterHandler:
    async def get_or_add_user_data(
        self,
        kwargs: dict,
        uniqueId: str,
        db: AsyncUserDB,
    ) -> Path:
        """
        获取或创建用户数据同时创建用户目录
        """

        # 尝试从数据库中获取用户数据
        local_user_data = await db.get_user_info(uniqueId)

f2/apps/twitter/db.pyupdate_user_infoget_user_infodelete_user_info方法。

class AsyncUserDB(BaseDB):
    async def update_user_info(self, user_id: str, **kwargs) -> None:
        ...

    async def get_user_info(self, user_id: str) -> dict:
        """
        获取用户信息

        Args:
            user_id (str): 用户唯一标识

        Returns:
            dict: 对应的用户信息,如果不存在则返回 None
        """
        cursor = await self.execute(
            f"SELECT * FROM {self.TABLE_NAME} WHERE user_id=?", (user_id,)
        )
        result = await cursor.fetchone()
        if not result:
            return {}
        columns = [description[0] for description in cursor.description]
        return dict(zip(columns, result))

    async def delete_user_info(self, user_id: str) -> None:
        ...
@YanCore YanCore added the 故障(bug) 代码问题导致的错误(Bugs caused by code problems) label Jan 9, 2025
Copy link

dosubot bot commented Jan 9, 2025

Hey @YanCore! I'm here to help you with your GitHub issues. If you need assistance with bug fixes, questions, or becoming a contributor, feel free to ask.

To address the issues mentioned:

  1. Nickname Spelling Error: If there's a spelling error in the nickname property in f2/apps/twitter/filter.py, you should correct it to ensure proper functionality.

  2. Database Query Mismatch: The methods in the AsyncUserDB class (get_user_info, update_user_info, delete_user_info) are using user_id for database operations, but the database uses user_unique_id as the primary key. You need to update these methods to use user_unique_id instead. Here's how you can modify them:

    • Update update_user_info method:

      async def update_user_info(self, user_unique_id: str, **kwargs) -> None:
          user_data = await self.get_user_info(user_unique_id)
          if user_data:
              set_sql = ", ".join([f"{key} = ?" for key in kwargs.keys()])
              await self.execute(
                  f"UPDATE {self.TABLE_NAME} SET {set_sql} WHERE user_unique_id=?",
                  (*kwargs.values(), user_unique_id),
              )
              await self.commit()
    • Update get_user_info method:

      async def get_user_info(self, user_unique_id: str) -> dict:
          cursor = await self.execute(
              f"SELECT * FROM {self.TABLE_NAME} WHERE user_unique_id=?", (user_unique_id,)
          )
          result = await cursor.fetchone()
          if not result:
              return {}
          columns = [description[0] for description in cursor.description]
          return dict(zip(columns, result))
    • Update delete_user_info method:

      async def delete_user_info(self, user_unique_id: str) -> None:
          await self.execute(f"DELETE FROM {self.TABLE_NAME} WHERE user_unique_id=?", (user_unique_id,))
          await self.commit()

By making these changes, the methods will correctly use user_unique_id for querying the database, aligning with the database schema where user_unique_id is the primary key [1].

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Other

@Johnserf-Seed
Copy link
Owner

你好,感谢你提出详细且专业的 Issue!我们已经对你反馈的问题进行了修复,感谢你的支持!😊

@Johnserf-Seed Johnserf-Seed added the 已确认(confirmed) 该issue已被处理,将在下次推送中更新(The issue has been processed and will be updated in the next push) label Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
故障(bug) 代码问题导致的错误(Bugs caused by code problems) 已确认(confirmed) 该issue已被处理,将在下次推送中更新(The issue has been processed and will be updated in the next push)
Projects
None yet
Development

No branches or pull requests

2 participants