def load_spreads(week_number): this_year = str(now().year) url = f'https://bettingdata.com/NFL_Odds/Odds_Read?season={this_year}&week={week_number}&seasontype=1&state=WORLD&teamkey=ARI&subscope=2&scope=1' response = requests.get(url) if response.status_code != 200: print('Failed to get data:', response.status_code) else: i=0 root = json.loads(response.text) raw = root['Raw'] root2 = json.loads(raw) for node in root2: # f*****g COVID try: for k in ['HomeTeam','AwayTeam','PointSpread']: print(node[k]) g = grab_game(week_number,node['HomeTeam']) if node['PointSpread'] is None: spread = None else: spread = float(node['PointSpread']) if not(spread is None): if spread < 0: g.setFav(team_from_string(node['HomeTeam']),-int(spread)) else: g.setFav(team_from_string(node['AwayTeam']),int(spread)) print(g.as_string()) g.save() print('') except: None
def dopicks(request): if request.user.is_authenticated: user = request.user else: return redirect('login') (week_number,status) = status_() if status == 'Closed': messages.warning(request,f'Week {week_number} is closed.') return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/')) if status == 'Not Open': messages.warning(request,f'Sorry, week {week_number} is not open for picks yet.') return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/')) queryset = Pick.objects.filter(week_number=week_number,player=user).order_by('game_number').all() formset = PickFormSet(queryset=queryset) (monday_instance, created) = Monday.objects.get_or_create(player=user, week_number=week_number) if monday_instance.total_points == 0: if week_number > 1: monday_instance.total_points = Monday.objects.get(player=user,week_number=week_number-1).total_points else: monday_instance.total_points = random.choice([30,35,40,45,50,55]) monday_form = MondayForm(instance=monday_instance) balance = Bank.objects.filter(player=user).aggregate(Sum('deposit_amount'))['deposit_amount__sum'] if balance is None or week_number < 3: balance = 0.0 now_ = now().strftime('%A %B %-d %-I:%M %p') return render(request, 'pool/dopicks.html', { 'now':now_, 'player':user.username, 'week_number':week_number, 'formset':formset, 'monday_form':monday_form, 'balance':int(-balance)} )
def status(now_ = None): if now_ is None: now_ = now() # pre week 1 if now_ < Game.objects.get(week_number=1,game_number=1).game_date: if Game.objects.filter(week_number=1,spread__isnull=True).count() == 0: return (1,'Open') else: return (1, 'Not Open') # post last week of season last_game_of_season = Game.objects.all().order_by('game_date').last() if now_ > last_game_of_season.game_date: return (last_game_of_season.week_number,'Closed') # all other cases iw = implied_week(now_,0) if now_.weekday() in (3,4,5): return (iw,'Open') elif now_.weekday() == 6: if now_.hour > 12: return (iw,'Closed') else: return (iw,'Open') elif now_.weekday() == 0: iw = implied_week(now_,24) return (iw,'Closed') else: # Tues,Wed if Game.objects.filter(week_number=iw,spread__isnull=True).count() == 0: return (iw,'Open') else: return (iw, 'Not Open')
def handle(self, *args, **options): this_week = implied_week() MAX = 3 for i in range(MAX): scores_have_been_loaded = False for g in Game.objects.filter(week_number=this_week): expected_end_time = g.game_date + timedelta(hours=2, minutes=45) if not (g.isOver()) and now() > expected_end_time: print(now().strftime("%Y-%m-%d, %H:%M:%S") + ": CHECKING FOR NEW FINAL SCORES") load_scores() scores_have_been_loaded = True break if not (scores_have_been_loaded): print(now().strftime("%Y-%m-%d, %H:%M:%S") + ": No games need to be updated at this time") if i < MAX - 1: sleep(300)
def results(week_number,player): games = [] right = 0 total = 0 completed = 0; query = f'SELECT *, pool_pick.picked_fav as picked_fav, (fav_score-udog_score-spread > 0 and picked_fav OR fav_score-udog_score-spread <=0 and not(picked_fav)) as is_correct, pool_team.nick_name as fav_nick_name, pt.nick_name as udog_nick_name FROM pool_game, pool_pick,pool_team, pool_team as pt, auth_user WHERE pool_pick.player_id = auth_user.id and pool_game.week_number = pool_pick.week_number and pool_game.game_number = pool_pick.game_number and pool_game.fav_id = pool_team.id and pool_game.udog_id = pt.id and pool_pick.week_number={week_number} and auth_user.username = "******" ORDER BY pool_game.game_number;' for game in Game.objects.raw(query): total+=1 if game.isOpen(): continue g = {} if game.fav_is_home: g['fav'] = game.fav_nick_name.upper() g['udog'] = game.udog_nick_name.lower() else: g['fav'] = game.fav_nick_name.lower() g['udog'] = game.udog_nick_name.upper() g['fav_score'] = game.fav_score g['udog_score'] = game.udog_score if game.spread is None: g['spread'] = 'NA' else: g['spread'] = game.spread if g['spread'] == 0: g['spread'] = '' if game.is_correct: g['right'] = "Yes" right += 1 else: g['right'] = "No" if game.isOver(): completed += 1 g['picked_fav'] = game.picked_fav g['isOver'] = game.isOver() if game.game_date.timetuple().tm_yday == now().timetuple().tm_yday: if game.game_date < now(): g['game_day'] = 'now' else: g['game_day'] = game.game_date.strftime('%-I:%M') else: g['game_day'] = game.game_date.strftime('%A') games.append(g) return (games,right,total,completed)
def implied_week(now_ = None, delta_hours = 30): if now_ is None: now_ = now() week_number = 1 last_week_of_season = Game.objects.filter(game_number=1).order_by('week_number').last().week_number while week_number < last_week_of_season: if now_ < Game.objects.filter(week_number=week_number).order_by('game_date').last().game_date + timedelta(hours=delta_hours): return week_number else: week_number += 1 return last_week_of_season
def home(request): (headline_week_number,status) = status_() week_number = headline_week_number if (now().weekday() == 1 or now().weekday() == 2) and headline_week_number > 1 and headline_week_number < 18: week_number = headline_week_number-1 # Standings standings = standings_(week_number,False) (total,rank_order) = overall_total() # Blog blog_list = [] for blog in Blog.objects.all().order_by('-entry_date'): if blog.entry_date > now() - timedelta(hours=160) or blog_list == []: blog_list.append([blog.entry_date.strftime('%A %B %-d'), blog.entry]) first_blog_date = blog_list[0][0] first_blog = blog_list[0][1] id = Blog.objects.all().order_by('-entry_date').first().id blog_list.pop(0) completed = Game.objects.filter(week_number=week_number,fav_score__isnull = False).count() total = Game.objects.filter(week_number=week_number).count() src = choose_art() time = now().strftime('%A %B %-d %-I:%M %p') ng_text = '' if now() < Game.objects.all().order_by('game_date').last().game_date: ng = Game.objects.filter(game_date__gt = now()).order_by('game_date').first() game_day = ng.game_date.strftime("%A") if game_day == now().strftime("%A"): game_day = 'Today' ng_text = f'Next Game: {game_day} at {ng.game_date.strftime("%-I:%M%p").lower().replace("pm","p")} ({ng.awayNickName()} @ {ng.homeNickName()})' if status == "Open": message1 = '<font color="#33A532">OPEN</font> until Sunday 1:00 PM' message2 = ng_text elif status == "Not Open": message1 = '<font color="#FFBF00">Not Open Yet.</font>' message2 = ng_text else: message1 = 'Closed' message2 = f'{completed} of {total} games completed' return render(request, 'pool/home.html',{'headline_week_number':headline_week_number, 'time':time, 'message1':message1,'message2':message2, 'status':status, 'src':src, 'total':total, 'completed':completed, 'id':id, 'rest_of_blog':blog_list, 'first_blog_date':first_blog_date, 'first_blog':first_blog, 'player':request.user.username, 'standings':standings, 'overall': rank_order, 'week_number': week_number})
def choose_art(): default_image = 'http://www.tammer.com/home_page_chimp.jpeg' response = requests.get('http://www.tammer.com/poolart/art.csv') if response.status_code != 200: return default_image else: eligible = [] wrapper = list(csv.reader(response.text.strip().split('\n'))) for this_row in wrapper: if len(this_row) != 2: continue if int(this_row[1] ) == 1: eligible.append(this_row[0]) if not(eligible): return default_image else: idx = int(now().minute / 60 * len(eligible)) return eligible[idx]