Exemple #1
0
def take_suggestions():
	if os.path.exists(LATESTFILE_SUG):
		fp = open(LATESTFILE_SUG)
		lastid = fp.read().strip()
		fp.close()
		
		if lastid == '':
			lastid = 0
	else:
		lastid = 0
		
	result = api.GetMentions(since_id = lastid)
	#No mention, no suggestion
	print lastid
	if len(result) == 0:
		print "No mention received"
		return []
	else :
		fp = open(LATESTFILE_SUG, 'w')
		fp.write(str(max([x.id for x in result])))
		fp.close()
		entry = History.query.descending('mongo_id').first()
		for x in result:
			print x.text
		#Somebody already suggested..
		if entry.next_location.name != entry.suggested_location.name :
			print "There already is a suggestion. Fitz is currently headed to "+entry.next_location.name
			return []
		else :
			candidates = {}
			#Walk through all the mentions we got here..
			
			for x in result :
				mention_entry = x.text.split(' ')
				s_user = x.user.screen_name
				#Locations have to be proposed in a form of "Check out ***"
				if mention_entry[1] == 'Check' and mention_entry[2] == 'out':
					s_key = s_user + ":" + ' '.join(mention_entry[3:])
					s_geo = geocoder(' '.join(mention_entry[3:]))
					distance = gcd(entry.next_location.geocode[0], entry.next_location.geocode[1], s_geo[1], s_geo[2])
					candidates[s_key] = distance
			#Got somethin' but no useful words
			if len(candidates) == 0:
				print "Got some words, but no suggestions.."
				return []
			#Got somewhere to go!
			else :
				next_move = min(candidates, key=candidates.get)
				print candidates[candidates.keys()[0]] > candidates[candidates.keys()[1]]
				print next_move		
				l = Location()
				l.name = next_move.split(':')[1]
				l.geocode = geocoder(next_move.split(':')[1])[1:]
				entry.suggested_location = l
				entry.suggested_by = next_move.split(':')[0]
				entry.save()
				user_sug = []
				user_sug.append(next_move.split(':')[0])
				user_sug.append(next_move.split(':')[1])
				return user_sug
Exemple #2
0
def move():
    p_entry = History.query.descending('mongo_id').first()
    n_entry = History()
    n_entry = p_entry
    print p_entry.next_location.geocode[0]
    print p_entry.next_location.geocode[1]
    print p_entry.suggested_location.geocode[0]
    print p_entry.suggested_location.geocode[1],
    print p_entry.suggested_location.geocode[0] == p_entry.next_location.geocode[0]
    #if the bot is not moving
    
   
    if p_entry.status is False:
    	print "Fitz Owen is not moving, and.."
    	#if the bot has got a new suggestion, then move move move yo
    	if p_entry.next_location.geocode != p_entry.suggested_location.geocode:
    		print "there is a suggested destination :" + p_entry.suggested_location.name
    		n_entry.status = True
    		n_entry.previous_location = p_entry.next_location
    		n_entry.next_location = p_entry.suggested_location
    		distance = gcd(n_entry.previous_location.geocode[0], n_entry.previous_location.geocode[1], n_entry.next_location.geocode[0], n_entry.next_location.geocode[1])
    		n_entry.distance = distance
    		n_entry.number_of_segments = int(distance / p_entry.speed)
    		if (distance / p_entry.speed) < 1:
    			n_entry.number_of_segments = 1
    		print "Now moving towards" + n_entry.next_location.name +"..."
    	else :
    		print "okay, not moving"
    #if the bot is approaching the destination..
    elif p_entry.status is True:
    	if p_entry.segment is p_entry.number_of_segments:
    		print "got there!"
    		#then stop, and calibrate the location, reset
    		n_entry.status = False
    		n_entry.current_location = p_entry.next_location
    		n_entry.segment = 1
    	#Okay, now let's move..
    	else:
    		print "moving.."
    		lon = (p_entry.next_location.geocode[0] - p_entry.previous_location.geocode[0]) / p_entry.number_of_segments
    		lat = (p_entry.next_location.geocode[1] - p_entry.previous_location.geocode[1]) / p_entry.number_of_segments
    		xy = (p_entry.current_location.geocode[0]+lon,p_entry.current_location.geocode[1]+lat)
    		n_entry.current_location.geocode = xy
    		n_entry.segment = p_entry.segment + 1
    		print n_entry.segment 
    		print n_entry.number_of_segments
    n_entry.current_time = str(datetime.datetime.now())
    n_entry.save()