Compare commits

..

No commits in common. "49507b0f1ccc12ef930f7b1b0853b6460f74f35a" and "5f2edec82666946651ffb5fb37b5f3951b7c05bb" have entirely different histories.

10 changed files with 68 additions and 72 deletions

View file

@ -1,2 +0,0 @@
venv/
__pycache__/

View file

@ -1,3 +1,5 @@
MASTODON_INSTANCE_ADDRESS=mastodon.social MISSKEY_INSTANCE_ADDRESS="worldc.one"
SENTENCES_DIRECTORIES=['./data/sentences/'] MISSKEY_ACCESS_TOKEN="BWbGlK6lWuUgXQpFM7igtmbZ30g6BOIc" # it is just a sample
WORDS_DIRECTORIES=["./data/words/"]
SENTENCES_FILES=['./data/sentences/default.txt'] # Putting an absolute path is recommended
# SENTENCES_DIRECTORIES=['./data/sentences/']

2
.gitignore vendored
View file

@ -1,6 +1,6 @@
venv/ venv/
setting.py setting.py
google_api.json
__pycache__/ __pycache__/
.env .env
docker-compose.yml docker-compose.yml
token.secret

View file

@ -2,8 +2,12 @@ FROM python:bookworm
WORKDIR /app WORKDIR /app
COPY . . COPY src src
COPY main.py main.py
COPY requirements.txt requirements.txt
# RUN python -m venv venv
# RUN source ./venv/bin/activate
RUN pip install -r requirements.txt RUN pip install -r requirements.txt
CMD ["python", "main.py"] CMD ["python", "main.py"]

View file

@ -1,33 +1,23 @@
# Mastodon Random Sentence Bot # Misskey Random Sentence Bot
[![no github badge](https://nogithub.codeberg.page/badge.svg)](https://nogithub.codeberg.page/) [![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 ## How to run with docker compose
1. clone repo 1. clone repo
2. change setting 2. change setting
```bash ```
# copy compose file # copy example compose file
cp docker-compose.example.yml docker-compose.yml cp docker-compose.example.yml docker-compose.env
# copy and edit .env # edit compose file
cp .example.env .env vim .docker-compose
vim .env
``` ```
3. add your access token 3. RUN
```bash
touch token.secret
vim token.secret
``` ```
4. RUN
```bash
docker compose up docker compose up
docker compose down docker compose down
``` ```
@ -38,20 +28,20 @@ docker compose down
2. make venv 2. make venv
```bash ```
python3 -m venv venv python3 -m venv venv
source ./venv/bin/activate source ./venv/bin/activate
``` ```
3. install modules 3. install modules
```bash ```
pip install -r requirements.txt pip install -r requirements.txt
``` ```
4. change setting(changing the sentences and words directories as a absolute path is recommended) 4. change setting
```bash ```
# copy example setting file # copy example setting file
cp .example.env .env cp .example.env .env
@ -61,7 +51,7 @@ vim .env
5. RUN 5. RUN
```bash ```
python main.py python main.py
``` ```

View file

@ -6,8 +6,9 @@ services:
context: . context: .
dockerfile: ./Dockerfile dockerfile: ./Dockerfile
environment: environment:
- MASTODON_INSTANCE_ADDRESS=${MASTODON_INSTANCE_ADDRESS} - MISSKEY_INSTANCE_ADDRESS=worldc.one
- SENTENCES_DIRECTORIES=${SENTENCES_DIRECTORIES} - MISSKEY_ACCESS_TOKEN=BWbGlK6lWuUgXQpFM7igtmbZ30g6BOIc
- WORDS_DIRECTORIES=${WORDS_DIRECTORIES} # - SENTENCES_FILES=['/data/sentences/default.txt']
- SENTENCES_DIRECTORIES=['/data/sentences/']
volumes: volumes:
- ./data:/app/data - ./data:/data

14
main.py
View file

@ -1,7 +1,7 @@
import os import os
import random import random
from mastodon import Mastodon from misskey import Misskey
from dotenv import load_dotenv from dotenv import load_dotenv
from src.choose_sentence import chooseSentence from src.choose_sentence import chooseSentence
@ -10,7 +10,7 @@ from src.generate_sentence import generateSentence
load_dotenv() load_dotenv()
# authentication to misskey # authentication to misskey
mastodon = Mastodon(access_token="token.secret", api_base_url=os.environ["MASTODON_INSTANCE_ADDRESS"]) misskey = Misskey(address=os.environ["MISSKEY_INSTANCE_ADDRESS"], i=os.environ["MISSKEY_ACCESS_TOKEN"])
def writeRandomSentenceNote(): def writeRandomSentenceNote():
@ -22,12 +22,12 @@ def writeRandomSentenceNote():
# choose method and get sentence # choose method and get sentence
sentence: str = random.choice(choice_methods)() sentence: str = random.choice(choice_methods)()
# write note # write note
toot = mastodon.toot(sentence) random_sentence_note = misskey.notes_create(text=sentence)
toot_url = toot["url"] note_id = random_sentence_note["createdNote"]["id"]
toot_content = toot["content"] note_text = random_sentence_note["createdNote"]["text"]
toot_created_at = toot["created_at"] print(f"{note_id} | {note_text}")
print(f"{toot_created_at} {toot_url} : {toot_content}")
if __name__ == "__main__": if __name__ == "__main__":

View file

@ -1,2 +1,3 @@
Mastodon.py Misskey.py
python-dotenv python-dotenv

View file

@ -1,10 +1,12 @@
# Get mastodon access token # Misskey api key 발급받기
1. Preferences > Development > New Application
2. Set name of your app and enable `write` 1. 설정 - api 접속
3. Submit 2. 엑세스 토큰 생성 - **"노트를 작성하거나 삭제합니다"** 켬
# docker compose crontab 설정하기
# Set crontab with docker compose
If you want your bot to post every hour:
``` ```
0 * * * * docker compose -f <your bot's path>/docker-compose.yml up docker compose -f 프로젝트경로/docker-compose.yml up
``` ```
을 crontab에 등록하시면 됩니다(아마도)

View file

@ -8,34 +8,32 @@ from dotenv import load_dotenv
load_dotenv() load_dotenv()
_sentences: list[str] =[] _sentences=[]
try:
sentence_files=[]
# try: if os.environ.get("SENTENCES_FILE") and os.path.isfile(os.environ["SENTENCES_FILE"]):
sentence_files=[] sentence_files.append(os.environ["SENTENCE_FILES"])
# if os.environ.get("SENTENCES_FILE") and os.path.isfile(os.environ["SENTENCES_FILE"]): if os.environ.get("SENTENCES_FILES"):
# sentence_files.append(os.environ["SENTENCE_FILES"]) sentence_files.extend(list(filter(os.path.isfile, json.loads(os.environ["SENTENCES_FILES"]))))
# if os.environ.get("SENTENCES_FILES"): if os.environ.get("SENTENCES_DIRECTORY") and os.path.isdir(os.environ["SENTENCES_DIRECTORY"]):
# sentence_files.extend(list(filter(os.path.isfile, json.loads(os.environ["SENTENCES_FILES"])))) sentence_files.extend(glob(f'{os.environ["SENTENCES_DIRECTORY"]}*.txt'))
# if os.environ.get("SENTENCES_DIRECTORY") and os.path.isdir(os.environ["SENTENCES_DIRECTORY"]): if os.environ.get("SENTENCES_DIRECTORIES"):
# sentence_files.extend(glob(f'{os.environ["SENTENCES_DIRECTORY"]}*.txt')) # 원라인 똥 뿌직(터져도 책임 안짐)
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_DIRECTORIES"): for sentence_file in sentence_files:
# 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: with open(sentence_file, "r") as f:
_sentences.extend(f.readlines()) _sentences.extend(f.readlines())
# except: except:
# e = sys.exc_info()[1] e = sys.exc_info()[1]
# _sentences = [f"Runtime error(Please contact to the admin): {e}"] _sentences = [f"에러발생(진짜임): {e}"]
# choose random sentence in google spread sheet # choose random sentence in google spread sheet
def chooseSentence() -> str: def chooseSentence() -> str:
# choice random sentence # choice random sentence
return random.choice(_sentences).replace("\\n","\n") return random.choice(_sentences).replace("\\n","\n")