def __init__(self): creds = get_creds() # Read credentials from file self.metadata = metadata self.base = Base self.url = creds["dialect"] + '://' + creds["user"] + ':' + \ creds["paswd"] + '@' + creds["server"] + ":" + creds["port"] + '/' + creds["db"] self.engine = create_engine(self.url, echo=True, pool_recycle=3600, pool_size=20, max_overflow=0) self.metadata.bind = self.engine
def main(): if len(argv) > 1: text = argv[1] else: text = default_text (username, password) = get_creds("credentials") full_url = build_url(text) json_data = fetch_json(full_url, username, password) # pprint(json_data) strongest_emotion = get_strongest_emotion(json_data) print("strongest emotion is : " + strongest_emotion)
def get_strongest_emotion(string): # emotion = json_data.get('emotion').get('document').get('emotion') (username, password) = get_creds("credentials") full_url = build_url(string) # print("full_url is " + full_url) try: json_data = fetch_json(full_url, username, password) except urllib.error.HTTPError: return 'unknown' # this often happens when the text is too short. except UnicodeEncodeError: return 'emoji' # pprint.pprint(json_data) emotions = json_data.get('emotion').get('document').get('emotion') for emote in (reversed(sorted(emotions, key=(lambda x: emotions[x])))): print(f"{emote}\t\t {emotions[emote]}") return max(emotions, key=(lambda x: emotions[x]))