make things easier to develop in future

This commit is contained in:
Danial Behzadi 2021-06-01 05:03:52 +04:30
parent 36a2257a95
commit 2ab3e0181c

View file

@ -26,7 +26,7 @@ def get_text_and_media(message):
elif message['media']: elif message['media']:
text = message['caption'] text = message['caption']
media = message.download() media = message.download()
return text, media return splitext(text), media
def connect(): def connect():
@ -40,23 +40,15 @@ def connect():
return mastodon return mastodon
def section(text: str, part: int): def splitext(text: str):
''' '''
return the text in sections split text into parts
for long texts
''' '''
limit = config.character_limit limit = config.character_limit
return text[limit*part:limit*(part+1)] result = [
text[limit*part:limit*(part+1)]
for part in range(len(text)/limit+1)]
def continued(text: str, part: int): return result
'''
specifies if text is continued
'''
limit = config.character_limit
if len(text) <= limit*(part+1):
return False
return True
def main(): def main():
@ -73,19 +65,17 @@ def main():
if chat['type'] == 'channel': if chat['type'] == 'channel':
if chat['username'] == config.channel_id: if chat['username'] == config.channel_id:
text, media = get_text_and_media(message) text, media = get_text_and_media(message)
part = 0
if media: if media:
mid = mastodon.media_post(media) mid = mastodon.media_post(media)
os.remove(media) os.remove(media)
else: else:
mid = None mid = None
post = mastodon.status_post( post = mastodon.status_post(
section(text, part), text[0],
media_ids=mid) media_ids=mid)
while continued(text, part): for part in range(1, len(text)):
part += 1
post = mastodon.status_post( post = mastodon.status_post(
section(text, part), text[part],
in_reply_to_id=post['id']) in_reply_to_id=post['id'])
telegram.start() telegram.start()