Răsfoiți Sursa

Add user search command

Tulir Asokan 4 ani în urmă
părinte
comite
68803d099d

+ 2 - 1
mauigpapi/http/api.py

@@ -4,7 +4,8 @@ from .challenge import ChallengeAPI
 from .account import AccountAPI
 from .qe import QeSyncAPI
 from .login import LoginAPI
+from .user import UserAPI
 
 
-class AndroidAPI(ThreadAPI, AccountAPI, QeSyncAPI, LoginAPI, UploadAPI, ChallengeAPI):
+class AndroidAPI(ThreadAPI, AccountAPI, QeSyncAPI, LoginAPI, UploadAPI, ChallengeAPI, UserAPI):
     pass

+ 28 - 0
mauigpapi/http/user.py

@@ -0,0 +1,28 @@
+# mautrix-instagram - A Matrix-Instagram puppeting bridge.
+# Copyright (C) 2020 Tulir Asokan
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+from ..types import UserSearchResponse
+from .base import BaseAndroidAPI
+
+
+class UserAPI(BaseAndroidAPI):
+    async def search_users(self, query: str, count: int = 30) -> UserSearchResponse:
+        req = {
+            "timezone_offset": self.state.device.timezone_offset,
+            "q": query,
+            "count": count,
+        }
+        return await self.std_http_get("/api/v1/users/search/", query=req,
+                                       response_type=UserSearchResponse)

+ 1 - 0
mauigpapi/types/__init__.py

@@ -25,3 +25,4 @@ from .mqtt import (Operation, ThreadAction, ReactionStatus, TypingStatus, Comman
                    RealtimeDirectEvent, LiveVideoSystemComment, LiveVideoCommentEvent,
                    LiveVideoComment, LiveVideoCommentPayload, ThreadSyncEvent)
 from .challenge import ChallengeStateResponse, ChallengeStateData
+from .user import SearchResultUser, UserSearchResponse

+ 38 - 0
mauigpapi/types/user.py

@@ -0,0 +1,38 @@
+# mautrix-instagram - A Matrix-Instagram puppeting bridge.
+# Copyright (C) 2020 Tulir Asokan
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+from typing import Optional, List
+
+from attr import dataclass
+
+from mautrix.types import SerializableAttrs
+
+from .account import BaseResponseUser
+
+
+@dataclass
+class SearchResultUser(BaseResponseUser, SerializableAttrs['SearchResultUser']):
+    mutual_followers_count: int
+    social_context: Optional[str] = None
+    search_social_context: Optional[str] = None
+
+
+@dataclass
+class UserSearchResponse(SerializableAttrs['UserSearchResponse']):
+    num_results: int
+    users: List[SearchResultUser]
+    has_more: bool
+    rank_token: str
+    status: str

+ 1 - 0
mautrix_instagram/commands/__init__.py

@@ -1,2 +1,3 @@
 from .auth import SECTION_AUTH
 from .conn import SECTION_CONNECTION
+from .misc import SECTION_MISC

+ 40 - 0
mautrix_instagram/commands/misc.py

@@ -0,0 +1,40 @@
+# mautrix-twitter - A Matrix-Twitter DM puppeting bridge
+# Copyright (C) 2020 Tulir Asokan
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+from mautrix.bridge.commands import HelpSection, command_handler
+
+from .. import puppet as pu
+from .typehint import CommandEvent
+
+SECTION_MISC = HelpSection("Miscellaneous", 40, "")
+
+
+@command_handler(needs_auth=True, management_only=False, help_section=SECTION_MISC,
+                 help_text="Search for Instagram users", help_args="<_query_>")
+async def search(evt: CommandEvent) -> None:
+    if len(evt.args) < 1:
+        await evt.reply("**Usage:** `$cmdprefix+sp search <query>`")
+        return
+    resp = await evt.sender.client.search_users(" ".join(evt.args))
+    if not resp.users:
+        await evt.reply("No results :(")
+        return
+    response_list = []
+    for user in resp.users[:10]:
+        puppet = await pu.Puppet.get_by_pk(user.pk, create=True)
+        await puppet.update_info(user, evt.sender)
+        response_list.append(f"* [{puppet.name}](https://matrix.to/#/{puppet.mxid})"
+                             f" ([@{user.username}](https://instagram.com/{user.username}))")
+    await evt.reply("\n".join(response_list))