upload.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import os
  2. import json
  3. import requests
  4. apiAddress = "http://127.0.0.1:8081/"
  5. TELEGRAM_TOKEN = os.getenv("TELEGRAM_TOKEN", "")
  6. CHAT_ID = os.environ.get("CHAT_ID", "")
  7. urlPrefix = apiAddress + "bot" + TELEGRAM_TOKEN
  8. def get_caption() -> str:
  9. with open("caption.txt", "r", encoding="utf-8") as f:
  10. return f.read()
  11. def genFileDirectory(path):
  12. files = {}
  13. media = []
  14. for root, dirs, file_name_dic in os.walk(path):
  15. for fileName in sorted(file_name_dic):
  16. files[fileName] = open(path + "/" + fileName, "rb")
  17. media.append(dict(type='document', media=f'attach://{fileName}'))
  18. media[-1]['caption'] = get_caption()
  19. media[-1]['parse_mode'] = "Markdown"
  20. return media, files
  21. def sendAPKs(path):
  22. media, files = genFileDirectory(path)
  23. parma = {
  24. "chat_id": CHAT_ID,
  25. "media": json.dumps(media)
  26. }
  27. response = requests.post(urlPrefix + "/sendMediaGroup", params=parma, files=files)
  28. print(response.json())
  29. if __name__ == "__main__":
  30. apk_path = "./apk"
  31. sendAPKs(apk_path)