Created
April 23, 2020 13:09
-
-
Save dylanlangston/91e103ff5540810556b12860ffb2016c to your computer and use it in GitHub Desktop.
Get the current average mood from twitter using WeFeel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import urllib.request | |
from builtins import print | |
import urllib.error | |
import re | |
import os | |
import json | |
import fnmatch | |
import time | |
import socket | |
from decimal import * | |
# Stores data to pass between functions. | |
def variables(data): | |
variables.data = data or variables.data | |
return variables.data | |
# Checks the internet connection and downloads the initial data. | |
def internet_on(): | |
try: | |
address=socket.gethostbyname("wefeel.csiro.au") | |
response=urllib.request.urlopen('http://' + address + '/api/emotions/primary/timepoints',timeout=1).read() | |
variables([response, "sacrificial data", "sacrificial data"]) | |
return True | |
except socket.gaierror as err: pass | |
return False | |
# Does the calculations to determine the difference. | |
def emotionnumbers(data, emotion): | |
emotionnumber = int((re.findall(r'\d+', fnmatch.filter(data, '*%s*' % emotion)[0]))[0]) | |
x = 0 | |
z = 0 | |
for y in data: | |
x = int(re.findall(r'\d+', y)[0]) | |
if x > z: | |
z = x | |
emotiontotal = (z) | |
difference = (emotionnumber / emotiontotal) | |
return round(difference * 1000000, 10) | |
# Passes data to the previous function for calculating. | |
def calc(emotionnumber): | |
emotion = emotions[emotionnumber] | |
data, olddata, newdata = variables([]) | |
return round(emotionnumbers(olddata, emotion) - emotionnumbers(newdata, emotion), 6) | |
# Does all the processing and returns the emotion. | |
def emotion(): | |
data, olddata, newdata = variables([]) | |
jsondata = json.loads(data.decode('utf-8')) | |
olddata = (json.dumps(jsondata[0]["counts"]))[1:][:-1].replace(" ", "").split(",") | |
newdata = (json.dumps(jsondata[(len(jsondata) - 1)]["counts"]))[1:][:-1].replace(" ", "").split(",") | |
variables([data, olddata, newdata]) | |
differences = [calc(0), calc(1), calc(2), calc(3), calc(4), calc(5), calc(6)] | |
return (emotions[differences.index(max(differences))]) | |
#Debugging | |
def debug(): | |
print("--Start Debug Info--") | |
data, olddata, newdata = variables([]) | |
print("\nFull Dataset: " + str(data)) | |
jsondata = json.loads(data.decode('utf-8')) | |
print("\nJSON Data: " + str(jsondata)) | |
olddata = (json.dumps(jsondata[0]["counts"]))[1:][:-1].replace(" ", "").split(",") | |
print("\nOld Data: " + str(olddata)) | |
newdata = (json.dumps(jsondata[(len(jsondata) - 1)]["counts"]))[1:][:-1].replace(" ", "").split(",") | |
print("\nNew Data: " + str(newdata)) | |
variables([data, olddata, newdata]) | |
differences = [calc(0), calc(1), calc(2), calc(3), calc(4), calc(5), calc(6)] | |
c=0 | |
dif = [] | |
for i in emotions: | |
dif.append(str('"' + emotions[c]) + '": ' + str(differences[c])) | |
c = c + 1 | |
print("\nDifferences: " + str(dif)) | |
print("\nCurrent Emotion: " + emotions[differences.index(max(differences))]) | |
return("--End Debug Info--\n") | |
# The actual code that is run. | |
if internet_on(): | |
emotions = ["love", "joy", "sadness", "other", "anger", "surprise", "fear"] | |
print(debug().capitalize()) | |
print(emotion().capitalize()) | |
else: | |
print("No Internet") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment