def process_func(msg): peer_id = msg.get('peer_id') atts = list( filter( lambda x: x.get('type') in ['audio', 'video'], msg.get('attachments') + get_attachments(msg.get('fwd_messages')))) for att in atts: type = att.get('type') obj = att.get(type) owner_id = att.get('owner_id') id = obj.get('id') existing_obj: RawLink = RawLink.get(id) if existing_obj and existing_obj.owner_id == owner_id: orig_user: User = User.get(existing_obj.user_id) orig_user.downs += 1 session.add(orig_user) seen_message = Phrase.get_random().split(':')[1].strip() + '\n' seen_message += f'Отправил {orig_user.first_name}' \ f' {orig_user.last_name} в' \ f' {format_time(existing_obj.add_time)}\n' api.messages.send(peer_id=peer_id, message=seen_message, random_id=get_rand()) session.add(orig_user) else: raw_link_id = obj.get('id') if RawLink.get(raw_link_id) is None: new_raw_link_obj = RawLink(id=raw_link_id, type=type, owner_id=obj.get('owner_id'), access_key=obj.get('access_key'), track_code=obj.get('track_code'), url=obj.get('url'), user_id=msg.get('from_id')) session.add(new_raw_link_obj) session.commit()
def check_func(msg): if msg.get('from_id') != 63448544: return False att = msg.get('attachments') + get_attachments(msg.get('fwd_messages')) return len(att) > 0 and any( list(map(lambda x: x.get('type') == 'audio', att)))
def check_func(msg): return any( map(lambda x: x.get('type') in ['audio', 'video'], msg.get('attachments') + get_attachments(msg.get('fwd_messages'))))
def process_pic(msg) -> None: # Getting all the attachments even in forwarded messages attachments = msg.get('attachments') + get_attachments( msg.get('fwd_messages')) # Leaving only the photos photos = list( map(lambda x: x.get('photo'), list(filter(lambda x: x.get('type') == 'photo', attachments)))) # New thread - new session outer_session = session_factory() sender_id = msg.get('from_id') # Getting the user from DB or creating a new one user: User = outer_session.query(User).filter(User.id == sender_id).first() if not User: user = User(sender_id) outer_session.add(user) outer_session.commit() user.all_pics += len(photos) # Message that will be sent to chat if picture has been already seen seen_message = Phrase.get_random().split(':')[1].strip() + '\n' seen: int = 0 start_time = time.time() # Count of seen pictures for pic in photos: sizes = pic.get('sizes') # All sizes for this picture pic_id = pic.get('id') # Checking if a max size of this picture has been already seen result = was_seen(sizes) if result.get('result'): # Already seen seen += 1 picture_size: PictureSize = result.get('simpic') local_session = session_factory() picture: Picture = Picture.get( picture_size.pic_id) if picture_size else None user.bads += picture.bads orig_user: User = User.get(picture.user_id, local_session) if picture else None if orig_user: seen_message += f'Отправил {orig_user.first_name}' \ f' {orig_user.last_name} в' \ f' {format_time(picture_size.add_time)}\n' local_session.close() else: # New picture # Adding it to the DB picture = Picture(pic_id, sender_id, pic.get('owner_id'), pic.get('access_key')) outer_session.add(picture) outer_session.commit() for size in sizes: outer_session.add( PictureSize(pic_id, size.get('type'), size.get('url'))) outer_session.add(PicMessage(sender_id, pic_id, msg.get('text'))) outer_session.commit() end_time = time.time() log(label, f"Checked in {end_time - start_time}") # Adding negative carma for each seen picture # Sending a message if any picture was not new if seen > 0: log(label, f"{user.first_name} {user.last_name} downs +1 = {user.downs}") user.downs += 1 peer_id = msg.get('peer_id') api.messages.send(peer_id=peer_id, message=seen_message, random_id=get_rand()) outer_session.add(user) outer_session.commit() outer_session.close()
def check_func(msg) -> bool: att = msg.get('attachments') + get_attachments(msg.get('fwd_messages')) # True if there is any attachments with type 'photo' return len(att) > 0 and any( list(map(lambda x: x.get('type') == 'photo', att)))