Browse Source

Applied Black code style

master
Jenkins 3 months ago
parent
commit
176d54f9ab
  1. 15
      add_channel.py
  2. 17
      login_masto.py
  3. 19
      login_telegram.py
  4. 23
      main.py
  5. 4
      register_masto.py
  6. 4
      settings.py

15
add_channel.py

@ -1,14 +1,17 @@
import json
import os.path
if not os.path.isfile('subscriptions.secret'):
with open('subscriptions.secret', 'w') as file:
file.write('{}')
if not os.path.isfile("subscriptions.secret"):
with open("subscriptions.secret", "w") as file:
file.write("{}")
with open('subscriptions.secret', 'r') as file:
with open("subscriptions.secret", "r") as file:
j = json.load(file)
j[input('Channel username: ').replace('@', '').strip().lower()] = {"name":input('Mastodon account name: ').strip(), "viewed": []}
j[input("Channel username: ").replace("@", "").strip().lower()] = {
"name": input("Mastodon account name: ").strip(),
"viewed": [],
}
with open('subscriptions.secret', 'w') as file:
with open("subscriptions.secret", "w") as file:
json.dump(j, file)

17
login_masto.py

@ -1,17 +1,10 @@
from mastodon import Mastodon
import settings
mastodon = Mastodon(
client_id='app.secret',
api_base_url=settings.HOMESERVER
)
mastodon = Mastodon(client_id="app.secret", api_base_url=settings.HOMESERVER)
email = input('Email: ')
password = input('Password: ')
account_name = input('Account name (for use in config files): ')
email = input("Email: ")
password = input("Password: ")
account_name = input("Account name (for use in config files): ")
mastodon.log_in(
email,
password,
to_file=f'{account_name}-account.secret'
)
mastodon.log_in(email, password, to_file=f"{account_name}-account.secret")

19
login_telegram.py

@ -4,10 +4,21 @@ import settings
async def main(client: TelegramClient):
me = await client.get_me()
print('Logged in as ' + me.first_name + (' ' + me.last_name if me.last_name else '') + '.' +
('\nLogged in as a bot! Bot cannot read channels to which it was not invited.' if me.bot else ''))
print(
"Logged in as "
+ me.first_name
+ (" " + me.last_name if me.last_name else "")
+ "."
+ (
"\nLogged in as a bot! Bot cannot read channels to which it was not invited."
if me.bot
else ""
)
)
if __name__ == '__main__':
client = TelegramClient('tg2masto', api_id=settings.API_ID, api_hash=settings.API_HASH).start()
if __name__ == "__main__":
client = TelegramClient(
"tg2masto", api_id=settings.API_ID, api_hash=settings.API_HASH
).start()
client.loop.run_until_complete(main(client))

23
main.py

@ -9,24 +9,27 @@ import os
def save_config(j):
with open('subscriptions.secret', 'w') as file:
with open("subscriptions.secret", "w") as file:
json.dump(j, file)
async def post(message: Message, account: str, channel: str):
mastodon = Mastodon(
access_token=f'{account}-account.secret',
api_base_url=settings.HOMESERVER
access_token=f"{account}-account.secret", api_base_url=settings.HOMESERVER
)
print(message.stringify())
media = await client.download_media(message)
posted_media = mastodon.media_post(media)
mastodon.status_post(status=message.text+f'\n\nCrostooted from https://t.me/{channel}/{str(message.id)}', media_ids=[posted_media['id']])
mastodon.status_post(
status=message.text
+ f"\n\nCrostooted from https://t.me/{channel}/{str(message.id)}",
media_ids=[posted_media["id"]],
)
os.remove(media)
async def main(client: TelegramClient):
with open('subscriptions.secret', 'r') as file:
with open("subscriptions.secret", "r") as file:
j = json.load(file)
for i in await client.get_dialogs():
i: Dialog
@ -35,13 +38,15 @@ async def main(client: TelegramClient):
if obj is not None:
messages: [Message] = await client.get_messages(i, 10)
for msg in messages:
if msg.id not in obj['viewed']:
await post(msg, obj['name'], i.entity.username)
obj['viewed'].append(msg.id)
if msg.id not in obj["viewed"]:
await post(msg, obj["name"], i.entity.username)
obj["viewed"].append(msg.id)
j[i.entity.username.lower()] = obj
save_config(j)
print(i.message)
client = TelegramClient('tg2masto', api_id=settings.API_ID, api_hash=settings.API_HASH).start()
client = TelegramClient(
"tg2masto", api_id=settings.API_ID, api_hash=settings.API_HASH
).start()
client.loop.run_until_complete(main(client))

4
register_masto.py

@ -3,7 +3,5 @@ import settings
Mastodon.create_app(
'telegram2masto',
api_base_url=settings.HOMESERVER,
to_file='app.secret'
"telegram2masto", api_base_url=settings.HOMESERVER, to_file="app.secret"
)

4
settings.py

@ -1,3 +1,3 @@
HOMESERVER = 'https://zhub.link'
HOMESERVER = "https://zhub.link"
API_ID = 3781832
API_HASH = '5dad050956afcf17a76d04a8ecb42290'
API_HASH = "5dad050956afcf17a76d04a8ecb42290"

Loading…
Cancel
Save