import csv
from datetime import datetime
from time import sleep
import OSC


# open a connection to pd
client = OSC.OSCClient()
address = '127.0.0.1', 4000 # 57120==SC
client.connect( address ) # set the address for all following messages
print client


msg = OSC.OSCMessage() # OSCresponder name: '/touch'
msg.setAddress("/twitter-ikstem")
#msg.append('hello from python')
#client.send(msg)



then = None
with open('final-ikstem-openinghours.csv', 'rU') as csvfile:
	file = csv.DictReader(csvfile)
	print file
	#csv.DictReader(csvfile)(["time"] + ['time'])
	i=0
	sleep_time=1
	for row in file:
		#print row
		i+=1
		t = row['time']
		t = float(t)
		now = datetime.fromtimestamp(t) #[]dictreader reads the rowheader
		print now, row
		if then:
			ti = (now-then).total_seconds()
			#print ti/100
			sleep_time = ti/15

		if "#ikstem" in row['text'].lower()	and '#gestemd' in row['text'].lower():
			msg.append( ['both', str(now)] )
		elif '#ikstem' in row['text'].lower():
			msg.append( ['#ikstem', str(now) ])
		elif '#gestemd' in row['text'].lower():
			msg.append( ['#gestemd', str(now)] )
	
		# send an osc message to pd
		print  msg#[ i, row['text'], str(now)]
 		sleep(sleep_time)	
 		client.send(msg)
 		msg.clearData()
 		then = now