mirror of
https://github.com/flynx/photographer.ru-bot-tg2vk.git
synced 2025-10-29 11:10:10 +00:00
cleanup...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
6d6118c68a
commit
a4ed8ed948
68
bot.py
68
bot.py
@ -1,9 +1,14 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
#----------------------------------------------------------------------
|
||||||
|
|
||||||
# systemd service...
|
# systemd service...
|
||||||
|
#
|
||||||
# see:
|
# see:
|
||||||
# https://tecadmin.net/run-shell-script-as-systemd-service/
|
# https://tecadmin.net/run-shell-script-as-systemd-service/
|
||||||
# https://tecadmin.net/setup-autorun-python-script-using-systemd/
|
# https://tecadmin.net/setup-autorun-python-script-using-systemd/
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#----------------------------------------------------------------------
|
||||||
|
|
||||||
import telebot
|
import telebot
|
||||||
import vk_api
|
import vk_api
|
||||||
@ -13,25 +18,39 @@ import io
|
|||||||
import datetime
|
import datetime
|
||||||
import re
|
import re
|
||||||
|
|
||||||
BASE_PATH = os.path.dirname(
|
|
||||||
os.path.abspath(__file__))
|
|
||||||
|
|
||||||
# Токен бота в Телеграме
|
#----------------------------------------------------------------------
|
||||||
telegram_token = open(os.path.join(BASE_PATH, 'telegram.token'), 'r').read().strip()
|
# Helpers...
|
||||||
|
|
||||||
# Токен группы ВКонтакте
|
# XXX HACK -- mini/cute html2 -> markdown parser...
|
||||||
vk_token = open(os.path.join(BASE_PATH,'vk.token'), 'r').read().strip()
|
def html2md(text):
|
||||||
|
text = re.sub(r'<b>(.*)</b>', r'*\1*', text, flags=re.I|re.M)
|
||||||
# ID группы ВКонтакте
|
text = re.sub(r'<i>(.*)</i>', r'_\1_', text, flags=re.I|re.M)
|
||||||
vk_group_id = open(os.path.join(BASE_PATH,'vk_group.id'), 'r').read().strip()
|
text = re.sub(r'<a\s+[ˆ>]*href="(.*)"[ˆ>]*>(.*)</a>', r'[\2](\1)', text, flags=re.I|re.M)
|
||||||
|
return text
|
||||||
|
|
||||||
|
def load(name):
|
||||||
|
return open(os.path.join(BASE_PATH, name), 'r').read().strip()
|
||||||
|
|
||||||
def getDate():
|
def getDate():
|
||||||
return datetime.datetime.now().strftime('%Y%m%d %H:%M:%S')
|
return datetime.datetime.now().strftime('%Y%m%d %H:%M:%S')
|
||||||
|
|
||||||
|
|
||||||
|
#----------------------------------------------------------------------
|
||||||
|
|
||||||
|
BASE_PATH = os.path.dirname(
|
||||||
|
os.path.abspath(__file__))
|
||||||
|
|
||||||
|
|
||||||
|
telegram_token = load('telegram.token')
|
||||||
|
vk_token = load('vk.token')
|
||||||
|
vk_group_id = load('vk_group.id')
|
||||||
|
|
||||||
|
|
||||||
TEXT_SNIP = 50
|
TEXT_SNIP = 50
|
||||||
|
|
||||||
# CONTENT_TYPES = [
|
|
||||||
|
# Content types supported:
|
||||||
# "text",
|
# "text",
|
||||||
# "audio",
|
# "audio",
|
||||||
# "document",
|
# "document",
|
||||||
@ -53,20 +72,22 @@ TEXT_SNIP = 50
|
|||||||
# "migrate_to_chat_id",
|
# "migrate_to_chat_id",
|
||||||
# "migrate_from_chat_id",
|
# "migrate_from_chat_id",
|
||||||
# "pinned_message",
|
# "pinned_message",
|
||||||
# ]
|
|
||||||
TRACK_TYPES = [
|
TRACK_TYPES = [
|
||||||
"text",
|
"text",
|
||||||
"photo",
|
"photo",
|
||||||
]
|
]
|
||||||
|
|
||||||
# Создаем объекты бота в Телеграме и API ВКонтакте
|
|
||||||
bot = telebot.TeleBot(telegram_token)
|
bot = telebot.TeleBot(telegram_token)
|
||||||
vk_session = vk_api.VkApi(token=vk_token)
|
vk_session = vk_api.VkApi(token=vk_token)
|
||||||
vk = vk_session.get_api()
|
vk = vk_session.get_api()
|
||||||
|
|
||||||
print(f'{getDate()}: Bot started...')
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#----------------------------------------------------------------------
|
||||||
|
# Text post handler...
|
||||||
|
#
|
||||||
# XXX TODO:
|
# XXX TODO:
|
||||||
# - for some reason this does not see attached images at all...
|
# - for some reason this does not see attached images at all...
|
||||||
# message.photo == None...
|
# message.photo == None...
|
||||||
@ -78,7 +99,6 @@ def repost_text_to_vk(message):
|
|||||||
|
|
||||||
attachments = []
|
attachments = []
|
||||||
|
|
||||||
# Если в сообщении есть изображения, загружаем их в ВКонтакте
|
|
||||||
# XXX for some reason we are not getting any photos here...
|
# XXX for some reason we are not getting any photos here...
|
||||||
print(f" --- PHOTOS: {message.photo}")
|
print(f" --- PHOTOS: {message.photo}")
|
||||||
if message.photo:
|
if message.photo:
|
||||||
@ -103,14 +123,10 @@ def repost_text_to_vk(message):
|
|||||||
print(f"{getDate()}: Posted message: {(message.text or '')[:TEXT_SNIP]}")
|
print(f"{getDate()}: Posted message: {(message.text or '')[:TEXT_SNIP]}")
|
||||||
|
|
||||||
|
|
||||||
# XXX HACK -- mini/cute html2 -> markdown parser...
|
|
||||||
def html2md(text):
|
|
||||||
text = re.sub(r'<b>(.*)</b>', r'*\1*', text, flags=re.I|re.M)
|
|
||||||
text = re.sub(r'<i>(.*)</i>', r'_\1_', text, flags=re.I|re.M)
|
|
||||||
text = re.sub(r'<a\s+[ˆ>]*href="(.*)"[ˆ>]*>(.*)</a>', r'[\2](\1)', text, flags=re.I|re.M)
|
|
||||||
return text
|
|
||||||
|
|
||||||
|
|
||||||
|
#----------------------------------------------------------------------
|
||||||
|
# Image post handler...
|
||||||
|
#
|
||||||
# see:
|
# see:
|
||||||
# https://github.com/python273/vk_api/blob/master/examples/upload_photo.py
|
# https://github.com/python273/vk_api/blob/master/examples/upload_photo.py
|
||||||
# https://vk-api.readthedocs.io/en/latest/upload.html (see: photo_wall(..))
|
# https://vk-api.readthedocs.io/en/latest/upload.html (see: photo_wall(..))
|
||||||
@ -159,7 +175,15 @@ def repost_photo_to_vk(message):
|
|||||||
print(f"{getDate()}: Posted photo: {(message.caption or '')[:TEXT_SNIP]}")
|
print(f"{getDate()}: Posted photo: {(message.caption or '')[:TEXT_SNIP]}")
|
||||||
|
|
||||||
|
|
||||||
# Запускаем бота в Телеграме с измененными параметрами опроса
|
|
||||||
|
#----------------------------------------------------------------------
|
||||||
|
# Start...
|
||||||
|
|
||||||
|
print(f'{getDate()}: Bot started...')
|
||||||
|
|
||||||
bot.polling(none_stop=False, interval=0, timeout=60)
|
bot.polling(none_stop=False, interval=0, timeout=60)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#----------------------------------------------------------------------
|
||||||
|
# vim:set ts=4 sw=4 :
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user