def __progress(data: dict): nonlocal edited, c_time diff = time() - c_time if data['status'] == "downloading" and ( not edited or diff >= Config.EDIT_SLEEP_TIMEOUT): c_time = time() edited = True eta = data.get('eta') speed = data.get('speed') if not (eta and speed): return out = "**Speed** >> {}/s\n**ETA** >> {}\n".format( humanbytes(speed), time_formatter(eta)) out += f'**File Name** >> `{data["filename"]}`\n\n' current = data.get('downloaded_bytes') total = data.get("total_bytes") if current and total: percentage = int(current) * 100 / int(total) out += f"Progress >> {int(percentage)}%\n" out += "[{}{}]".format( ''.join((Config.FINISHED_PROGRESS_STR for _ in range(floor(percentage / 5)))), ''.join( (Config.UNFINISHED_PROGRESS_STR for _ in range(20 - floor(percentage / 5))))) xdecha.loop.create_task(message.edit(out))
async def handle_afk_incomming(message: Message) -> None: """ handle incomming messages when you afk """ user_id = message.from_user.id chat = message.chat user_dict = await message.client.get_user_dict(user_id) afk_time = time_formatter(round(time.time() - TIME)) coro_list = [] if user_id in USERS: if not (USERS[user_id][0] + USERS[user_id][1]) % randint(2, 4): if REASON: out_str = (f"I'm still **AFK**.\nReason: <code>{REASON}</code>\n" f"Last Seen: `{afk_time} ago`") else: out_str = choice(AFK_REASONS) coro_list.append(message.reply(out_str)) if chat.type == 'private': USERS[user_id][0] += 1 else: USERS[user_id][1] += 1 else: if REASON: out_str = (f"I'm **AFK** right now.\nReason: <code>{REASON}</code>\n" f"Last Seen: `{afk_time} ago`") else: out_str = choice(AFK_REASONS) coro_list.append(message.reply(out_str)) if chat.type == 'private': USERS[user_id] = [1, 0, user_dict['mention']] else: USERS[user_id] = [0, 1, user_dict['mention']] if chat.type == 'private': coro_list.append(CHANNEL.log( f"#PRIVATE\n{user_dict['mention']} send you\n\n" f"{message.text}")) else: coro_list.append(CHANNEL.log( "#GROUP\n" f"{user_dict['mention']} tagged you in [{chat.title}](http://t.me/{chat.username})\n\n" f"{message.text}\n\n" f"[goto_msg](https://t.me/c/{str(chat.id)[4:]}/{message.message_id})")) coro_list.append(AFK_COLLECTION.update_one({'_id': user_id}, {"$set": { 'pcount': USERS[user_id][0], 'gcount': USERS[user_id][1], 'men': USERS[user_id][2]}}, upsert=True)) await asyncio.gather(*coro_list)
def _download_file(self, path: str, name: str, **kwargs) -> None: request = self._service.files().get_media(fileId=kwargs['id'], supportsTeamDrives=True) with io.FileIO(os.path.join(path, name), 'wb') as d_f: d_file_obj = MediaIoBaseDownload(d_f, request, chunksize=50 * 1024 * 1024) c_time = time.time() done = False while done is False: status, done = d_file_obj.next_chunk(num_retries=5) if self._is_canceled: raise ProcessCanceled if status: f_size = status.total_size diff = time.time() - c_time downloaded = status.resumable_progress percentage = downloaded / f_size * 100 speed = round(downloaded / diff, 2) eta = round((f_size - downloaded) / speed) tmp = \ "__Downloading From GDrive...__\n" + \ "```[{}{}]({}%)```\n" + \ "**File Name** : `{}`\n" + \ "**File Size** : `{}`\n" + \ "**Downloaded** : `{}`\n" + \ "**Completed** : `{}/{}`\n" + \ "**Speed** : `{}/s`\n" + \ "**ETA** : `{}`" self._progress = tmp.format( "".join((Config.FINISHED_PROGRESS_STR for _ in range(math.floor(percentage / 5)))), "".join( (Config.UNFINISHED_PROGRESS_STR for _ in range(20 - math.floor(percentage / 5)))), round(percentage, 2), name, humanbytes(f_size), humanbytes(downloaded), self._completed, self._list, humanbytes(speed), time_formatter(eta)) self._completed += 1 _LOG.info("Downloaded Google-Drive File => Name: %s ID: %s", name, kwargs['id'])
async def handle_afk_outgoing(message: Message) -> None: """ handle outgoing messages when you afk """ global IS_AFK # pylint: disable=global-statement IS_AFK = False afk_time = time_formatter(round(time.time() - TIME)) replied: Message = await message.reply("`I'm no longer AFK!`", log=__name__) coro_list = [] if USERS: p_msg = '' g_msg = '' p_count = 0 g_count = 0 for pcount, gcount, men in USERS.values(): if pcount: p_msg += f"👤 {men} ✉️ **{pcount}**\n" p_count += pcount if gcount: g_msg += f"👥 {men} ✉️ **{gcount}**\n" g_count += gcount coro_list.append(replied.edit( f"`You recieved {p_count + g_count} messages while you were away. " f"Check log for more details.`\n\n**AFK time** : __{afk_time}__", del_in=3)) out_str = f"You've recieved **{p_count + g_count}** messages " + \ f"from **{len(USERS)}** users while you were away!\n\n**AFK time** : __{afk_time}__\n" if p_count: out_str += f"\n**{p_count} Private Messages:**\n\n{p_msg}" if g_count: out_str += f"\n**{g_count} Group Messages:**\n\n{g_msg}" coro_list.append(CHANNEL.log(out_str)) USERS.clear() else: await asyncio.sleep(3) coro_list.append(replied.delete()) coro_list.append(asyncio.gather( AFK_COLLECTION.drop(), SAVED_SETTINGS.update_one( {'_id': 'AFK'}, {"$set": {'on': False}}, upsert=True))) await asyncio.gather(*coro_list)
def uptime(self) -> str: """ returns xdecha uptime """ return time_formatter(time.time() - _START_TIME)
def eta(self) -> str: """ Returns eta """ return time_formatter((self._file_size - self._cmp_size) / self.speed if self.speed else 0)
def _upload_file(self, file_path: str, parent_id: str) -> str: if self._is_canceled: raise ProcessCanceled mime_type = guess_type(file_path)[0] or "text/plain" file_name = os.path.basename(file_path) file_size = os.path.getsize(file_path) body = { "name": file_name, "mimeType": mime_type, "description": "Uploaded using xdecha" } if parent_id: body["parents"] = [parent_id] if file_size == 0: media_body = MediaFileUpload(file_path, mimetype=mime_type, resumable=False) u_file_obj = self._service.files().create( body=body, media_body=media_body, supportsTeamDrives=True).execute() file_id = u_file_obj.get("id") else: media_body = MediaFileUpload(file_path, mimetype=mime_type, chunksize=50 * 1024 * 1024, resumable=True) u_file_obj = self._service.files().create(body=body, media_body=media_body, supportsTeamDrives=True) c_time = time.time() response = None while response is None: status, response = u_file_obj.next_chunk(num_retries=5) if self._is_canceled: raise ProcessCanceled if status: f_size = status.total_size diff = time.time() - c_time uploaded = status.resumable_progress percentage = uploaded / f_size * 100 speed = round(uploaded / diff, 2) eta = round((f_size - uploaded) / speed) tmp = \ "__Uploading to GDrive...__\n" + \ "```[{}{}]({}%)```\n" + \ "**File Name** : `{}`\n" + \ "**File Size** : `{}`\n" + \ "**Uploaded** : `{}`\n" + \ "**Completed** : `{}/{}`\n" + \ "**Speed** : `{}/s`\n" + \ "**ETA** : `{}`" self._progress = tmp.format( "".join((Config.FINISHED_PROGRESS_STR for _ in range(math.floor(percentage / 5)))), "".join( (Config.UNFINISHED_PROGRESS_STR for _ in range(20 - math.floor(percentage / 5)))), round(percentage, 2), file_name, humanbytes(f_size), humanbytes(uploaded), self._completed, self._list, humanbytes(speed), time_formatter(eta)) file_id = response.get("id") if not Config.G_DRIVE_IS_TD: self._set_permission(file_id) self._completed += 1 _LOG.info("Created Google-Drive File => Name: %s ID: %s Size: %s", file_name, file_id, file_size) return file_id