From c7463b378d0d55ea39a0bae53ac0f3594df76ffd Mon Sep 17 00:00:00 2001 From: Ching Date: Fri, 4 Mar 2022 16:48:05 +0800 Subject: [PATCH] =?UTF-8?q?feat(run):=20=E5=A2=9E=E5=8A=A0=20docker=20buil?= =?UTF-8?q?d=20image=20=E5=92=8C=E6=89=A7=E8=A1=8C=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加 docker build image 和执行脚本 Signed-off-by: Ching --- Dockerfile | 6 ++-- env.sh | 6 ++++ last_id.txt | 2 ++ main.py | 78 +++++++++++++++++++++++++++++++++++++++++++++++- requirements.txt | 1 + run.sh | 4 +++ 6 files changed, 94 insertions(+), 3 deletions(-) create mode 100755 env.sh create mode 100644 last_id.txt create mode 100755 run.sh diff --git a/Dockerfile b/Dockerfile index 81118b0..99a0c15 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,6 +4,8 @@ ADD . /app WORKDIR /app -RUN pip install -r requirements.txt +RUN sudo apt update +RUN sudo apt install -y python3-pip +RUN pip3 install -r requirements.txt -CMD ["python", "main.py"] +CMD ["python3", "main.py"] diff --git a/env.sh b/env.sh new file mode 100755 index 0000000..0700f12 --- /dev/null +++ b/env.sh @@ -0,0 +1,6 @@ +#!/bin/sh +export FF_PASSWORD= +export FF_ACCOUNT= +export M_INSTANCE= +export M_TOKEN= +export YOUR_CONTAINER_NAME=mas2ff diff --git a/last_id.txt b/last_id.txt new file mode 100644 index 0000000..1c89816 --- /dev/null +++ b/last_id.txt @@ -0,0 +1,2 @@ +107896829133801546 +107897040775908429 diff --git a/main.py b/main.py index 7761ddb..097c5a8 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,32 @@ from mastodon import Mastodon import argparse +from selenium import webdriver +from selenium.webdriver.common.by import By +import time +def post_fanfou(webdrive_, fanfou_status, images=None): + if not images: + images = [] + # post status + if not images: + input_field = webdrive_.find_element(by=By.NAME, value='content') + input_field.send_keys(fanfou_status) + input_field.submit() + else: + # post with images + pass + +def run(instance, access_token, fanfou_account, fanfou_password): + chrome = webdriver.Chrome() + # login to fanfou + chrome.get('https://m.fanfou.com/') + login_name = chrome.find_element(by=By.NAME, value='loginname') + login_pass = chrome.find_element(by=By.NAME, value='loginpass') + login_name.send_keys(fanfou_account) + login_pass.send_keys(fanfou_password) + login_pass.submit() -def run(instance, access_token): mastodon_cli = Mastodon( access_token=access_token, api_base_url=instance) me_info = mastodon_cli.me() @@ -12,11 +35,58 @@ def run(instance, access_token): me_id, exclude_replies=True) min_id = None max_id = None + # first time run + # init min_id for status in me_timeline: if not status['reblog'] and status['visibility'] == 'public': min_id = status['id'] + print(status['content']) break + # if is in already posted status + posted_ids = [] + # if file not exist, create it + with open('last_id.txt', 'a+') as f: + f.seek(0) + # read lines + posted_ids = f.readlines() + # remove \n + posted_ids = [x.strip() for x in posted_ids] + print(posted_ids) + if str(min_id) not in posted_ids: + content = status['content'].replace('

', '').replace('

', '').replace('
', '\n') + post_fanfou(chrome, content, None) + # write to file + with open('last_id.txt', 'a') as f: + f.write(str(min_id) + '\n') + posted_ids.append(min_id) + time.sleep(10) + + while True: + # get new status + me_timeline = mastodon_cli.account_statuses( + me_id, exclude_replies=True, min_id=min_id) + # if no new status + if not me_timeline: + time.sleep(10) + continue + statuses = [] + for status in me_timeline: + statuses.append(status) + statuses.reverse() + for status in statuses: + if not status['reblog'] and status['visibility'] == 'public': + if str(status['id']) not in posted_ids: + content = status['content'].replace('

', '').replace('

', '').replace('
', '\n') + print(status['id'], content) + post_fanfou(chrome, content, None) + with open('last_id.txt', 'a') as f: + f.write(str(status['id']) + '\n') + posted_ids.append(status['id']) + time.sleep(10) + # update min_id + if statuses: + min_id = statuses[-1]['id'] if __name__ == '__main__': parser = argparse.ArgumentParser(description='Mastodon Bot') @@ -24,3 +94,9 @@ if __name__ == '__main__': help='https://nofan.xyz/settings/applications') parser.add_argument('--mastodon-instance', dest='m_instance', required=True, help='https://nofan.xyz/') + parser.add_argument('--fanfou-account', dest='ff_account', required=True, + help='abc@def.com') + parser.add_argument('--fanfou-password', dest='ff_password', required=True, + help='fanfou passworad') + args = parser.parse_args() + run(args.m_instance, args.m_token, args.ff_account, args.ff_password) diff --git a/requirements.txt b/requirements.txt index faefdec..45404bc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ requests Mastodon.py argparse +selenium diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..a0fd671 --- /dev/null +++ b/run.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +source ./env.sh +docker build -t $YOUR_CONTAINER_NAME .; docker container run -it -v $(pwd):/app $YOUR_CONTAINER_NAME --mastodon-token $M_TOKEN --mastodon-instance $M_INSTANCE --fanfou-account $FF_ACCOUNT --fanfou-password $FF_PASSWORD