From 62558addcd6bd7a957b5850ceab93a4ef29d39d4 Mon Sep 17 00:00:00 2001 From: sunwoo1524 Date: Thu, 4 Jul 2024 18:11:04 +0900 Subject: [PATCH] migrate to mastodon version --- .dockerignore | 2 ++ .example.env | 8 +++----- .gitignore | 2 +- Dockerfile | 6 +----- README.md | 36 +++++++++++++++++++++------------- docker-compose.example.yml | 9 ++++----- main.py | 16 +++++++-------- requirements.txt | 5 ++--- src/choose_sentence.py | 40 ++++++++++++++++++++------------------ 9 files changed, 65 insertions(+), 59 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..4ea05a1 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +venv/ +__pycache__/ \ No newline at end of file diff --git a/.example.env b/.example.env index 5b6f7c2..5c4f974 100644 --- a/.example.env +++ b/.example.env @@ -1,5 +1,3 @@ -MISSKEY_INSTANCE_ADDRESS="worldc.one" -MISSKEY_ACCESS_TOKEN="BWbGlK6lWuUgXQpFM7igtmbZ30g6BOIc" # it is just a sample - -SENTENCES_FILES=['./data/sentences/default.txt'] # Putting an absolute path is recommended -# SENTENCES_DIRECTORIES=['./data/sentences/'] \ No newline at end of file +MASTODON_INSTANCE_ADDRESS=mastodon.social +SENTENCES_DIRECTORIES=['./data/sentences/'] +WORDS_DIRECTORIES=["./data/words/"] \ No newline at end of file diff --git a/.gitignore b/.gitignore index 799382f..b76fcf7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ venv/ setting.py -google_api.json __pycache__/ .env docker-compose.yml +token.secret \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index ce3e725..4a6fdaf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,12 +2,8 @@ FROM python:bookworm WORKDIR /app -COPY src src -COPY main.py main.py -COPY requirements.txt requirements.txt +COPY . . -# RUN python -m venv venv -# RUN source ./venv/bin/activate RUN pip install -r requirements.txt CMD ["python", "main.py"] \ No newline at end of file diff --git a/README.md b/README.md index d2e9370..233d02f 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,33 @@ -# Misskey Random Sentence Bot +# Mastodon Random Sentence Bot [![no github badge](https://nogithub.codeberg.page/badge.svg)](https://nogithub.codeberg.page/) +A mastodon bot posting random sentences. Forked from [Misskey Random Sentence Bot](https://git.worldc.one/worldcone/misskey-random-sentence-bot) + ## How to run with docker compose 1. clone repo + 2. change setting -``` -# copy example compose file -cp docker-compose.example.yml docker-compose.env +```bash +# copy compose file +cp docker-compose.example.yml docker-compose.yml -# edit compose file -vim .docker-compose +# copy and edit .env +cp .example.env .env +vim .env ``` -3. RUN - +3. add your access token +```bash +touch token.secret +vim token.secret ``` + +4. RUN + +```bash docker compose up docker compose down ``` @@ -28,20 +38,20 @@ docker compose down 2. make venv -``` +```bash python3 -m venv venv source ./venv/bin/activate ``` 3. install modules -``` +```bash pip install -r requirements.txt ``` -4. change setting +4. change setting(changing the sentences and words directories as a absolute path is recommended) -``` +```bash # copy example setting file cp .example.env .env @@ -51,7 +61,7 @@ vim .env 5. RUN -``` +```bash python main.py ``` diff --git a/docker-compose.example.yml b/docker-compose.example.yml index e5f9376..1a8eb9e 100644 --- a/docker-compose.example.yml +++ b/docker-compose.example.yml @@ -6,9 +6,8 @@ services: context: . dockerfile: ./Dockerfile environment: - - MISSKEY_INSTANCE_ADDRESS=worldc.one - - MISSKEY_ACCESS_TOKEN=BWbGlK6lWuUgXQpFM7igtmbZ30g6BOIc - # - SENTENCES_FILES=['/data/sentences/default.txt'] - - SENTENCES_DIRECTORIES=['/data/sentences/'] + - MASTODON_INSTANCE_ADDRESS=${MASTODON_INSTANCE_ADDRESS} + - SENTENCES_DIRECTORIES=${SENTENCES_DIRECTORIES} + - WORDS_DIRECTORIES=${WORDS_DIRECTORIES} volumes: - - ./data:/data + - ./data:/app/data diff --git a/main.py b/main.py index e432403..7014264 100644 --- a/main.py +++ b/main.py @@ -1,16 +1,16 @@ import os import random -from misskey import Misskey +from mastodon import Mastodon from dotenv import load_dotenv from src.choose_sentence import chooseSentence from src.generate_sentence import generateSentence -load_dotenv() +load_dotenv() # authentication to misskey -misskey = Misskey(address=os.environ["MISSKEY_INSTANCE_ADDRESS"], i=os.environ["MISSKEY_ACCESS_TOKEN"]) +mastodon = Mastodon(access_token="token.secret", api_base_url=os.environ["MASTODON_INSTANCE_ADDRESS"]) def writeRandomSentenceNote(): @@ -22,12 +22,12 @@ def writeRandomSentenceNote(): # choose method and get sentence sentence: str = random.choice(choice_methods)() - # write note - random_sentence_note = misskey.notes_create(text=sentence) - note_id = random_sentence_note["createdNote"]["id"] - note_text = random_sentence_note["createdNote"]["text"] - print(f"{note_id} | {note_text}") + toot = mastodon.toot(sentence) + toot_url = toot["url"] + toot_content = toot["content"] + toot_created_at = toot["created_at"] + print(f"{toot_created_at} {toot_url} : {toot_content}") if __name__ == "__main__": diff --git a/requirements.txt b/requirements.txt index fe92f12..c88cd36 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,2 @@ -Misskey.py -python-dotenv - +Mastodon.py +python-dotenv \ No newline at end of file diff --git a/src/choose_sentence.py b/src/choose_sentence.py index c52511a..9b4daf0 100644 --- a/src/choose_sentence.py +++ b/src/choose_sentence.py @@ -8,32 +8,34 @@ from dotenv import load_dotenv load_dotenv() -_sentences=[] -try: - sentence_files=[] +_sentences: list[str] =[] - if os.environ.get("SENTENCES_FILE") and os.path.isfile(os.environ["SENTENCES_FILE"]): - sentence_files.append(os.environ["SENTENCE_FILES"]) +# try: +sentence_files=[] - if os.environ.get("SENTENCES_FILES"): - sentence_files.extend(list(filter(os.path.isfile, json.loads(os.environ["SENTENCES_FILES"])))) +# if os.environ.get("SENTENCES_FILE") and os.path.isfile(os.environ["SENTENCES_FILE"]): +# sentence_files.append(os.environ["SENTENCE_FILES"]) - if os.environ.get("SENTENCES_DIRECTORY") and os.path.isdir(os.environ["SENTENCES_DIRECTORY"]): - sentence_files.extend(glob(f'{os.environ["SENTENCES_DIRECTORY"]}*.txt')) +# if os.environ.get("SENTENCES_FILES"): +# sentence_files.extend(list(filter(os.path.isfile, json.loads(os.environ["SENTENCES_FILES"])))) - if os.environ.get("SENTENCES_DIRECTORIES"): - # 원라인 똥 뿌직(터져도 책임 안짐) - sentence_files.extend([glob(f"{_dir}*.txt") for _dir in filter(os.path.isdir, json.loads(os.environ["SENTENCES_DIRECTORIES"]))][0]) +# if os.environ.get("SENTENCES_DIRECTORY") and os.path.isdir(os.environ["SENTENCES_DIRECTORY"]): +# sentence_files.extend(glob(f'{os.environ["SENTENCES_DIRECTORY"]}*.txt')) + +# if os.environ.get("SENTENCES_DIRECTORIES"): + +# get the directories including the sentences files +sentence_files.extend([glob(f"{_dir}*.txt") for _dir in filter(os.path.isdir, json.loads(os.environ["SENTENCES_DIRECTORIES"]))][0]) + +for sentence_file in sentence_files: + with open(sentence_file, "r") as f: + _sentences.extend(f.readlines()) +# except: +# e = sys.exc_info()[1] +# _sentences = [f"Runtime error(Please contact to the admin): {e}"] - for sentence_file in sentence_files: - with open(sentence_file, "r") as f: - _sentences.extend(f.readlines()) -except: - e = sys.exc_info()[1] - _sentences = [f"에러발생(진짜임): {e}"] # choose random sentence in google spread sheet def chooseSentence() -> str: # choice random sentence return random.choice(_sentences).replace("\\n","\n") -