/
osc_helpers.py
120 lines (90 loc) · 2.91 KB
/
osc_helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from pythonosc.udp_client import UDPClient
from pythonosc.osc_message_builder import OscMessageBuilder
import logging
import json
import re
import pickle
from twitter_infos import infos
clients = {}
def osc_message(d):
"""
create OSC message from a dict
"""
msg = OscMessageBuilder(address="/send")
msg.add_arg(json.dumps(d))
return msg.build()
def send_message_to_screen(candidate, message):
global clients
if candidate not in clients:
v = infos[candidate]
if 'ip' not in v:
logging.debug('no ip for %s' % (candidate))
return
clients[candidate] = UDPClient(v['ip'], v['osc_port'])
if type(message) == bytes:
clients[candidate].send(pickle.loads(message))
else:
clients[candidate].send(message)
logging.debug('sent message to {0}'.format(candidate))
def assign_user(d, user):
d['name'] = user['name']
d['handle'] = user['screen_name']
d['user-img-url'] = user['profile_image_url']
def tweet_arg(data):
tweet = re.sub(r'(http|https)://[a-zA-Z0-9./…]+(?:\s|$)', '', data['text'])
arg = { 'tweet': tweet }
if 'retweeted_status' in data:
assign_user(arg, data['retweeted_status']['user'])
arg['retweeted-by'] = data['user']['name']
arg['tweet'] = data['text'].split(': ')[1]
# print("added retweet!!!!!!")
else:
assign_user(arg, data['user'])
try:
if 'media'in data['entities']:
arg['media'] = data['entities']['media'][0]['media_url']
except KeyError:
pass
return arg
def tweet_message(data):
arg = tweet_arg(data)
return osc_message({
'func': 'post',
'arg': arg
})
def personal_update(tweet_data, favs, retweets):
arg = tweet_arg(tweet_data)
arg['favourites'] = favs
arg['retweets'] = retweets
return osc_message({
'func': 'personal_update',
'arg': arg
})
def action_update(tweet_data, amount):
arg = tweet_arg(tweet_data)
arg['amount'] = amount
return osc_message({
'func': 'action_update',
'arg': arg
})
balloon_osc = None
def affect_candidate(candidate, osc_msg, amount):
global balloon_osc
if not balloon_osc:
balloon_osc = UDPClient('0.0.0.0', 5005)
osc_callback_msg = pickle.dumps(osc_msg)
msg = OscMessageBuilder("/instruction")
msg.add_arg(candidate)
msg.add_arg(amount)
msg.add_arg(osc_callback_msg)
balloon_osc.send(msg.build())
logging.info("sent instruction %s, %d" % (candidate, amount))
# send_message_to_screen(candidate, )
# logging.info("sent instruction to candidate")
def adjust_balloons():
global balloon_osc
if not balloon_osc:
balloon_osc = UDPClient('0.0.0.0', 5005)
msg = OscMessageBuilder("/adjust")
balloon_osc.send(msg.build())
logging.info("sent adjustment")