from x import *
from functions import *
from config import *

if __name__ == '__main__':
    record = None
    title = ''
    text = ''
    tags = ''
    media = ''
    tweet_text = ''
    tags_text_array = ''
    tags_array = []
    id = 0

    connection = get_connection(host=HOST, db=DB, user=USER, password=PASSWORD)
    # Get the Prompt
    record = get_prompt(connection)
    if record is not None:
        id = record['id']
        title = record['title']
        text = record['text']
        tags = record['tags']
        media = record['media']
        print(media)
        if tags != '':
            tags_array = tags.split(',')
            for tag in tags_array:
                tags_text_array += '#' + tag.strip() + ','
        tags_text_array = tags_text_array.rstrip(',')
        max_length = 150
        if len(text) > max_length:
            tweet_text = title + '\n\nPrompt: ' + text[0:max_length] + '...'
        else:
            tweet_text = title + '\n' + text
        tweet_text = tweet_text + '\n' + tags_text_array
        if len(tweet_text) < 280:
            response, tweet_id = send_tweet(tweet_text, MEDIA_PATH + media)
            if response:
                print(response)
                update_status_tweet_id(prompt_id=id, tweet_id=tweet_id, connection=connection)
                print('STATUS UPDATED')
            connection.close()
