private.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. from pyrogram import Client, filters, types
  2. from zipfile import ZipFile
  3. from os import remove, rmdir, mkdir
  4. from utils import zip_work, dir_work, up_progress, list_dir, Msg, db_session, User, commit
  5. @Client.on_message(filters.command("start"))
  6. def start(_, msg: types.Message):
  7. """ reply start message and add the user to database """
  8. uid = msg.from_user.id
  9. with db_session:
  10. if not User.get(uid=uid):
  11. User(uid=uid, status=0) # Initializing the user on database
  12. commit()
  13. msg.reply(Msg.start(msg))
  14. @Client.on_message(filters.command("zip"))
  15. def start_zip(_, msg: types.Message):
  16. """ starting get files to archive """
  17. uid = msg.from_user.id
  18. msg.reply(Msg.zip)
  19. with db_session:
  20. User.get(uid=uid).status = 1 # change user-status to "INSERT"
  21. commit()
  22. try:
  23. mkdir(dir_work(uid)) # create static-folder for user
  24. except FileExistsError: # in case the folder already exist
  25. for file in list_dir(uid):
  26. remove(dir_work(uid) + file) # delete all file from folder
  27. rmdir(dir_work(uid)) # delete folder
  28. mkdir(dir_work(uid))
  29. @Client.on_message(filters.media)
  30. def enter_files(_, msg: types.Message):
  31. """ download files """
  32. uid = msg.from_user.id
  33. with db_session:
  34. usr = User.get(uid=uid)
  35. if usr.status == 1: # check if user-status is "INSERT"
  36. type = msg.document or msg.video or msg.photo or msg.audio
  37. if type.file_size > 20000000:
  38. msg.reply(Msg.too_big)
  39. elif len(list_dir(uid)) > 20:
  40. msg.reply(Msg.too_much)
  41. else:
  42. downsts = msg.reply(Msg.downloading, True) # send status-download message
  43. msg.download(dir_work(uid))
  44. downsts.delete() # delete status-download message
  45. else:
  46. msg.reply(Msg.send_zip) # if user-status is not "INSERT"
  47. @Client.on_message(filters.command("stopzip"))
  48. def stop_zip(_, msg: types.Message):
  49. """ exit from insert mode and send the archive """
  50. uid = msg.from_user.id
  51. if len(msg.command) == 1:
  52. zip_path = zip_work(uid)
  53. else:
  54. zip_path = "static/" + msg.command[1] # costume zip-file name
  55. with db_session:
  56. usr = User.get(uid=uid)
  57. if usr.status == 1:
  58. usr.status = 0 # change user-status to "NOT-INSERT"
  59. commit()
  60. else:
  61. msg.reply(Msg.send_zip)
  62. return
  63. stsmsg = msg.reply(Msg.zipping.format(len(list_dir(uid)))) # send status-message "ZIPPING" and count files
  64. if not list_dir(uid): # if len files is zero
  65. msg.reply(Msg.zero_files)
  66. rmdir(dir_work(uid))
  67. return
  68. for file in list_dir(uid):
  69. with ZipFile(zip_path, "a") as zip:
  70. zip.write(f"{dir_work(uid)}/{file}") # add files to zip-archive
  71. remove(f"{dir_work(uid)}{file}") # delete files that added
  72. stsmsg.edit_text(Msg.uploading) # change status-msg to "UPLOADING"
  73. try:
  74. msg.reply_document(zip_path, progress=up_progress, # send the zip-archive
  75. progress_args=(stsmsg,))
  76. except ValueError as e:
  77. msg.reply(Msg.unknow_error.format(str(e)))
  78. stsmsg.delete() # delete the status-msg
  79. remove(zip_path) # delete the zip-archive
  80. rmdir(dir_work(uid)) # delete the static-folder