async def forward_msg(self, msg: Message, to: int, what: str = 'photo') -> None: if await self.blacklist_checker(msg): # if msg.from_user and msg.from_user.id == 630175608: return # block tgcn-captcha self.func_blacklist( BlackListForwardRequest( msg, LogStruct(True, 'forward blacklist context %s from %s (id: %d)', what, msg.chat.title, msg.chat.id))) return forward_target = to # spec_target = None if what == 'other' else await self.redis.get(f'{self.redis_prefix}{msg.chat.id}') spec_target = None if what == 'other' else await self.redis.get( str(msg.chat.id)) if spec_target is None: # spec_target = await self.redis.get(f'{self.redis_prefix}{msg.forward_from_chat.id}') if msg.forward_from_chat: spec_target = await self.redis.get( str(msg.forward_from_chat.id)) if spec_target is not None: forward_target = getattr(self.configure, spec_target.decode()) elif is_bot(msg): forward_target = self.configure.bot self.ForwardThread.put( ForwardRequest( forward_target, msg, LogStruct(True, 'forward %s from %s (id: %d)', what, msg.chat.title, msg.chat.id)))
async def forward_msg(self, msg: Message, to: int, what: str = 'photo') -> None: if msg.has_protected_content: return if await self.redis.check_msg_from_blacklist(msg): return forward_target = to # spec_target = None if what == 'other' else await self.redis.get(f'{self.redis_prefix}{msg.chat.id}') spec_target = None if what == 'other' else await self.redis.get( str(msg.chat.id)) if spec_target is None: # spec_target = await self.redis.get(f'{self.redis_prefix}{msg.forward_from_chat.id}') if msg.forward_from_chat: spec_target = await self.redis.get( str(msg.forward_from_chat.id)) if spec_target is not None: forward_target = getattr(self.configure, spec_target.decode()) elif is_bot(msg): forward_target = self.configure.bot self.ForwardThread.put( ForwardRequest( forward_target, msg, LogStruct(True, 'forward %s from %s (id: %d)', what, msg.chat.title, msg.chat.id)))
def put_blacklist(cls, request: BlackListForwardRequest) -> None: cls.put( ForwardRequest.from_super(Configure.get_instance().blacklist, request)) # type: ignore
async def run(self) -> None: checkfunc = self.checker.checkFile if not self.dirty_run else self.checker.checkFile_dirty photos, videos, docs = [], [], [] msg_group = await self.client.get_history(self.target_id, offset_id=self.offset_id) await self.client.send_message( self.chat_id, 'Now process query {}, total {} messages{}'.format( self.target_id, msg_group.messages[0]['message_id'], ' (Dirty mode)' if self.dirty_run else '')) status_thread = SetTypingCoroutine(self.client, self.chat_id) self.offset_id = msg_group.messages[0]['message_id'] while self.offset_id > 1: for x in list(msg_group.messages): if x.photo: if not await checkfunc(x.photo.sizes[-1].file_unique_id): continue photos.append((is_bot(x), { 'chat': { 'id': self.target_id }, 'message_id': x['message_id'] })) elif x.video: if not await checkfunc(x.video.file_unique_id): continue videos.append((is_bot(x), { 'chat': { 'id': self.target_id }, 'message_id': x['message_id'] })) elif x.document: if '/' in x.document.mime_type and x.document.mime_type.split('/')[0] in ('image', 'video') and \ not await checkfunc(x.document.file_unique_id): continue docs.append((is_bot(x), { 'chat': { 'id': self.target_id }, 'message_id': x['message_id'] })) try: self.offset_id = msg_group.messages[-1]['message_id'] - 1 except IndexError: logger.info('Query channel end by message_id %d', self.offset_id + 1) break try: msg_group = await self.client.get_history( self.target_id, offset_id=self.offset_id) except pyrogram.errors.FloodWait as e: logger.warning('Got flood wait, sleep %d seconds', e.x) await asyncio.sleep(e.x) if not self.dirty_run: await self.client.send_message(self.configure.query_photo, f'Begin {self.target_id} forward') await self.client.send_message(self.configure.query_video, f'Begin {self.target_id} forward') await self.client.send_message(self.configure.query_doc, f'Begin {self.target_id} forward') for x in reversed(photos): ForwardThread.put( ForwardRequest( self.configure.query_photo if not x[0] else self.configure.bot_for, x[1])) for x in reversed(videos): ForwardThread.put( ForwardRequest( self.configure.query_video if not x[0] else self.configure.bot_for, x[1])) for x in reversed(docs): ForwardThread.put( ForwardRequest( self.configure.query_doc if not x[0] else self.configure.bot_for, x[1])) status_thread.set_off() await self.client.send_message( self.chat_id, 'Query completed {} photos,' ' {} videos, {} docs{}'.format( len(photos), len(videos), len(docs), ' (Dirty mode)' if self.dirty_run else '')) logger.info( 'Query %d completed%s, total %d photos, %d videos, %d documents.', self.target_id, ' (Dirty run)' if self.dirty_run else '', len(photos), len(videos), len(docs)) del photos, videos, docs