async def handle_matrix_avatar(self, sender: 'u.User', url: ContentURI) -> None: if self.peer_type not in ("chat", "channel"): # Invalid peer type return file = await self.main_intent.download_media(url) mime = magic.from_buffer(file, mime=True) ext = sane_mimetypes.guess_extension(mime) uploaded = await sender.client.upload_file(file, file_name=f"avatar{ext}") photo = InputChatUploadedPhoto(file=uploaded) if self.peer_type == "chat": response = await sender.client(EditChatPhotoRequest(chat_id=self.tgid, photo=photo)) else: channel = await self.get_input_entity(sender) response = await sender.client(EditPhotoRequest(channel=channel, photo=photo)) self.dedup.register_outgoing_actions(response) for update in response.updates: is_photo_update = (isinstance(update, UpdateNewMessage) and isinstance(update.message, MessageService) and isinstance(update.message.action, MessageActionChatEditPhoto)) if is_photo_update: loc, size = self._get_largest_photo_size(update.message.action.photo) self.photo_id = f"{size.location.volume_id}-{size.location.local_id}" self.save() break
async def icon_init(folder, newChannelID): channel_entity, input_chat_uploaded_photo = None, None try: icon = list(Path(folder + "/").glob("*.jpg"))[0] channel_entity = await client.get_entity(newChannelID) upload_file_result = await client.upload_file(file=str(icon)) input_chat_uploaded_photo = InputChatUploadedPhoto(upload_file_result) finally: return channel_entity, input_chat_uploaded_photo
async def avatar(event,white=False): sender = await event.get_sender() logger.info("sender_id = %s,username= %s,sender first_name = %s,last_name=%s, message = %s,chat_id= %s",event.message.from_id,sender.username,sender.first_name,sender.last_name,event.message.message ,event.chat_id) global chat_list if event.chat_id not in chat_list: m = await event.reply("如果需要使用請先聯系 @MarvelousBlack 將該羣加入白名單。chat_id={}".format(event.chat_id)) return None async with avatar_lock: if not await is_timeup(event): return None replymsg = await event.message.get_reply_message() waitmsg = await event.reply("處理中...") try: if replymsg.file is not None: if replymsg.file.size > 5*1024**2: m = await event.reply("不要啊啊啊啊,太大了!!!") return None if replymsg.file.name == "sticker.webp": file_name = '/tmp/sticker.webp' await client.download_media(message=replymsg,file=file_name) conv_sticker(file_name) elif replymsg.file.mime_type == "video/mp4": file_name = '/tmp/video.mp4' await client.download_media(message=replymsg,file=file_name) conv_mp4(file_name) elif replymsg.file.mime_type == "image/gif": file_name = '/tmp/noirgif.gif' await client.download_media(message=replymsg,file=file_name) conv_gif(file_name) elif "image" in replymsg.file.mime_type: file_name = '/tmp/image'+ replymsg.file.ext await client.download_media(message=replymsg,file=file_name) img2png(file_name) else: m = await event.reply("不支持的類型") return None if white: conv_white('/tmp/avatar.png') if size2small('/tmp/avatar.png'): os.remove('/tmp/avatar.png') m = await event.reply("你的頭太小了!需要 160×160 以上") return None upload_file_result = await client.upload_file(file='/tmp/avatar.png') os.remove('/tmp/avatar.png') input_chat_uploaded_photo = InputChatUploadedPhoto(upload_file_result) result = await client(EditPhotoRequest(channel=event.message.to_id, photo=input_chat_uploaded_photo)) update_last(event) logger.info("success,chat_id = %s",event.chat_id) except BaseException: m = await event.reply("你的頭呢?") except Exception as e: logger.error(e) m = await event.reply("出錯了!") finally: await waitmsg.delete() return None
def main(): store_local = input('Do you want to leave the local files? [N/y] ') in ['y', 'yes'] vkaudio, user_id = auth_vk() with auth_tg() as client: VKMusicChannel = None last_file = None progress = 0 dialogs = client.get_dialogs(limit=None) for chat in dialogs: if type(chat.entity) == Channel and chat.title == channelName: VKMusicChannel = chat if VKMusicChannel is None: VKMusicChannel = client(CreateChannelRequest( title=channelName, about='made with https://github.com/HaCk3Dq/vktotg')).chats[0] client(EditPhotoRequest( InputChannel(VKMusicChannel.id, VKMusicChannel.access_hash), InputChatUploadedPhoto( client.upload_file('music.jpg')) )) client.delete_messages(client.get_entity(VKMusicChannel), 2) else: last_file = client.get_messages(VKMusicChannel, limit=None)[0].document if last_file: last_file = last_file.attributes[1].file_name audios = vkaudio.get(user_id) total = len(audios) if last_file: progress = [track['artist'] + ' - ' + track['title'] for track in audios[::-1]].index(last_file) + 1 if progress == total: print(f'[Done] Found {progress}/{total} tracks') exit() else: print(f'\nFound {progress}/{total} tracks, continue downloading...') print() progress += 1 for i, track in enumerate(audios[::-1]): if progress and i < progress - 1: continue filename = track['artist'] + ' - ' + track['title'] escaped_filename = filename.replace("/", "_") file_path = folderName + str(user_id) + '/' + escaped_filename + '.mp3' print(f'Downloading [{i + 1}/{total}]') print(filename) try: save(track['url'], file_path) except ssl.SSLError: print(f'SSL ERROR: {escaped_filename}, launching again...') try: save(track['url'], escaped_filename + '.mp3') except: print(f'Failed to save track after 2 tries [{i + 1}/{total}]') exit() print('\nUploading...') sys.stdout.flush() send_file( client, client.get_entity(VKMusicChannel), file_path, track['duration'], track['title'], track['artist'], filename ) if not store_local: os.remove(file_path) print() sys.stdout.flush()
def main(): store_local = input('Do you want to leave the local files? [N/y] ') in ['y', 'yes'] folderName = 'Music ' vkaudio, user_id = auth_vk() client = auth_tg() VKMusicChannel = None progress = 0 _, entities = client.get_dialogs(limit=100) for e in entities: if type(e) == Channel and e.title == 'VKMusic': VKMusicChannel = e if VKMusicChannel is None: VKMusicChannel = client(CreateChannelRequest(title='VKMusic', about='made with https://github.com/HaCk3Dq/vktotg')).chats[0] client(EditPhotoRequest( InputChannel(VKMusicChannel.id, VKMusicChannel.access_hash), InputChatUploadedPhoto(client.upload_file('music.jpg')) )) client.delete_messages(client.get_entity(VKMusicChannel), 2) else: progress = client.get_message_history(VKMusicChannel)[0] print('\nFound ' + str(progress) + ' tracks, continue downloading...') offset = 0 audios = [] last_chunk = [] chunk = None while chunk != last_chunk: last_chunk = chunk chunk = vkaudio.get(user_id, None, offset) audios.extend(chunk) offset += 50 total = len(audios) print() for i, track in enumerate(audios[::-1]): if progress and i < progress-1: continue filename = track['artist'] + ' - ' + track['title'] escaped_filename = filename.replace("/","_") file_path = folderName + str(user_id) + '/' + escaped_filename +'.mp3' print('Downloading [' + str(i+1) + '/' + str(total) + ']') try: save(track['url'], file_path) except HTTPError: print('ERROR: ' + escaped_filename) except ssl.SSLError: print('SSL ERROR: ' + escaped_filename + ', launching again...') try: save(track['url'], escaped_filename +'.mp3') except: print('Failed to save track after 2 tries [' + str(i+1) + '/' + str(total) + ']') exit() print('\nUploading...') sys.stdout.flush() try: send_file( client, client.get_entity(VKMusicChannel), file_path, track['dur'], track['title'], track['artist'], filename ) except: print('Failed to send track ' + str(i) + ', try again') exit() if not store_local: os.remove(file_path) print() sys.stdout.flush() client.disconnect()