upload.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # mautrix-instagram - A Matrix-Instagram puppeting bridge.
  2. # Copyright (C) 2020 Tulir Asokan
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. from typing import Optional, Dict, Any
  17. from uuid import uuid4
  18. import random
  19. import time
  20. import json
  21. from .base import BaseAndroidAPI
  22. from ..types import UploadPhotoResponse
  23. class UploadAPI(BaseAndroidAPI):
  24. async def upload_jpeg_photo(self, data: bytes, upload_id: Optional[str] = None,
  25. is_sidecar: bool = False, waterfall_id: Optional[str] = None,
  26. media_type: int = 1) -> UploadPhotoResponse:
  27. upload_id = upload_id or str(time.time())
  28. name = f"{upload_id}_0_{random.randint(1000000000, 9999999999)}"
  29. params = {
  30. "retry_context": json.dumps(
  31. {"num_step_auto_retry": 0, "num_reupload": 0, "num_step_manual_retry": 0}),
  32. # TODO enum?
  33. "media_type": str(media_type),
  34. "upload_id": upload_id,
  35. "xsharing_user_ids": json.dumps([]),
  36. "image_compression": json.dumps(
  37. {"lib_name": "moz", "lib_version": "3.1.m", "quality": 80}),
  38. }
  39. if is_sidecar:
  40. params["is_sidecar"] = "1"
  41. headers = {
  42. "X_FB_PHOTO_WATERFALL_ID": waterfall_id or str(uuid4()),
  43. "X-Entity-Type": "image/jpeg",
  44. "Offset": "0",
  45. "X-Instagram-Rupload-Params": json.dumps(params),
  46. "X-Entity-Name": name,
  47. "X-Entity-Length": str(len(data)),
  48. "Content-Type": "application/octet-stream",
  49. }
  50. return await self.std_http_post(f"/rupload_igphoto/{name}", headers=headers, data=data,
  51. raw=True, response_type=UploadPhotoResponse)