Exemplo n.º 1
0
 async def get_paid_content(
     self,
     check: bool = False,
     refresh: bool = True,
     limit: int = 10,
     offset: int = 0,
     inside_loop: bool = False,
 ) -> list[create_message | create_post] | ErrorDetails:
     result, status = await api_helper.default_data(self, refresh)
     if status:
         return result
     link = endpoint_links(global_limit=limit, global_offset=offset).paid_api
     final_results = await self.session_manager.json_request(link)
     if not isinstance(final_results, ErrorDetails):
         if len(final_results) > 0 and not check:
             results2 = await self.get_paid_content(
                 limit=limit, offset=limit + offset, inside_loop=True
             )
             final_results.extend(results2)
         if not inside_loop:
             temp: list[create_message | create_post] = []
             for final_result in final_results:
                 content = None
                 if final_result["responseType"] == "message":
                     user = create_user(final_result["fromUser"], self)
                     content = create_message(final_result, user)
                     print
                 elif final_result["responseType"] == "post":
                     user = create_user(final_result["author"], self)
                     content = create_post(final_result, user)
                 if content:
                     temp.append(content)
             final_results = temp
         self.paid_content = final_results
     return final_results
Exemplo n.º 2
0
 async def get_message_by_id(
     self,
     user_id: Optional[int] = None,
     message_id: Optional[int] = None,
     refresh: bool = True,
     limit: int = 10,
     offset: int = 0,
 ):
     if not user_id:
         user_id = self.id
     link = endpoint_links(
         identifier=user_id,
         identifier2=message_id,
         global_limit=limit,
         global_offset=offset,
     ).message_by_id
     response = await self.get_session_manager().json_request(link)
     if isinstance(response, dict):
         temp_response: dict[str, Any] = response
         results: list[dict[str, Any]] = [
             x for x in temp_response["list"] if x["id"] == message_id
         ]
         result = results[0] if results else {}
         final_result = message_model.create_message(result, self)
         return final_result
     return response
Exemplo n.º 3
0
    async def get_chats(
        self,
        links: Optional[list[str]] = None,
        limit: int = 100,
        offset: int = 0,
        refresh: bool = True,
        inside_loop: bool = False,
    ) -> list[dict[str, Any]]:
        result, status = await api_helper.default_data(self, refresh)
        if status:
            return result
        if links is None:
            links = []
        api_count = self.chatMessagesCount
        if api_count and not links:
            link = endpoint_links(
                identifier=self.id, global_limit=limit, global_offset=offset
            ).list_chats
            ceil = math.ceil(api_count / limit)
            numbers = list(range(ceil))
            for num in numbers:
                num = num * limit
                link = link.replace(f"limit={limit}", f"limit={limit}")
                new_link = link.replace("offset=0", f"offset={num}")
                links.append(new_link)
        multiplier = self.session_manager.max_threads
        if links:
            link = links[-1]
        else:
            link = endpoint_links(
                identifier=self.id, global_limit=limit, global_offset=offset
            ).list_chats
        links_2 = api_helper.calculate_the_unpredictable(link, limit, multiplier)
        final_links = links
        if not inside_loop:
            final_links += links_2
        else:
            final_links = links_2
        results = await self.session_manager.async_requests(final_links)
        has_more = results[-1]["hasMore"]
        final_results = [x["list"] for x in results]
        final_results = list(chain.from_iterable(final_results))
        for result in final_results:
            result["withUser"] = create_user(result["withUser"], self)
            result["lastMessage"] = create_message(
                result["lastMessage"], result["withUser"]
            )

        if has_more:
            results2 = await self.get_chats(
                links=[final_links[-1]],
                limit=limit,
                offset=limit + offset,
                inside_loop=True,
            )
            final_results.extend(results2)

        final_results.sort(key=lambda x: x["withUser"].id, reverse=True)
        self.chats = final_results
        return final_results
Exemplo n.º 4
0
    async def get_messages(
        self,
        links: Optional[list[str]] = None,
        limit: int = 10,
        offset: int = 0,
        refresh: bool = True,
        inside_loop: bool = False,
    ):
        result, status = await api_helper.default_data(self, refresh)
        if status:
            return result
        if links is None:
            links = []
        multiplier = self.get_session_manager().max_threads
        if links:
            link = links[-1]
        else:
            link = endpoint_links(
                identifier=self.id, global_limit=limit, global_offset=offset
            ).message_api
            links.append(link)
        links2 = api_helper.calculate_the_unpredictable(link, limit, multiplier)
        if not inside_loop:
            links += links2
        else:
            links = links2
        results = await self.get_session_manager().async_requests(links)
        results = await api_helper.remove_errors(results)
        final_results = []
        if isinstance(results, list):
            results = [x for x in results if x]
            has_more = results[-1]["hasMore"] if results else False
            final_results = [x["list"] for x in results if "list" in x]
            final_results = list(chain.from_iterable(final_results))

            if has_more:
                results2 = await self.get_messages(
                    links=[links[-1]],
                    limit=limit,
                    offset=limit + offset,
                    inside_loop=True,
                )
                final_results.extend(results2)
            print
            if not inside_loop:
                final_results = [
                    message_model.create_message(x, self) for x in final_results if x
                ]
            else:
                final_results.sort(key=lambda x: x["fromUser"]["id"], reverse=True)
            self.temp_scraped.Messages = final_results
        return final_results