text: # Copyright (C) 2020 The Raphielscape Company LLC.
  #
  # Licensed under the Raphielscape Public License, Version 1.d (the "License");
  # you may not use this file except in compliance with the License.
  #
  # Port from UniBorg to Userbot by yincen17
  
  import asyncio
  import os
  import time
  import zipfile
  from datetime import date
  
  from userbot import CMD_HELP, TEMP_DOWNLOAD_DIRECTORY, ZIP_DOWNLOAD_DIRECTORY, bot
  from userbot.events import register
  from userbot.utils import progress
  
  # ====================
  today = date.today()
  # ====================
  
  
  @register(outgoing=True, pattern=r"^\.compress(?: |$)(.*)")
  async def _(event):
      # Prevent Channel Bug to use update
      if event.is_channel and not event.is_group:
          await event.edit("`Comando de compressão não é permitido em canais`")
          return
      if event.fwd_from:
          return
      if not event.is_reply:
          await event.edit("`Responda a um arquivo para compactá-lo.`")
          return
      mone = await event.edit("`Processando...`")
      if not os.path.isdir(TEMP_DOWNLOAD_DIRECTORY):
          os.makedirs(TEMP_DOWNLOAD_DIRECTORY)
      if event.reply_to_msg_id:
          reply_message = await event.get_reply_message()
          try:
              c_time = time.time()
              downloaded_file_name = await bot.download_media(
                  reply_message,
                  TEMP_DOWNLOAD_DIRECTORY,
                  progress_callback=lambda d, t: asyncio.get_event_loop().create_task(
                      progress(d, t, mone, c_time, "[DOWNLOADING]")
                  ),
              )
              directory_name = downloaded_file_name
              await event.edit(
                  f"Baixado para `{directory_name}`" "`\ncompactando arquivo...`"
              )
          except Exception as e:  # pylint:disable=C0103,W0703
              await mone.edit(str(e))
      zipfile.ZipFile(directory_name + ".zip", "w", zipfile.ZIP_DEFLATED).write(
          directory_name
      )
      c_time = time.time()
      await bot.send_file(
          event.chat_id,
          directory_name + ".zip",
          force_document=True,
          allow_cache=False,
          reply_to=event.message.id,
          progress_callback=lambda d, t: asyncio.get_event_loop().create_task(
              progress(d, t, mone, c_time, "[UPLOADING]")
          ),
      )
      await event.edit("`Feito!!`")
      await asyncio.sleep(7)
      await event.delete()
  
  
  @register(outgoing=True, pattern=r"^\.addzip(?: |$)(.*)")
  async def addzip(add):
      """ Copyright (c) 2020 azrim @github"""
      # Prevent Channel Bug to use update
      if add.is_channel and not add.is_group:
          await add.edit("`O comando não é permitido em canais`")
          return
      if add.fwd_from:
          return
      if not add.is_reply:
          await add.edit("`Responda a um arquivo para compactá-lo.`")
          return
      mone = await add.edit("`Processando...`")
      if not os.path.isdir(ZIP_DOWNLOAD_DIRECTORY):
          os.makedirs(ZIP_DOWNLOAD_DIRECTORY)
      if add.reply_to_msg_id:
          reply_message = await add.get_reply_message()
          try:
              c_time = time.time()
              downloaded_file_name = await bot.download_media(
                  reply_message,
                  ZIP_DOWNLOAD_DIRECTORY,
                  progress_callback=lambda d, t: asyncio.get_event_loop().create_task(
                      progress(d, t, mone, c_time, "[DOWNLOADING]")
                  ),
              )
              success = str(downloaded_file_name).replace("./zips/", "")
              await add.edit(f"`{success} Adicionado com sucesso à lista`")
          except Exception as e:  # pylint:disable=C0103,W0703
              await mone.edit(str(e))
              return
  
  
  @register(outgoing=True, pattern=r"^\.upzip(?: |$)(.*)")
  async def upload_zip(up):
      if not os.path.isdir(ZIP_DOWNLOAD_DIRECTORY):
          await up.edit("`Arquivos não encontrados`")
          return
      mone = await up.edit("`Compactando Arquivo...`")
      input_str = up.pattern_match.group(1)
      curdate = today.strftime("%m%d%y")
      title = str(input_str) if input_str else "zipfile" + f"{curdate}"
      zipf = zipfile.ZipFile(title + ".zip", "w", zipfile.ZIP_DEFLATED)
      zipdir(ZIP_DOWNLOAD_DIRECTORY, zipf)
      zipf.close()
      c_time = time.time()
      await bot.send_file(
          up.chat_id,
          title + ".zip",
          force_document=True,
          allow_cache=False,
          reply_to=up.message.id,
          progress_callback=lambda d, t: asyncio.get_event_loop().create_task(
              progress(d, t, mone, c_time, "[UPLOADING]", input_str)
          ),
      )
      os.rmdir(ZIP_DOWNLOAD_DIRECTORY)
      await up.delete()
  
  
  @register(outgoing=True, pattern=r"^\.rmzip(?: |$)(.*)")
  async def remove_dir(rm):
      if not os.path.isdir(ZIP_DOWNLOAD_DIRECTORY):
          await rm.edit("`Diretório não encontrado`")
          return
      os.rmdir(ZIP_DOWNLOAD_DIRECTORY)
      await rm.edit("`Lista zip removida`")
  
  
  def zipdir(path, ziph):
      # ziph is zipfile handle
      for root, dirs, files in os.walk(path):
          for file in files:
              ziph.write(os.path.join(root, file))
              os.remove(os.path.join(root, file))
  
  
  CMD_HELP.update(
      {
          "zipfile": ".compress **[opcional: <responder ao arquivo>]**\
              \nUso: para compactar arquivos.\
              \n.addzip **<responder ao arquivo>**\
              \nUso: adicionar arquivos à lista zip.\
              \n.upzip **[opcional: <título zip>]**\
              \nUso: carregar lista zip.\
              \n.rmzip **[opcional: <título zip>]**\
              \nUso: limpar lista zip."
      }
  )
  