Esempio n. 1
0
def enter_best_contests(s,session_id,bet_sport,max_bet,potential_contests,time_remaining,wins_data,bin_size):
	current_bet = 0
	with open('C:/Users/Cole/Desktop/Fanduel/fanduel/userwinscache.txt',"r") as myfile:
		data = myfile.read()
	user_wins_cache = ast.literal_eval(data)
	with open('C:/Users/Cole/Desktop/Fanduel/fanduel/roster.txt',"r") as myfile:
		data = myfile.read()
	strategy_data = ast.literal_eval(data)
	for contest in potential_contests:
		if current_bet < max_bet:
			entry_url = 'https://www.fanduel.com/pg/Lobby/showTableEntriesJSON/' + contest['contest_id'] + '/0/10000'
			response = urllib2.urlopen(entry_url)
			shtml = response.read()
			entry_dict = ast.literal_eval(shtml)
			user_wins_array = {'Total':[],'NFL':[],'MLB':[],'NBA':[],'NHL':[],'CBB':[],'CFB':[]}
			for entry_data in entry_dict['entries']:
				user_html = entry_data['userHTML']
				intStart = user_html.find("alt=''>")
				intEnd = user_html.find('<',intStart)
				username = user_html[intStart:intEnd].replace("alt=''>","")
				if username in user_wins_cache.keys():
					for sport,wins in user_wins_cache[username].iteritems():
						user_wins_array[sport].append(wins)
				else:
					user_url = 'https://www.fanduel.com/users/'+username
					response = urllib2.urlopen(user_url)
					shtml = response.read()
					soup = BeautifulSoup(shtml)
					table = soup.find('table', {'class': 'condensed'})
					td = table.find('th').findNext('td')
					user_wins_cache[username] = {}
					for th in table.findAll('th'):
						try:
							user_wins_array[th.text].append(int(td.text))
							user_wins_cache[username][th.text] = int(td.text)
							td = td.findNext('td')
						except ValueError:
							user_wins_array[th.text].append(0)
							user_wins_cache[username][th.text] = 0
							td = td.findNext('td')					
						except KeyError:
							pass	
					time.sleep(1)
			arr = numpy.array(user_wins_array[bet_sport])
			print arr
			avg_top_wins = numpy.mean(arr)#Need to decide best stats for paticualar contest type
			print avg_top_wins
			contest_utility,future_utility = get_contest_utlity(avg_top_wins,time_remaining,wins_data,bin_size)
			print (contest_utility, future_utility)
			if contest_utility > future_utility and current_bet < max_bet and contest['entryFee']<=(max_bet - current_bet):
				print 'entry attempt'
				player_data = str(strategy_data['player_data'])
				entry_id,entry_status = fdo.enter_contest(s,session_id,'https://www.fanduel.com/e/Game/' + contest['game_id'] + '?tableId=' + contest['contest_id'] + '&fromLobby=true',player_data)
				print entry_status
				contest['avg_top_wins'] = avg_top_wins
				contest['entry_id'] = entry_id
				contest['strat_params'] = str(strategy_data['strat_params'])
				if entry_status == 'success':
					current_bet = current_bet + contest['entryFee']
					placeholders = ', '.join(['%s'] * len(contest))
					columns = ', '.join(contest.keys())
					dbo.insert_mysql('FD_table_contests',columns,placeholders,contest.values())
	with open('C:/Users/Cole/Desktop/Fanduel/fanduel/userwinscache.txt',"w") as myfile:
		myfile.write(str(user_wins_cache))
	return current_bet