Support for uploading to rooms

This commit is contained in:
Natty 2023-11-18 23:32:51 +01:00
parent 27eb290d5e
commit 7b2e88865c
Signed by: natty
GPG Key ID: BF6CB659ADEE60EC
2 changed files with 37 additions and 7 deletions

View File

@ -17,13 +17,24 @@ Run as a CLI command:
* Please note the handle has to be in the format `@user:instance.org`
```
python main.py [-h] --tag TAG --token TOKEN --instance INSTANCE emoji_file [emoji_file ...]
python main.py [-h] [--tag TAG | --room-id ROOM_ID] [--room-event-id ROOM_EVENT_ID] --token TOKEN --instance INSTANCE emoji_file [emoji_file ...]
positional arguments:
emoji_file a list of files to upload
options:
// Upload to the personal emoji pack:
--tag TAG the Matrix handle of the user
// Or upload to a room emoji pack:
--room-id ROOM_ID the Matrix room to upload the emoji in
--room-event-id ROOM_EVENT_ID
the Matrix room event ID the pack is stored in,
leave empty for the default one
// Required options:
--token TOKEN a Matrix account bearer token
--instance INSTANCE a Matrix instance domain
```

31
main.py
View File

@ -18,7 +18,9 @@ from tqdm import tqdm
@dataclass
class Args:
files: list[str]
tag: str
tag: typing.Optional[str]
room_id: typing.Optional[str]
room_event_id: typing.Optional[str]
token: str
instance: str
@ -48,8 +50,18 @@ class DataclassEncoder(json.JSONEncoder):
async def upload_emoji(args: Args):
handle_urlenc: str = urllib.parse.quote(args.tag, safe="")
api_url: str = f"https://{args.instance}/_matrix/client/r0/user/{handle_urlenc}/account_data/im.ponies.user_emotes"
client_api_base: str = f"https://{args.instance}/_matrix/client"
if args.tag:
handle_urlenc: str = urllib.parse.quote(args.tag, safe="")
api_url: str = f"{client_api_base}/r0/user/{handle_urlenc}/account_data/im.ponies.user_emotes"
elif args.room_id:
room_urlenc: str = urllib.parse.quote(args.room_id, safe="")
room_event_urlenc: str = urllib.parse.quote(args.room_event_id, safe="")
api_url: str = f"{client_api_base}/r0/rooms/{room_urlenc}/state/im.ponies.room_emotes/{room_event_urlenc}"
else:
print("Neither the user nor room option have been selected", file=sys.stderr)
return
async with aiohttp.ClientSession(headers={
"Authorization": f"Bearer {args.token}"
@ -109,10 +121,17 @@ if __name__ == "__main__":
nargs="+",
help="a list of files to upload")
parser.add_argument("--tag",
ex = parser.add_mutually_exclusive_group()
ex.add_argument("--tag",
type=str,
help="the Matrix handle of the user")
ex.add_argument("--room-id",
type=str,
help="the Matrix room to upload the emoji in")
parser.add_argument("--room-event-id",
type=str,
required=True,
help="the Matrix handle of the user")
default="",
help="the Matrix room event ID the pack is stored in")
parser.add_argument("--token",
type=str,