Browse Source

Applied Black code style

master
Jenkins 3 months ago
parent
commit
4e37560da6
  1. 45
      main.py
  2. 6
      register_masto.py

45
main.py

@ -15,15 +15,27 @@ def save_config(j):
def is_bullshit(text: str):
text = text.lower()
bullshit = ['otus.pw', 't.me', 'selectel', 'slc.tl', '#промо', '#реклама', 'sber', 'beclick.cc', '#текстприслан',
'bit.ly', 'clck.ru', 'clc.to']
bullshit = [
"otus.pw",
"t.me",
"selectel",
"slc.tl",
"#промо",
"#реклама",
"sber",
"beclick.cc",
"#текстприслан",
"bit.ly",
"clck.ru",
"clc.to",
]
if 1 in [(1 if text.find(i) >= 0 else 0) for i in bullshit]:
return True
return False
def unpack_links(text: str):
text = text.replace('](', ' (').replace('[', '')
text = text.replace("](", " (").replace("[", "")
return text
@ -34,26 +46,26 @@ async def post(message: Message, account: str, channel: str):
media_ids = []
if message.media is not None:
try:
print('Downloading media from telegram...')
print("Downloading media from telegram...")
media = await client.download_media(message)
if media.find('.tgs') >= 0:
print('Media is sticker, which unsupported in mastodon. Skipping.')
if media.find(".tgs") >= 0:
print("Media is sticker, which unsupported in mastodon. Skipping.")
else:
print('Uploading media to mastodon...')
print("Uploading media to mastodon...")
posted_media = mastodon.media_post(media)
media_ids.append(posted_media['id'])
media_ids.append(posted_media["id"])
except Exception as e:
print(e)
text = unpack_links(message.text)
if text == '' and media_ids == []:
print('Toot is empty. Skipping.')
if text == "" and media_ids == []:
print("Toot is empty. Skipping.")
else:
mastodon.status_post(
status=text
+ f"\n\nCrostooted from https://t.me/{channel}/{str(message.id)}",
media_ids=media_ids,
)
print('Toot!')
print("Toot!")
if message.media is not None:
try:
os.remove(media)
@ -64,27 +76,28 @@ async def post(message: Message, account: str, channel: str):
async def main(client: TelegramClient):
with open("subscriptions.secret", "r") as file:
j = json.load(file)
print('Getting dialogs...')
print("Getting dialogs...")
for i in await client.get_dialogs():
i: Dialog
if isinstance(i.entity, Channel):
obj = j.get(i.entity.username.lower())
if obj is not None:
print(f'Processing channel {i.entity.username}...')
print(f"Processing channel {i.entity.username}...")
messages: [Message] = await client.get_messages(i, 10)
messages = messages[::-1]
for msg in messages:
print(f'Processing message {msg.id}...')
print(f"Processing message {msg.id}...")
if msg.id not in obj["viewed"]:
if is_bullshit(msg.text) is False:
await post(msg, obj["name"], i.entity.username)
else:
print('Is bullshit. Skipping.')
print("Is bullshit. Skipping.")
obj["viewed"].append(msg.id)
j[i.entity.username.lower()] = obj
save_config(j)
else:
print('Already viewed. Skipping.')
print("Already viewed. Skipping.")
client = TelegramClient(
"tg2masto", api_id=settings.API_ID, api_hash=settings.API_HASH

6
register_masto.py

@ -3,6 +3,8 @@ import settings
Mastodon.create_app(
"telegram2masto", api_base_url=settings.HOMESERVER, to_file="app.secret",
website='https://git.cofob.ru/cofob/Telegram2Masto'
"telegram2masto",
api_base_url=settings.HOMESERVER,
to_file="app.secret",
website="https://git.cofob.ru/cofob/Telegram2Masto",
)

Loading…
Cancel
Save