telegram 群组监控 / 群组功能
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.8 KiB

8 months ago
  1. import aiohttp
  2. import asyncio
  3. import json
  4. from aiohttp import FormData
  5. from loguru import logger
  6. from config import UPLOAD_URL
  7. async def upload_file(file_path, url=UPLOAD_URL):
  8. """
  9. :param file_path:
  10. :param url:
  11. :return:
  12. """
  13. if file_path:
  14. async with aiohttp.ClientSession() as session:
  15. data = FormData()
  16. data.add_field("file",open(file_path, 'rb'),content_type='multipart/form-data;charset=utf-8"')
  17. data.add_field("output", "json")
  18. async with session.post(url, data=data) as response:
  19. result = await response.text() # 返回结果为json字符串
  20. result = json.loads(result)
  21. # logger.info(f"upload file {result}")
  22. if 'src' in result.keys():
  23. video_path = result['src']
  24. return video_path
  25. async def download_file(url, filename):
  26. """
  27. :param url:
  28. :return:
  29. """
  30. async with aiohttp.ClientSession() as session:
  31. logger.info(f"Starting download file from {url}")
  32. async with session.get(url) as response:
  33. assert response.status == 200
  34. with open(filename, "wb") as f:
  35. while True:
  36. chunk = await response.content.readany()
  37. if not chunk:
  38. break
  39. f.write(chunk)
  40. logger.info(f"Downloaded {filename} from {url}")
  41. return filename
  42. if __name__ == '__main__':
  43. loop = asyncio.get_event_loop()
  44. # loop.run_until_complete(upload_file("resources/photo_2024-01-17_02-05-00.jpg"))
  45. loop.run_until_complete(download_file("http://172.18.1.180:9980/group17/default/20240507/17/07/3/image.jpg", "../resources/image.jpg"))