112 lines
3.8 KiB
Python
112 lines
3.8 KiB
Python
from mastodon import Mastodon
|
|
import argparse
|
|
from selenium import webdriver
|
|
from selenium.webdriver.common.by import By
|
|
from selenium.webdriver.chrome.options import Options
|
|
import time
|
|
import re
|
|
|
|
|
|
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_options = Options()
|
|
chrome_options.add_argument('--headless')
|
|
chrome_options.add_argument('--no-sandbox')
|
|
chrome_options.add_argument('--disable-dev-shm-usage')
|
|
chrome_options.add_argument('--disable-gpu')
|
|
chrome = webdriver.Chrome(options=chrome_options)
|
|
# 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()
|
|
|
|
mastodon_cli = Mastodon(
|
|
access_token=access_token, api_base_url=instance)
|
|
me_info = mastodon_cli.me()
|
|
me_id = me_info['id']
|
|
me_timeline = mastodon_cli.account_statuses(
|
|
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('<br />', '\n')
|
|
content = re.sub('<.*?>', '', content)
|
|
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('<br />', '\n')
|
|
content = re.sub('<.*?>', '', content)
|
|
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')
|
|
parser.add_argument('--mastodon-token', dest='m_token', required=True,
|
|
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)
|