def add_highlight(highlight, sent=False): # Add team to new team if does not exists and return if not has_teams_in_db(highlight): add_new_team_to_db(highlight) return # Add competition to new competition if does not exists and return if not has_competition_in_db(highlight): add_new_competition_to_db(highlight) return team1 = football_team_manager.get_football_team(highlight.team1) team2 = football_team_manager.get_football_team(highlight.team2) category = football_competition_manager.get_football_competition( highlight.category) LatestHighlight.objects.update_or_create( link=highlight.link, img_link=highlight.img_link, time_since_added=highlight.time_since_added, team1=team1, score1=highlight.score1, team2=team2, score2=highlight.score2, category=category, view_count=highlight.view_count, source=highlight.source, sent=sent, goal_data=highlight.goal_data)
def get_same_highlights(highlight): # Add team to new team if does not exists and return if not has_teams_in_db(highlight) or not has_competition_in_db(highlight): return [] team1 = football_team_manager.get_football_team(highlight.team1) team2 = football_team_manager.get_football_team(highlight.team2) return LatestHighlight.objects.filter(team1=team1, team2=team2, time_since_added__gt=highlight.get_parsed_time_since_added() - timedelta(days=3), time_since_added__lt=highlight.get_parsed_time_since_added() + timedelta(days=3))
def add_highlight(highlight, sent=False): # Add team to new team if does not exists and return if not has_teams_in_db(highlight): add_new_team_to_db(highlight) return None # Add competition to new competition if does not exists and return if not has_competition_in_db(highlight): add_new_competition_to_db(highlight) return None # Run mapping for football team names as team can be named differently mapped_team1 = mapping_football_team.get_exact_name(highlight.team1) mapped_team2 = mapping_football_team.get_exact_name(highlight.team2) team1 = football_team_manager.get_football_team(mapped_team1) team2 = football_team_manager.get_football_team(mapped_team2) # Run mapping for football competition/category names as they can be named differently mapped_competition = mapping_football_competition.get_exact_name(highlight.category) category = football_competition_manager.get_football_competition(mapped_competition) # Get the match time and id match_time = datetime.fromordinal(highlight.time_since_added.date().toordinal()) id = get_new_id() oldest = get_oldest_same_highlight(highlight) if oldest: match_time = oldest.match_time id = oldest.id new_highlight, _ = LatestHighlight.objects.update_or_create(id=id, link=highlight.link, img_link=highlight.img_link, time_since_added=highlight.time_since_added, match_time=match_time, team1=team1, score1=highlight.score1, team2=team2, score2=highlight.score2, category=category, view_count=highlight.view_count, source=highlight.source, sent=sent, goal_data=highlight.goal_data, type=highlight.type) return new_highlight
def get_similar_sent_highlights(highlight): # Add team to new team if does not exists and return if not has_teams_in_db(highlight) or not has_competition_in_db(highlight): return [] team1 = football_team_manager.get_football_team(highlight.team1) team2 = football_team_manager.get_football_team(highlight.team2) return [ h for h in LatestHighlight.objects.filter( team1=team1, team2=team2, sent=True) if abs(highlight.get_parsed_time_since_added() - h.get_parsed_time_since_added()) < timedelta(days=2) ]
def add_notification_stat(fb_id, highlight): user = user_manager.get_user(fb_id) send_time = datetime.now() match_time = highlight.get_parsed_time_since_added() team1 = football_team_manager.get_football_team(highlight.team1) team2 = football_team_manager.get_football_team(highlight.team2) HighlightNotificationStat.objects.update_or_create(user=user, team1=team1, score1=highlight.score1, team2=team2, score2=highlight.score2, match_time=str(match_time), send_time=send_time)
def run_task(self, options): for n in mapping_football_team.NAME_MAPPING: name = mapping_football_team.NAME_MAPPING[n] try: team = football_team_manager.get_football_team(name) FootballTeamMapping.objects.update_or_create(team_name=n, team=team) # print(n, team) except IndexError: print('Error with team: ' + str(n)) for n in mapping_football_competition.NAME_MAPPING: name = mapping_football_competition.NAME_MAPPING[n] try: competition = football_competition_manager.get_football_competition( name) FootballCompetitionMapping.objects.update_or_create( competition_name=n, competition=competition) # print(n, competition) except IndexError: print('Error with competition: ' + str(n))
def get_highlights(team1, score1, team2, score2, date): if not has_team(team1) or not has_team(team2): return None team1 = football_team_manager.get_football_team(team1) team2 = football_team_manager.get_football_team(team2) return LatestHighlight.objects.filter(team1=team1, team2=team2, score1=score1, score2=score2, valid=True, ready=True, source__in=sources.get_available_sources(), time_since_added__gt=date - timedelta(days=3), time_since_added__lt=date + timedelta(days=3))
def get_similar_sent_highlights(highlight): # Add team to new team if does not exists and return if add_new_team_to_db(highlight): return [] team1 = football_team_manager.get_football_team(highlight.team1) team2 = football_team_manager.get_football_team(highlight.team2) return [ h for h in LatestHighlight.objects.filter(team1=team1, team2=team2, score1=highlight.score1, score2=highlight.score2, sent=True) if abs(highlight.get_parsed_time_since_added() - h.get_parsed_time_since_added()) < timedelta(days=2) ]
def add_blocked_competition_highlight(team, competition=None): team = football_team_manager.get_football_team(team) if competition: competition = football_competition_manager.get_football_competition( competition) BlockedNotification.objects.update_or_create(team=team, competition=competition)
def get_highlights_for_team(team_name): if not football_team_manager.has_football_team(team_name): return None team = football_team_manager.get_football_team(team_name) highlights = [highlight for highlight in LatestHighlight.objects.filter(team1=team)] \ + [highlight for highlight in LatestHighlight.objects.filter(team2=team)] return highlights
def add_highlight(highlight, sent=False): # Add team to new team if does not exists and return if add_new_team_to_db(highlight): return team1 = football_team_manager.get_football_team(highlight.team1) team2 = football_team_manager.get_football_team(highlight.team2) LatestHighlight.objects.update_or_create( link=highlight.link, img_link=highlight.img_link, time_since_added=highlight.time_since_added, team1=team1, score1=highlight.score1, team2=team2, score2=highlight.score2, category=highlight.category, view_count=highlight.view_count, source=highlight.source, sent=sent)
def get_highlights_for_team_or_competition(team_or_competition_name): highlights = [] # team if has_team(team_or_competition_name): team = football_team_manager.get_football_team(team_or_competition_name) highlights = LatestHighlight.objects.filter( Q(team1=team, sent=True, valid=True, ready=True, score1__gte=0, score2__gte=0, source__in=sources.get_available_sources()) | Q(team2=team, sent=True, valid=True, ready=True, score1__gte=0, score2__gte=0, source__in=sources.get_available_sources()))\ .order_by('-match_time', '-time_since_added') # competition elif has_competition(team_or_competition_name): competition = football_competition_manager.get_football_competition(team_or_competition_name) highlights = LatestHighlight.objects.filter(category=competition, sent=True, valid=True, ready=True, score1__gte=0, score2__gte=0, source__in=sources.get_available_sources())\ .order_by('-match_time', '-time_since_added') return list(highlights)
def get_users_for_team(team_name): team = football_team_manager.get_football_team(team_name) teams = Team.objects.filter(team_name=team) return [team.user.facebook_id for team in teams]
def delete_team(fb_id, team_name): team = football_team_manager.get_football_team(team_name) Team.objects.filter(user_id=fb_id, team_name=team).delete()
def add_team(fb_id, team_name): team = football_team_manager.get_football_team(team_name) Team.objects.update_or_create(user_id=fb_id, team_name=team)