#!/usr/bin/env python3

import os
import sys

from analyzer import Analyzer
from termcolor import colored
import application
import helpers
    
def main():
    
    # accepts one and only one command-line argument, the screen name for a user on Twitter
    if len(sys.argv) != 2:
        sys.exit("Usage: ./tweets @Twitter_screen_name")
    
    # absolute paths to positive and negative lists.
    positives = os.path.join(sys.path[0], "positive-words.txt")
    negatives = os.path.join(sys.path[0], "negative-words.txt")
    
    # instantiate analyzer
    analyzer = Analyzer(positives, negatives)
    
    # queries Twitter’s API for a user’s most recent 50 tweets
    tweets = helpers.get_user_timeline(sys.argv[1], count=49)
    #checking if there are tweets http://stackoverflow.com/questions/3289601/null-object-in-python
    if tweets is None:
        print("This screen_name does not excist")
        exit(1)
        # analyzes the sentiment of each of those tweets
    #check first if  tweets list isnt empty. http://stackoverflow.com/questions/53513/best-way-to-check-if-a-list-is-empty
    for tweet in tweets:
            score = analyzer.analyze(tweet)
            # outputs each tweet’s score and text, colored in green if positive, red if negative, and yellow otherwise.
            if score > 0:
                print(colored(str(score) + " " + tweet, "green"))
            elif score < 0:
                print(colored(str(score) + " " + tweet, "red"))
            else:
                print(colored(str(score) + " " + tweet, "yellow"))

if __name__ == "__main__":
    main()
    

    
