失败重试
This commit is contained in:
@@ -29,32 +29,16 @@ logger = telebot.logger.setLevel(logging.INFO)
|
|||||||
if not os.path.isfile("credentials.py"):
|
if not os.path.isfile("credentials.py"):
|
||||||
logging.info("No credentials found")
|
logging.info("No credentials found")
|
||||||
telegram_token = input("输入你的telegram bot token: ")
|
telegram_token = input("输入你的telegram bot token: ")
|
||||||
web_type = input("输入网站类型(misskey):")
|
misskey_token = input("请输入Misskey bot token:")
|
||||||
if web_type == 'misskey':
|
misskey_instance = input("请输入Misskey实例地址(https://m.moec.top):")
|
||||||
misskey_token = input("请输入Misskey bot token:")
|
misskey_visibility = input("帖子类型(public,home,followers)")
|
||||||
misskey_instance = input("请输入Misskey实例地址(https://m.moec.top):")
|
character_limit = input("字数限制:")
|
||||||
misskey_visibility = input("帖子类型(public,home,followers)")
|
|
||||||
character_limit = input("字数限制:")
|
|
||||||
|
|
||||||
with open("credentials.py", "w") as creds:
|
with open("credentials.py", "w") as creds:
|
||||||
creds.write(
|
creds.write(
|
||||||
f"telegram_token = '{telegram_token}'\nmisskey_token = '{misskey_token}'\nweb_type= '{web_type}'\n"
|
f"telegram_token = '{telegram_token}'\nmisskey_token = '{misskey_token}'\nweb_type= '{web_type}'\n"
|
||||||
f"misskey_instance = '{misskey_instance}'\nmisskey_visibility='{misskey_visibility}'\n"
|
f"misskey_instance = '{misskey_instance}'\nmisskey_visibility='{misskey_visibility}'\n"
|
||||||
f"character_limit={character_limit}")
|
f"character_limit={character_limit}")
|
||||||
elif web_type == "mastodon":
|
|
||||||
character_limit = 500
|
|
||||||
mastodon_token = input("输入你的mastodon bot token: ")
|
|
||||||
mastodon_instance = input(
|
|
||||||
"输入mastodon实例地址(https://example.social): ")
|
|
||||||
web = mastodon_instance
|
|
||||||
mastodon_visibility = input(
|
|
||||||
"输入mastodon嘟文类型(public, unlisted, or private): ")
|
|
||||||
with open("credentials.py", "w") as creds:
|
|
||||||
creds.write(
|
|
||||||
f"telegram_token = '{telegram_token}'\nweb_type= '{web_type}'\nmastodon_token = '{mastodon_token}'\n"
|
|
||||||
f"mastodon_instance = '{mastodon_instance}'\nmastodon_visibility={mastodon_visibility}")
|
|
||||||
else:
|
|
||||||
print("输入有误")
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
@@ -68,10 +52,6 @@ else:
|
|||||||
'''
|
'''
|
||||||
Bots
|
Bots
|
||||||
'''
|
'''
|
||||||
# Mastodon
|
|
||||||
if web_type == "mastodon":
|
|
||||||
mastodon_bot = Mastodon(access_token=mastodon_token,
|
|
||||||
api_base_url=mastodon_instance) # i.e.https://mastodon.social
|
|
||||||
|
|
||||||
# Telegram
|
# Telegram
|
||||||
# parse mode can be either HTML or MARKDOWN
|
# parse mode can be either HTML or MARKDOWN
|
||||||
@@ -79,20 +59,12 @@ bot = telebot.TeleBot(telegram_token, parse_mode="HTML")
|
|||||||
|
|
||||||
|
|
||||||
def ping_bots():
|
def ping_bots():
|
||||||
if web_type == "mastodon":
|
|
||||||
try:
|
|
||||||
ping_mastodon = mastodon_bot.me()["username"]
|
|
||||||
logging.info(f"Running mastodon as {ping_mastodon}")
|
|
||||||
except:
|
|
||||||
logging.fatal("Failed to verify mastodon access token.")
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
a = requests.get('https://api.telegram.org/')
|
a = requests.get('https://api.telegram.org/')
|
||||||
if a.status_code != 200:
|
if a.status_code != 200:
|
||||||
logging.info(f"无法连接至TG API服务器")
|
logging.info(f"无法连接至TG API服务器")
|
||||||
ping_telegram = bot.get_me()
|
ping_telegram = bot.get_me()
|
||||||
logging.info(f"成功登入Telegram 机器人帐号 {ping_telegram.username}")
|
logging.info(f"成功登入Telegram 机器人帐号 @{ping_telegram.username}")
|
||||||
except:
|
except:
|
||||||
logging.fatal('无法验证 telegram token.')
|
logging.fatal('无法验证 telegram token.')
|
||||||
exit(1)
|
exit(1)
|
||||||
@@ -161,11 +133,13 @@ def footer_image(message):
|
|||||||
def uploadfile(caption,filename, mimetype):
|
def uploadfile(caption,filename, mimetype):
|
||||||
rmediajson = {"i": misskey_token}
|
rmediajson = {"i": misskey_token}
|
||||||
files = {'file': (filename, open(filename, "rb"), mimetype)}
|
files = {'file': (filename, open(filename, "rb"), mimetype)}
|
||||||
try:
|
i = 0
|
||||||
mediapost = requests.post(misskey_instance+'/api/drive/files/create', timeout=10, data=rmediajson, files=files)
|
while i < 3:
|
||||||
except:
|
try:
|
||||||
logging.info(f"上传失败")
|
mediapost = requests.post(misskey_instance+'/api/drive/files/create', timeout=10, data=rmediajson, files=files)
|
||||||
break
|
i = 4
|
||||||
|
except:
|
||||||
|
logging.info(f"上传失败")
|
||||||
media_id_list=[]
|
media_id_list=[]
|
||||||
media_id_list.append(json.loads(mediapost.text)["id"])
|
media_id_list.append(json.loads(mediapost.text)["id"])
|
||||||
rjson = {'text': caption, "localOnly": False, "visibility": misskey_visibility,
|
rjson = {'text': caption, "localOnly": False, "visibility": misskey_visibility,
|
||||||
@@ -177,67 +151,6 @@ def uploadfile(caption,filename, mimetype):
|
|||||||
#Posting
|
#Posting
|
||||||
'''
|
'''
|
||||||
|
|
||||||
'''
|
|
||||||
#mastodon 有空写
|
|
||||||
@bot.channel_post_handler(content_types=["photo"])
|
|
||||||
def get_image(message):
|
|
||||||
logging.info(f"New {message.content_type}")
|
|
||||||
caption = footer_image(message)
|
|
||||||
|
|
||||||
fileID = message.photo[-1].file_id
|
|
||||||
logging.info(f"Photo ID {fileID}")
|
|
||||||
|
|
||||||
file_info = bot.get_file(fileID)
|
|
||||||
downloaded_file = bot.download_file(file_info.file_path)
|
|
||||||
with open("tmp_img", "wb") as tmp_image:
|
|
||||||
tmp_image.write(downloaded_file)
|
|
||||||
|
|
||||||
media_id = mastodon_bot.media_post("tmp_img")
|
|
||||||
posted = mastodon_bot.status_post(
|
|
||||||
status=caption, media_ids=media_id, visibility=mastodon_visibility)
|
|
||||||
logging.info(f"Posted: {posted['uri']}")
|
|
||||||
|
|
||||||
|
|
||||||
@bot.channel_post_handler(content_types=["video"])
|
|
||||||
def get_video(message):
|
|
||||||
logging.info(f"New {message.content_type}")
|
|
||||||
caption = footer_image(message)
|
|
||||||
|
|
||||||
fileID = message.video.file_id
|
|
||||||
logging.info(f"Video ID {fileID}")
|
|
||||||
|
|
||||||
file_info = bot.get_file(fileID)
|
|
||||||
downloaded_file = bot.download_file(file_info.file_path)
|
|
||||||
with open("tmp_video", "wb") as tmp_video:
|
|
||||||
tmp_video.write(downloaded_file)
|
|
||||||
|
|
||||||
media_id = mastodon_bot.media_post(
|
|
||||||
"tmp_video", mime_type=message.video.mime_type)
|
|
||||||
posted = mastodon_bot.status_post(
|
|
||||||
status=caption, media_ids=media_id, visibility=mastodon_visibility)
|
|
||||||
logging.info(f"Posted: {posted['uri']}")
|
|
||||||
|
|
||||||
|
|
||||||
# repost text messages
|
|
||||||
@bot.channel_post_handler(content_types=["text"])
|
|
||||||
def get_text(message):
|
|
||||||
logging.info(f"New {message.content_type}")
|
|
||||||
status_text = footer_text(message)
|
|
||||||
|
|
||||||
if type(status_text) == list:
|
|
||||||
recent_post = mastodon_bot.status_post(
|
|
||||||
status=status_text[0], visibility=mastodon_visibility)
|
|
||||||
|
|
||||||
for i in status_text[1:]:
|
|
||||||
time.sleep(1)
|
|
||||||
this_recent_post = mastodon_bot.status_post(
|
|
||||||
status=i, visibility=mastodon_visibility, in_reply_to_id=recent_post.get('id'))
|
|
||||||
recent_post = this_recent_post
|
|
||||||
else:
|
|
||||||
print(status_text)
|
|
||||||
mastodon_bot.status_post(
|
|
||||||
status=status_text, visibility=mastodon_visibility)
|
|
||||||
'''
|
|
||||||
|
|
||||||
|
|
||||||
# Misskey
|
# Misskey
|
||||||
@@ -285,24 +198,16 @@ def get_video(message):
|
|||||||
downloaded_file = bot.download_file(file_info.file_path)
|
downloaded_file = bot.download_file(file_info.file_path)
|
||||||
with open("tmp_video", "wb") as tmp_video:
|
with open("tmp_video", "wb") as tmp_video:
|
||||||
tmp_video.write(downloaded_file)
|
tmp_video.write(downloaded_file)
|
||||||
if web_type == "misskey":
|
rmediajson = {"i": misskey_token}
|
||||||
rmediajson = {"i": misskey_token}
|
files = {'file': ("tmp_video", open("tmp_video", "rb"))}
|
||||||
files = {'file': ("tmp_video", open("tmp_video", "rb"))}
|
mediapost = requests.post(misskey_instance + '/api/drive/files/create', data=rmediajson, files=files)
|
||||||
mediapost = requests.post(misskey_instance + '/api/drive/files/create', data=rmediajson, files=files)
|
media_id_list = []
|
||||||
media_id_list = []
|
media_id_list.append(json.loads(mediapost.text)["id"])
|
||||||
media_id_list.append(json.loads(mediapost.text)["id"])
|
rjson = {'text': caption, "localOnly": False, "visibility": misskey_visibility,
|
||||||
rjson = {'text': caption, "localOnly": False, "visibility": misskey_visibility,
|
"fileIds": media_id_list, "viaMobile": False, "i": misskey_token}
|
||||||
"fileIds": media_id_list, "viaMobile": False, "i": misskey_token}
|
logging.info(f"上传视频成功")
|
||||||
logging.info(f"上传视频成功")
|
posted = requests.post(misskey_instance + "/api/notes/create", json=rjson)
|
||||||
posted = requests.post(misskey_instance + "/api/notes/create", json=rjson)
|
logging.info(f"发布帖子成功")
|
||||||
logging.info(f"发布帖子成功")
|
|
||||||
|
|
||||||
elif web_type == "mastodon":
|
|
||||||
media_id = mastodon_bot.media_post(
|
|
||||||
"tmp_video", mime_type=message.video.mime_type)
|
|
||||||
posted = mastodon_bot.status_post(
|
|
||||||
status=caption, media_ids=media_id, visibility=mastodon_visibility)
|
|
||||||
logging.info(f"Posted: {posted['uri']}")
|
|
||||||
|
|
||||||
@bot.channel_post_handler(content_types=["audio"])
|
@bot.channel_post_handler(content_types=["audio"])
|
||||||
def get_audio(message):
|
def get_audio(message):
|
||||||
|
|||||||
Reference in New Issue
Block a user