diff --git a/main.py b/main.py index 562c249..1625a14 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,10 @@ -from datetime import datetime import os +import re import time +from datetime import datetime +from pathlib import Path from zoneinfo import ZoneInfo -import pyperclip import requests from dotenv import load_dotenv @@ -13,6 +14,7 @@ API_TOKEN = os.environ["API_TOKEN"] EMOJI_FOLDER_ID = "a8qkigygjv" MAX_CHECK_NEW_FILE = 10 TIMEZONE = ZoneInfo("Asia/Tokyo") +FILES_SRC = Path(os.environ["FILES_SRC"]) def admin_emoji_list(since_id: str | None = None, until_id: str | None = None): @@ -21,9 +23,9 @@ def admin_emoji_list(since_id: str | None = None, until_id: str | None = None): "limit": 100, } if since_id is not None: - request_body["since_id"] = since_id + request_body["sinceId"] = since_id if until_id is not None: - request_body["until_id"] = until_id + request_body["untilId"] = until_id return requests.post( "https://misskey.qwjyh.net/api/admin/emoji/list", headers={ @@ -49,6 +51,23 @@ def drive_files_upload_from_url(url: str): ) +def drive_files_create(file_path: str | os.PathLike, name: str): + return requests.post( + "https://misskey.qwjyh.net/api/drive/files/create", + headers={ + # "Content-Type": "multipart/form-data", + "Authorization": f"Bearer {API_TOKEN}", + }, + files={ + "file": open(file_path, "rb"), + }, + data={ + "folderId": EMOJI_FOLDER_ID, + "name": name, + }, + ) + + def check_file_exists(emoji: dict) -> bool: r = requests.get(emoji["url"]) return r.status_code == requests.codes.ok @@ -82,17 +101,24 @@ def admin_emoji_update(emoji_id: str, new_file_id: str): def main(): print(f"{API_TOKEN=}") print("Hello from misskey-update-emoji-files!") - r = admin_emoji_list() + r = admin_emoji_list(until_id="9h9zv01kcr") print(f"out body={r.json()}") r.raise_for_status() for emoji in r.json(): file_exists = check_file_exists(emoji) if not file_exists: print(f"{emoji=}") - pyperclip.copy(emoji['name']) + m = re.search(r"[\da-f\-]+$", emoji["url"]) + if m is None: + raise Exception(f"Failed to get file name from URL: {emoji['url']}") + source_file = FILES_SRC / m.group() + if not source_file.exists(): + raise Exception(f"file doesn't exist: {source_file=}") + print(f"Uploading file from {source_file}") + before_upload = datetime.now(tz=TIMEZONE) - new_url = input("enter new file url:") - r = drive_files_upload_from_url(new_url) + r = drive_files_create(source_file, emoji["name"]) + print(f"{r=}") r.raise_for_status() for i_get_file_iter in range(MAX_CHECK_NEW_FILE): time.sleep(1) diff --git a/pyproject.toml b/pyproject.toml index 5799b63..d765c48 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,6 @@ readme = "README.md" requires-python = ">=3.13" dependencies = [ "bpython>=0.25", - "pyperclip>=1.9.0", "python-dotenv>=1.1.0", "requests>=2.32.3", ] diff --git a/uv.lock b/uv.lock index 3053e6d..99b0817 100644 --- a/uv.lock +++ b/uv.lock @@ -151,7 +151,6 @@ version = "0.1.0" source = { virtual = "." } dependencies = [ { name = "bpython" }, - { name = "pyperclip" }, { name = "python-dotenv" }, { name = "requests" }, ] @@ -159,7 +158,6 @@ dependencies = [ [package.metadata] requires-dist = [ { name = "bpython", specifier = ">=0.25" }, - { name = "pyperclip", specifier = ">=1.9.0" }, { name = "python-dotenv", specifier = ">=1.1.0" }, { name = "requests", specifier = ">=2.32.3" }, ] @@ -173,12 +171,6 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/8a/0b/9fcc47d19c48b59121088dd6da2488a49d5f72dacf8262e2790a1d2c7d15/pygments-2.19.1-py3-none-any.whl", hash = "sha256:9ea1544ad55cecf4b8242fab6dd35a93bbce657034b0611ee383099054ab6d8c", size = 1225293, upload-time = "2025-01-06T17:26:25.553Z" }, ] -[[package]] -name = "pyperclip" -version = "1.9.0" -source = { registry = "https://pypi.org/simple" } -sdist = { url = "https://files.pythonhosted.org/packages/30/23/2f0a3efc4d6a32f3b63cdff36cd398d9701d26cda58e3ab97ac79fb5e60d/pyperclip-1.9.0.tar.gz", hash = "sha256:b7de0142ddc81bfc5c7507eea19da920b92252b548b96186caf94a5e2527d310", size = 20961, upload-time = "2024-06-18T20:38:48.401Z" } - [[package]] name = "python-dotenv" version = "1.1.0"