Exemple #1
0
def add_videos_info():
    highlights = latest_highlight_manager.get_all_highlights_without_info()

    logger.log('Total highlights info to add: {}'.format(len(highlights)),
               forward=True)

    for h in highlights:
        info = info_fetcher.get_info(h.link)

        if not info:
            latest_highlight_manager.set_video_duration(h, -1)
            continue

        video_duration = info.get('duration')
        video_url = info.get('video_url')

        if video_duration:
            latest_highlight_manager.set_video_duration(h, video_duration)

            if video_duration > 600:
                # Mark a video of more than 10 minutes extended
                latest_highlight_manager.set_extended_type(h)

        if video_url:
            latest_highlight_manager.set_video_url(h, video_url)
def send_batch_facebook_message(fb_ids, message):
    """
    Send same message to all fb_id
    """
    post_message_url = 'https://graph.facebook.com/v2.6/me/messages?access_token=' + ACCESS_TOKEN
    response_msgs = []

    for i in range(len(fb_ids)):
        response_msgs.append(json.dumps(
            {
                "recipient": {
                    "id": str(fb_ids[i])
                },
                "messaging_type": "MESSAGE_TAG",  # Messaging type is MESSAGE_TAG, NON_PROMOTIONAL_SUBSCRIPTION
                "tag": "NON_PROMOTIONAL_SUBSCRIPTION",
                "message": message
            })
        )

    CLIENT.send_fb_messages_async(post_message_url, response_msgs)

    if not settings.is_prod():
        logger.log(response_msgs)

    return response_msgs
def get_info(link):
    """
    Find the video info for video link

    :param link: the video link
    :return: the info of the video (duration, ressource url)
    """

    for fetcher in ALL_VIDEO_INFO_FETCHER:
        info = None

        try:
            info = fetcher['fetch'](link)

        except TooManyRequestsException:

            # remove temporarily fetcher for which too many request
            for fetcher_from_list in ALL_VIDEO_INFO_FETCHER:

                if fetcher['name'] == fetcher_from_list['name']:
                    logger.log('REMOVING fetcher: ' + fetcher['name'],
                               forward=True)
                    ALL_VIDEO_INFO_FETCHER.remove(fetcher_from_list)

        if info:
            return info

    # Leave duration to 0 to allow a retry
    for fetcher in ALL_VIDEO_INFO_FETCHER:
        if fetcher['name'] in link:
            return {'duration': 0, 'video_url': None}

    return None
Exemple #4
0
def get_video_info(link):

    # Make sure video is from Ok.ru
    if not 'ok.ru' in link:
        return None

    response = None

    try:
        response = requests.get(link)
    except Exception:
        client.captureException()
        logger.log('Ok.ru status: error | Error url: ' + link, forward=True)
        return None

    duration_search_result = re.compile(
        'duration\\\\":\\\\"(.*?)\\\\"',
        0).search(response.text)

    if not duration_search_result:
        return None

    duration = duration_search_result.groups()[0]

    info = {'duration': int(duration), 'video_url': None}

    return info
Exemple #5
0
def _check_validity(highlights):
    for h in highlights:
        try:
            is_valid = ressource_checker.check(h.link)

            if not is_valid:
                logger.log("Invalidated highlight: " + h.link)
                latest_highlight_manager.set_invalid(h)

        except:
            logger.error("Failed to validate link: {}".format(h.link))
Exemple #6
0
def send_facebook_message(fb_id, message):
    response_msg = json.dumps({
        "recipient": {
            "id": fb_id
        },
        "messaging_type": "RESPONSE",  # Messaging type is RESPONSE
        "message": message
    })

    CLIENT.send_fb_message(GRAPH_URL, response_msg)

    if not settings.is_prod():
        logger.log(response_msg)

    return response_msg
def send_facebook_message(fb_id, message):
    post_message_url = 'https://graph.facebook.com/v2.6/me/messages?access_token=' + ACCESS_TOKEN
    response_msg = json.dumps(
        {
            "recipient": {
                "id": fb_id
            },
            "messaging_type": "RESPONSE", # Messaging type is RESPONSE
            "message": message
        })

    CLIENT.send_fb_message(post_message_url, response_msg)

    if not settings.is_prod():
        logger.log(response_msg)

    return response_msg
Exemple #8
0
 def handle(self, *args, **options):
     try:
         start_time = time.time()
         self.run_task()
         # Monitor duration of the task
         logger.log("Task " + str(self.get_task_name()) + " executed in " +
                    str(round(time.time() - start_time, 2)) + "s",
                    forward=True)
     except Exception as error:
         if not settings.DEBUG:
             # Say if PROD or PRE-PROD
             client.user_context({'prod_status': settings.PROD_STATUS})
             # Report to sentry if problem detected
             client.captureException()
             # Report task had a problem
             logger.log("Task " + str(self.get_task_name()) + " failed",
                        forward=True)
         else:
             raise error
def get_video_info(link):

    # Make sure video is from Streamable
    if not providers.STREAMABLE in link:
        return None

    # Get rid of any argument at end of video link
    link = re.compile('[?](.*)', 0).sub('', link)

    search_result = re.compile('/e/(.*)', 0).search(link)

    if not search_result:
        return None

    resource = search_result.groups()[0]

    json = None
    response = None

    try:
        response = requests.get(VIDEO_INFO_ENDPOINT.format(resource))
        json = response.json()
    except JSONDecodeError:
        logger.log('Streamable status: ' + str(response.status_code) + ' | Error url: ' + VIDEO_INFO_ENDPOINT.format(resource), forward=True)

        if response.status_code == requests.codes.too_many_requests:
            raise TooManyRequestsException

    if not json or json.get('error'):
        return None

    video_url = str(json['url'])

    # Check if the video resource url is accessible
    if requests.head(video_url).status_code != requests.codes.ok:
        video_url = None

    info = {
        'duration': int(json['duration']),
        'video_url': video_url
    }

    return info
Exemple #10
0
def send_batch_multiple_facebook_messages(fb_ids, messages):
    """
    Send multiple messages to all fb_id (2 or more message in a row to the same user)
    Does this in a chunk manner so people get the message at the same time in order
    """
    response_msgs = []
    messages_to_send = []

    def chunks(l, n):
        """Yield successive n-sized chunks from list l"""
        for i in range(0, len(l), n):
            yield l[i:i + n]

    for fb_ids_chunk in chunks(fb_ids, 50):  # choose chunks of 50

        for message in messages:

            for fb_id in fb_ids_chunk:

                fb_message = json.dumps({
                    "recipient": {
                        "id": str(fb_id)
                    },
                    "messaging_type":
                    "MESSAGE_TAG",  # Messaging type is MESSAGE_TAG, NON_PROMOTIONAL_SUBSCRIPTION
                    "tag": "NON_PROMOTIONAL_SUBSCRIPTION",
                    "message": message
                })

                messages_to_send.append(fb_message)
                response_msgs.append(fb_message)

            CLIENT.send_fb_messages_async(GRAPH_URL, messages_to_send)
            messages_to_send = []

    if not settings.is_prod():
        logger.log(response_msgs)

    return response_msgs
Exemple #11
0
def get_video_info(link):

    # Make sure video is from Streamable
    if not 'dailymotion.com' in link:
        return None

    # Get rid of any argument at end of video link
    link = re.compile('[?](.*)', 0).sub('', link)

    search_result = re.compile('/video/(.*)', 0).search(link)

    if not search_result:
        return None

    resource = search_result.groups()[0]

    json = None
    response = None

    try:
        response = requests.get(VIDEO_INFO_ENDPOINT.format(resource))
        json = response.json()
    except JSONDecodeError:
        logger.log('Dailymotion status: ' + str(response.status_code) + ' | Error url: ' + VIDEO_INFO_ENDPOINT.format(resource), forward=True)

        # TODO: check if dailymotion send too many requests error code
        if response.status_code == requests.codes.too_many_requests:
            raise TooManyRequestsException

    if not json or json.get('error'):
        return None

    info = {
        'duration': int(json['duration']),
        'video_url': None
    }

    return info
Exemple #12
0
def send_batch_facebook_message(fb_ids, message):
    """
    Send same message to all fb_id
    """
    response_msgs = []

    for i in range(len(fb_ids)):
        response_msgs.append(
            json.dumps({
                "recipient": {
                    "id": str(fb_ids[i])
                },
                "messaging_type":
                "MESSAGE_TAG",  # Messaging type is MESSAGE_TAG, NON_PROMOTIONAL_SUBSCRIPTION
                "tag": "NON_PROMOTIONAL_SUBSCRIPTION",
                "message": message
            }))

    CLIENT.send_fb_messages_async(GRAPH_URL, response_msgs)

    if not settings.is_prod():
        logger.log(response_msgs)

    return response_msgs
def convert(url):
    content = requests.get(url).content
    soup = BeautifulSoup(content, 'html.parser')

    for script in soup.find_all('script'):
        script_text = script.text

        if 'settings.bitrates' in script_text:
            regex = "settings.bitrates = {hls:\"//(.*?)\"}"
            search_result = re.compile(regex, 0).search(script_text)

            video_link = 'http://' + search_result.groups()[0]

            params = (
                ('url', video_link),
            )

            account = random.choice(ACCOUNTS)

            response = requests.get('https://api.streamable.com/import',
                                    params=params,
                                    auth=(account, settings.get_env_var('STREAMABLE_PASSWORD')))

            try:
                shortcode = response.json().get('shortcode')
            except JSONDecodeError:
                logger.log('Failed to create streamable video for: ' + url + ' | error: ' + response.text, forward=True)
                return None

            if shortcode:
                link = 'https://streamable.com/e/' + shortcode
                logger.log('New streamable video: ' + link + ' | for account: ' + account, forward=True)

                return link

    return None
Exemple #14
0
def post(url, data):
    for key in scraper_api_key_manager.get_scraper_api_keys():
        response = requests.post(SCRAPER_POST_URL.format(
            key.code, form_url(url)),
                                 data=data)

        if response.status_code == requests.codes.unauthorized:
            logger.log('Scrapper API key invalid - removing: ' + key.code,
                       forward=True)
            scraper_api_key_manager.remove_key(key)
            continue

        if response.status_code == requests.codes.forbidden:
            logger.log('Scrapper API key too many requests: ' + key.code,
                       forward=True)
            scraper_api_key_manager.invalidate_key(key)
            continue

        if response.status_code == requests.codes.ok and not key.valid:
            logger.log('Scrapper API key reset: ' + key.code, forward=True)
            scraper_api_key_manager.validate_key(key)

        return response
Exemple #15
0
def get(url, render=False):
    render = 'true' if render else 'false'

    for key in scraper_api_key_manager.get_scraper_api_keys():
        response = requests.get(
            SCRAPER_GET_URL.format(key.code, form_url(url), render))

        if response.status_code == requests.codes.unauthorized:
            logger.log('Scrapper API key invalid - removing: ' + key.code,
                       forward=True)
            # scraper_api_key_manager.remove_key(key)
            continue

        if response.status_code == requests.codes.forbidden:
            logger.log('Scrapper API key too many requests: ' + key.code,
                       forward=True)
            scraper_api_key_manager.invalidate_key(key)
            continue

        if response.status_code == requests.codes.ok and not key.valid:
            logger.log('Scrapper API key reset: ' + key.code, forward=True)
            scraper_api_key_manager.validate_key(key)

        return response
Exemple #16
0
def send_most_recent_highlights(footyroom_pagelet=3,
                                hoofoot_pagelet=4,
                                highlightsfootball_pagelet=3,
                                sportyhl_pagelet=3):

    # Fetch highlights from multiple sources
    highlights = fetcher_footyroom.fetch_highlights(num_pagelet=footyroom_pagelet, max_days_ago=7) \
                 + fetcher_hoofoot.fetch_highlights(num_pagelet=hoofoot_pagelet, max_days_ago=7) \
                 + fetcher_highlightsfootball.fetch_highlights(num_pagelet=highlightsfootball_pagelet, max_days_ago=7) \
                 + fetcher_sportyhl.fetch_highlights(num_pagelet=sportyhl_pagelet, max_days_ago=7)

    # Add new highlights
    for highlight in highlights:
        # Parse the date before inserting it (date needs to be a string)
        highlight.time_since_added = str(
            dateparser.parse(highlight.time_since_added))

        if latest_highlight_manager.has_highlight(highlight):
            # Skip if highlight already in database
            continue

        sent = False

        # Mark as sent if a similar highlight (same match, different provider) is in database and has already been sent
        if latest_highlight_manager.get_similar_sent_highlights(highlight):
            sent = True

        latest_highlight_manager.add_highlight(highlight, sent=sent)

    # Set Footyroom infos
    for h in latest_highlight_manager.get_all_highlights_from_source(
            sources=['hoofoot', 'sportyhl', 'highlightsfootball']):
        footyroom_highlight = latest_highlight_manager.get_same_highlight_footyroom(
            h)

        if not footyroom_highlight:
            continue

        latest_highlight_manager.set_img_link(h, footyroom_highlight.img_link)
        latest_highlight_manager.set_goal_data(h,
                                               footyroom_highlight.goal_data)

        # Also add game score for specific sources
        if h.source in ['sportyhl', 'highlightsfootball']:
            latest_highlight_manager.set_score(h, footyroom_highlight.score1,
                                               footyroom_highlight.score2)

    # Send highlights not already sent
    not_sent_highlights = latest_highlight_manager.get_not_sent_highlights(
        AVAILABLE_SOURCES)

    today = datetime.today()

    for highlight in not_sent_highlights:
        time_since_added = highlight.get_parsed_time_since_added()

        # Add time to make sure video is good
        if timedelta(minutes=30) < abs(
                today - time_since_added) or highlight.priority_short > 0:

            if highlight.sent:
                # highlight has already been sent
                continue

            if highlight.score1 < 0 or highlight.score2 < 0:
                # score was not set as no similar video - invalid
                latest_highlight_manager.set_invalid(highlight)
                continue

            # Log highlights sent
            logger.log("Highlight sent: " + highlight.get_match_name(),
                       forward=True)

            # Set highlights for same match to sent
            similar_highlights = latest_highlight_manager.get_similar_highlights(
                highlight, not_sent_highlights)

            for h in similar_highlights:
                latest_highlight_manager.set_sent(h)

            # Send highlight to users
            _send_highlight_to_users(highlight)

    # Delete old highlights
    # FIXME: try to find a way to keep old highlights
    all_highlights = latest_highlight_manager.get_all_highlights()

    for highlight in all_highlights:
        time_since_added = highlight.get_parsed_time_since_added()

        # Old highlight, delete
        if (today - time_since_added) > timedelta(days=60):
            latest_highlight_manager.delete_highlight(highlight)
Exemple #17
0
def send_most_recent_highlights():
    # Footyroom + Hoofoot highlights fetching
    highlights = fetcher_footyroom.fetch_highlights(num_pagelet=1, max_days_ago=2) \
                 + fetcher_hoofoot.fetch_highlights(num_pagelet=4, max_days_ago=7) \
                 + fetcher_footyroom_videos.fetch_highlights(num_pagelet=3, max_days_ago=7)

    # Add new highlights
    for highlight in highlights:
        # Parse the date before inserting it (date needs to be a string)
        highlight.time_since_added = str(
            dateparser.parse(highlight.time_since_added))

        if latest_highlight_manager.has_highlight(highlight):
            # Skip if highlight already in database
            continue

        sent = False

        # Mark as sent if a similar highlight (same match, different provider) is in database and has already been sent
        if latest_highlight_manager.get_similar_sent_highlights(highlight):
            sent = True

        latest_highlight_manager.add_highlight(highlight, sent=sent)

    # Set Footyroom images for hoofoot highlights
    for hoofoot_highlight in latest_highlight_manager.get_all_highlights_from_source(
            source='hoofoot'):
        img_link = latest_highlight_manager.get_highlight_img_link_from_footyroom(
            hoofoot_highlight)

        if not img_link:
            continue

        latest_highlight_manager.set_img_link(hoofoot_highlight, img_link)

    # Send highlights not already sent
    not_sent_highlights = latest_highlight_manager.get_not_sent_highlights()

    today = datetime.today()

    for highlight in not_sent_highlights:
        time_since_added = highlight.get_parsed_time_since_added()

        # Add time to make sure video is good
        if timedelta(minutes=30) < abs(today - time_since_added):

            if highlight.sent:
                # highlight has already been sent
                continue

            # Log highlights sent
            logger.log("Highlight sent: " + highlight.get_match_name())

            # Send highlight for team1
            team1 = highlight.team1.name.lower()
            send_highlight_to_users(highlight, team1)

            # Send highlight for team2
            team2 = highlight.team2.name.lower()
            send_highlight_to_users(highlight, team2)

            # Set highlights for same match to sent
            similar_highlights = latest_highlight_manager.get_similar_highlights(
                highlight, not_sent_highlights)

            for h in similar_highlights:
                latest_highlight_manager.set_sent(h)

    # Delete old highlights
    all_highlights = latest_highlight_manager.get_all_highlights()

    for highlight in all_highlights:
        time_since_added = highlight.get_parsed_time_since_added()

        # Old highlight, delete
        if (today - time_since_added) > timedelta(days=60):
            latest_highlight_manager.delete_highlight(highlight)
Exemple #18
0
    def post(self, request, *args, **kwargs):
        logger.log("Message received: " + str(request.body))

        # Converts the text payload into a python dictionary
        incoming_message = json.loads(request.body.decode('utf-8'))

        response_msg = []

        for entry in incoming_message['entry']:

            for message in entry['messaging']:

                sender_id = message['sender'].get('id')
                HighlightsBotView.LATEST_SENDER_ID = sender_id

                # Send typing event - so user is aware received message
                messenger_manager.send_typing(sender_id)

                user_manager.increment_user_message_count(sender_id)

                logger.log_for_user("Message received: " + str(message),
                                    sender_id)

                # Events
                if 'message' in message:

                    text = message['message'].get(
                        'text') if message['message'].get('text') else ''
                    message = language.remove_accents(text.lower())

                    # Cancel quick reply
                    if 'cancel' in message:
                        print("CANCEL")
                        context_manager.update_context(sender_id,
                                                       ContextType.NONE)

                        response_msg.append(
                            messenger_manager.send_cancel_message(sender_id))

                        # Answer with new message what want to do
                        response_msg.append(
                            messenger_manager.
                            send_anything_else_i_can_do_message(sender_id))

                    # Done quick reply
                    elif 'done' in message:
                        print("DONE")
                        context_manager.update_context(sender_id,
                                                       ContextType.NONE)

                        response_msg.append(
                            messenger_manager.send_done_message(sender_id))

                        # Answer with new message what want to do
                        response_msg.append(
                            messenger_manager.
                            send_anything_else_i_can_do_message(sender_id))

                    # HELP
                    elif 'help' in message:
                        print("HELP")
                        context_manager.update_context(sender_id,
                                                       ContextType.NONE)

                        response_msg.append(
                            messenger_manager.send_help_message(sender_id))

                    elif 'thank you' in message or 'thanks' in message or 'cheers' in message or 'merci' in message:
                        print("THANK YOU MESSAGE")
                        context_manager.update_context(sender_id,
                                                       ContextType.NONE)

                        response_msg.append(
                            messenger_manager.send_than_you_message(sender_id))

                    # TUTORIAL CONTEXT
                    # FIXME: duplication between tutorial and adding team
                    elif context_manager.is_tutorial_context(sender_id):
                        print("TUTORIAL ADD TEAM")

                        team_to_add = message

                        # Check if team exists, make a recommendation if no teams
                        if team_to_add == 'other':
                            response_msg.append(
                                messenger_manager.send_add_team_message(
                                    sender_id))

                        elif football_team_manager.has_football_team(
                                team_to_add):
                            # Does team exist check

                            team_manager.add_team(sender_id, team_to_add)

                            response_msg.append(
                                messenger_manager.send_tutorial_message(
                                    sender_id, text))
                            response_msg.append(
                                messenger_manager.send_tutorial_highlight(
                                    sender_id, team_to_add))

                            context_manager.update_context(
                                sender_id, ContextType.NOTIFICATIONS_SETTING)

                            # Send notification mode FIXME: remove duplication from notification

                            teams = team_manager.get_teams_for_user(sender_id)
                            # Format team names
                            teams = [team.title() for team in teams]

                            response_msg.append(
                                messenger_manager.send_notification_message(
                                    sender_id, teams))

                        elif football_team_manager.similar_football_team_names(
                                team_to_add):
                            # Team recommendation

                            recommendations = football_team_manager.similar_football_team_names(
                                team_to_add)[:messenger_manager.
                                             MAX_QUICK_REPLIES]
                            # Format recommendation names
                            recommendations = [
                                recommendation.title()
                                for recommendation in recommendations
                            ]

                            response_msg.append(
                                messenger_manager.
                                send_recommended_team_tutorial_message(
                                    sender_id, recommendations))

                        else:
                            # No team or recommendation found

                            response_msg.append(
                                messenger_manager.
                                send_team_not_found_tutorial_message(
                                    sender_id))

                    # SEARCH HIGHLIGHT OPTION
                    elif 'search highlights' in message or 'search again' in message:
                        print("SEARCH HIGHLIGHTS")
                        context_manager.update_context(
                            sender_id, ContextType.SEARCH_HIGHLIGHTS)

                        response_msg.append(
                            messenger_manager.send_search_highlights_message(
                                sender_id))

                    # SEARCHING HIGHLIGHTS
                    elif context_manager.is_searching_highlights_context(
                            sender_id):
                        print("SEARCHING HIGHLIGHTS")
                        team_found = messenger_manager.has_highlight_for_team(
                            message)

                        response_msg.append(
                            messenger_manager.send_highlight_message_for_team(
                                sender_id, message))

                        if team_found:
                            context_manager.update_context(
                                sender_id, ContextType.NONE)

                            # Answer with new message what want to do
                            response_msg.append(
                                messenger_manager.
                                send_anything_else_i_can_do_message(sender_id))

                        else:
                            context_manager.update_context(
                                sender_id, ContextType.SEARCH_HIGHLIGHTS)

                    # NOTIFICATION SETTING
                    elif 'my teams' in message:
                        print("NOTIFICATION SETTING")
                        context_manager.update_context(
                            sender_id, ContextType.NOTIFICATIONS_SETTING)

                        teams = team_manager.get_teams_for_user(sender_id)
                        # Format team names
                        teams = [team.title() for team in teams]

                        response_msg.append(
                            messenger_manager.send_notification_message(
                                sender_id, teams))

                    # ADD TEAM SETTING
                    elif 'add' in message and context_manager.is_notifications_setting_context(
                            sender_id):
                        print("ADD TEAM SETTING")
                        context_manager.update_context(sender_id,
                                                       ContextType.ADDING_TEAM)

                        response_msg.append(
                            messenger_manager.send_add_team_message(sender_id))

                    # REMOVE TEAM SETTING
                    elif 'remove' in message and context_manager.is_notifications_setting_context(
                            sender_id):
                        print("REMOVE TEAM SETTING")
                        context_manager.update_context(
                            sender_id, ContextType.DELETING_TEAM)

                        teams = team_manager.get_teams_for_user(sender_id)
                        # Format team names
                        teams = [team.title() for team in teams]

                        response_msg.append(
                            messenger_manager.send_delete_team_message(
                                sender_id, teams))

                    # ADDING TEAM
                    # FIXME: duplication between tutorial and adding team
                    elif context_manager.is_adding_team_context(sender_id):
                        print("ADDING TEAM")

                        team_to_add = message

                        # Check if team exists, make a recommendation if no teams
                        if team_to_add == 'other' or team_to_add == 'try again':
                            context_manager.update_context(
                                sender_id, ContextType.ADDING_TEAM)

                            response_msg.append(
                                messenger_manager.send_add_team_message(
                                    sender_id))

                        elif football_team_manager.has_football_team(
                                team_to_add):
                            # Does team exist check
                            context_manager.update_context(
                                sender_id, ContextType.NOTIFICATIONS_SETTING)

                            team_manager.add_team(sender_id, team_to_add)
                            response_msg.append(
                                messenger_manager.send_team_added_message(
                                    sender_id, True, text))

                            teams = team_manager.get_teams_for_user(sender_id)
                            # Format team names
                            teams = [team.title() for team in teams]

                            response_msg.append(
                                messenger_manager.send_notification_message(
                                    sender_id, teams))

                        elif football_team_manager.similar_football_team_names(
                                team_to_add):
                            # Team recommendation
                            context_manager.update_context(
                                sender_id, ContextType.ADDING_TEAM)

                            recommendations = football_team_manager.similar_football_team_names(
                                team_to_add)[:messenger_manager.
                                             MAX_QUICK_REPLIES]
                            # Format recommendation names
                            recommendations = [
                                recommendation.title()
                                for recommendation in recommendations
                            ]

                            response_msg.append(
                                messenger_manager.
                                send_recommended_team_message(
                                    sender_id, recommendations))

                        else:
                            # No team or recommendation found
                            context_manager.update_context(
                                sender_id, ContextType.ADDING_TEAM)

                            response_msg.append(
                                messenger_manager.send_team_not_found_message(
                                    sender_id))

                    # DELETING TEAM
                    elif context_manager.is_deleting_team_context(sender_id):
                        print("DELETING TEAM")
                        team_to_delete = message.lower()

                        if football_team_manager.has_football_team(
                                team_to_delete):
                            # Delete team
                            team_manager.delete_team(sender_id, team_to_delete)
                            response_msg.append(
                                messenger_manager.send_team_deleted_message(
                                    sender_id, message))

                            teams = team_manager.get_teams_for_user(sender_id)
                            # Format team names
                            teams = [team.title() for team in teams]

                            context_manager.update_context(
                                sender_id, ContextType.NOTIFICATIONS_SETTING)
                            response_msg.append(
                                messenger_manager.send_notification_message(
                                    sender_id, teams))

                        else:
                            # Team to delete not found
                            context_manager.update_context(
                                sender_id, ContextType.DELETING_TEAM)

                            teams = team_manager.get_teams_for_user(sender_id)
                            # Format team names
                            teams = [team.title() for team in teams]

                            response_msg.append(
                                messenger_manager.
                                send_team_to_delete_not_found_message(
                                    sender_id, teams))

                    # IF NO MATCH, UNLESS NAME OF A TEAM IS TYPED, ASK WHAT WANT TO DO
                    else:
                        context_manager.update_context(sender_id,
                                                       ContextType.NONE)

                        if football_team_manager.has_football_team(message):
                            # FIXME: duplication with searching highlights
                            print("NO MATCH - SEARCHING HIGHLIGHTS")
                            response_msg.append(
                                messenger_manager.
                                send_highlight_message_for_team(
                                    sender_id, message))

                            # Answer with new message what want to do
                            response_msg.append(
                                messenger_manager.
                                send_anything_else_i_can_do_message(sender_id))

                        else:
                            print("WHAT WANT TO DO")
                            response_msg.append(
                                messenger_manager.
                                send_what_do_you_want_to_do_message(sender_id))

                elif 'postback' in message:
                    postback = message['postback']['payload']

                    if postback == 'get_started':
                        user = user_manager.get_user(sender_id)

                        response_msg.append(
                            messenger_manager.send_getting_started_message(
                                sender_id, user.first_name))
                        response_msg.append(
                            messenger_manager.send_getting_started_message_2(
                                sender_id))

                        # Set the user in tutorial context
                        context_manager.update_context(
                            sender_id, ContextType.TUTORIAL_ADD_TEAM)

                logger.log_for_user("Message sent: " + str(response_msg),
                                    sender_id)
                HighlightsBotView.LATEST_SENDER_ID = 0

        if not settings.DEBUG:
            return HttpResponse()

        # For DEBUG MODE only
        formatted_response = "["

        for i in range(len(response_msg)):
            formatted_response += response_msg[i]

            if i != len(response_msg) - 1:
                formatted_response += ", "

        formatted_response += "]"

        return JsonResponse(formatted_response, safe=False)
Exemple #19
0
    def post(self, request, *args, **kwargs):

        # Converts the text payload into a python dictionary
        incoming_message = json.loads(request.body.decode('utf-8'))

        logger.log("Message received: " + str(incoming_message))

        response_msg = []

        for entry in incoming_message['entry']:

            for message in entry['messaging']:

                sender_id = message['sender'].get('id')
                HighlightsBotView.LATEST_SENDER_ID = sender_id

                user_manager.increment_user_message_count(sender_id)

                logger.log_for_user("Message received: " + str(message), sender_id)

                # Events
                if 'message' in message:

                    text = message['message'].get('text') if message['message'].get('text') else ''
                    message = language.remove_accents(text.lower())

                    # Do not respond in those cases
                    if 'no' == message or 'nothing' == message or 'ok' == message or 'shut up' in message or message == '':
                        continue

                    # Send typing event - so user is aware received message
                    messenger_manager.send_typing(sender_id)

                    # Special replies
                    # TODO: remove at some point
                    if message == 'add competition ' + EMOJI_TROPHY:
                        logger.log("ADD COMPETITION")

                        context_manager.update_context(sender_id, ContextType.ADDING_REGISTRATION)

                        suggestions_override = ['champions league', 'ligue 1', 'premier league', 'europa league',
                                                'fa cup', 'serie a', 'world cup', 'bundesliga', 'friendly match']

                        response_msg.append(
                            messenger_manager.send_add_registration_message(sender_id,
                                                                            suggestions_override=suggestions_override)
                        )

                    # Special replies
                    # TODO: remove at some point
                    elif message == 'no thanks ' + EMOJI_CROSS:
                        response_msg.append(
                            messenger_manager.send_facebook_message(
                                sender_id, messenger_manager.create_message("Another time! " + EMOJI_SMILE))
                        )

                    # Cancel quick reply
                    elif 'cancel' in message:
                        logger.log("CANCEL")

                        context_manager.set_default_context(sender_id)
                        response_msg.append(
                            messenger_manager.send_cancel_message(sender_id)
                        )

                    # Done quick reply
                    elif 'done' in message:
                        logger.log("DONE")

                        context_manager.set_default_context(sender_id)
                        response_msg.append(
                            messenger_manager.send_done_message(sender_id)
                        )

                    # HELP
                    elif 'help' in message:
                        logger.log("HELP")

                        context_manager.set_default_context(sender_id)
                        response_msg.append(
                            messenger_manager.send_help_message(sender_id)
                        )

                    elif accepted_messages(message, ['thank you', 'thanks', 'cheers', 'merci', 'cimer',
                                                     'good job', 'good bot']):
                        logger.log("THANK YOU MESSAGE")

                        context_manager.set_default_context(sender_id)
                        response_msg.append(
                            messenger_manager.send_thank_you_message(sender_id)
                        )

                    # TUTORIAL CONTEXT
                    # FIXME: duplication between tutorial and registration team
                    elif context_manager.is_tutorial_context(sender_id):
                        logger.log("TUTORIAL ADD REGISTRATION")

                        registration_to_add = message

                        # Check if team exists, make a recommendation if no teams
                        if registration_to_add == 'other':

                            response_msg.append(
                                messenger_manager.send_getting_started_message_2(sender_id)
                            )

                        elif football_team_manager.has_football_team(registration_to_add):
                            # Does team exist check

                            registration_team_manager.add_team(sender_id, registration_to_add)

                            response_msg.append(
                                messenger_manager.send_tutorial_message(sender_id, text)
                            )

                            response_msg.append(
                                messenger_manager.send_tutorial_highlight(sender_id, registration_to_add)
                            )

                            response_msg.append(
                                view_message_helper.send_subscriptions_settings(sender_id)
                            )

                        elif football_team_manager.similar_football_team_names(registration_to_add):
                            # Team recommendation

                            # Register wrong search
                            new_football_registration_manager.add_football_registration(registration_to_add, 'user')

                            # Format recommendation names
                            recommendations = football_team_manager.similar_football_team_names(registration_to_add)
                            recommendations = [recommendation.title() for recommendation in recommendations]

                            response_msg.append(
                                messenger_manager.send_recommended_team_tutorial_message(sender_id, recommendations)
                            )

                        else:
                            # No team or recommendation found

                            # Register wrong search
                            new_football_registration_manager.add_football_registration(registration_to_add, 'user')

                            response_msg.append(
                                messenger_manager.send_team_not_found_tutorial_message(sender_id)
                            )

                    # SEE RESULT CHANGE SETTING
                    elif context_manager.is_see_result_setting_context(sender_id):
                        logger.log("SEE RESULT CHANGE SETTING")

                        if text in [SHOW_BUTTON, HIDE_BUTTON]:
                            user_manager.set_see_result_setting(sender_id, text == SHOW_BUTTON)

                            response_msg.append(
                                messenger_manager.send_setting_changed(sender_id)
                            )

                            context_manager.set_default_context(sender_id)

                        else:
                            response_msg.append(
                                messenger_manager.send_setting_invalid(sender_id)
                            )

                            response_msg.append(
                                messenger_manager.send_see_result_setting(sender_id)
                            )

                    # SUBSCRIPTION SETTING
                    elif accepted_messages(message, ['subscription', 'teams', 'subscribe', 'notification']):
                        logger.log("SUBSCRIPTION SETTING")

                        response_msg.append(
                            view_message_helper.send_subscriptions_settings(sender_id)
                        )

                    # ADD REGISTRATION SETTING
                    elif accepted_messages(message, ['add']) and context_manager.is_notifications_setting_context(sender_id):
                        logger.log("ADD REGISTRATION SETTING")

                        context_manager.update_context(sender_id, ContextType.ADDING_REGISTRATION)

                        response_msg.append(
                            messenger_manager.send_add_registration_message(sender_id)
                        )

                    # REMOVE REGISTRATION SETTING
                    elif accepted_messages(message, ['remove']) and context_manager.is_notifications_setting_context(sender_id):
                        logger.log("REMOVE REGISTRATION SETTING")

                        context_manager.update_context(sender_id, ContextType.REMOVE_REGISTRATION)

                        registrations = view_message_helper.get_registrations_formatted(sender_id)

                        response_msg.append(
                            messenger_manager.send_delete_registration_message(sender_id, registrations)
                        )

                    # ADDING REGISTRATION
                    # FIXME: duplication between tutorial and registration
                    elif context_manager.is_adding_registration_context(sender_id) \
                            or context_manager.is_notifications_setting_context(sender_id):
                        logger.log("ADDING REGISTRATION")

                        registration_to_add = message

                        # Check if registration exists, make a recommendation if no registration
                        if registration_to_add == 'other' or registration_to_add == 'try again':
                            context_manager.update_context(sender_id, ContextType.ADDING_REGISTRATION)

                            response_msg.append(
                                messenger_manager.send_add_registration_message(sender_id)
                            )

                        elif football_team_manager.has_football_team(registration_to_add):
                            # Does team exist check
                            registration_team_manager.add_team(sender_id, registration_to_add)

                            response_msg.append(
                                messenger_manager.send_registration_added_message(sender_id, text)
                            )

                            response_msg.append(
                                view_message_helper.send_subscriptions_settings(sender_id)
                            )

                        elif football_competition_manager.has_football_competition(registration_to_add):
                            # Does competition exist check
                            registration_competition_manager.add_competition(sender_id, registration_to_add)

                            response_msg.append(
                                messenger_manager.send_registration_added_message(sender_id, text)
                            )

                            response_msg.append(
                                view_message_helper.send_subscriptions_settings(sender_id)
                            )

                        elif football_team_manager.similar_football_team_names(registration_to_add) or \
                                football_competition_manager.similar_football_competition_names(registration_to_add):
                            # Registration recommendation
                            context_manager.update_context(sender_id, ContextType.ADDING_REGISTRATION)

                            # Register wrong search
                            new_football_registration_manager.add_football_registration(registration_to_add, 'user')

                            recommendations = football_team_manager.similar_football_team_names(registration_to_add)\
                                              + football_competition_manager.similar_football_competition_names(registration_to_add)

                            # Format recommendation names
                            recommendations = [recommendation.title() for recommendation in recommendations]

                            response_msg.append(
                                messenger_manager.send_recommended_registration_message(sender_id, recommendations)
                            )

                        else:
                            # No registration recommendation found
                            context_manager.update_context(sender_id, ContextType.ADDING_REGISTRATION)

                            # Register wrong search
                            new_football_registration_manager.add_football_registration(registration_to_add, 'user')

                            response_msg.append(
                                messenger_manager.send_registration_not_found_message(sender_id)
                            )

                    # REMOVING REGISTRATION
                    elif context_manager.is_deleting_team_context(sender_id):
                        logger.log("REMOVING REGISTRATION")
                        registration_to_delete = message.lower()

                        if football_team_manager.has_football_team(registration_to_delete):
                            # Delete team
                            registration_team_manager.delete_team(sender_id, registration_to_delete)

                            response_msg.append(
                                messenger_manager.send_registration_deleted_message(sender_id, message)
                            )

                            response_msg.append(
                                view_message_helper.send_subscriptions_settings(sender_id)
                            )

                        elif football_competition_manager.has_football_competition(registration_to_delete):
                            # Delete competition
                            registration_competition_manager.delete_competition(sender_id, registration_to_delete)

                            response_msg.append(
                                messenger_manager.send_registration_deleted_message(sender_id, message)
                            )

                            response_msg.append(
                                view_message_helper.send_subscriptions_settings(sender_id)
                            )

                        else:
                            # Registration to delete not found
                            context_manager.update_context(sender_id, ContextType.REMOVE_REGISTRATION)

                            registrations = view_message_helper.get_registrations_formatted(sender_id)

                            response_msg.append(
                                messenger_manager.send_registration_to_delete_not_found_message(sender_id, registrations)
                            )

                    # SEE RESULT SETTING
                    elif accepted_messages(message, ['see result setting', 'spoiler', 'show result', 'hide result',
                                                     'show score', 'hide score']):
                        logger.log("SEE RESULT SETTING")

                        response_msg.append(
                            view_message_helper.send_send_see_result_settings(sender_id)
                        )

                    # SHARE
                    elif accepted_messages(message, ['share', 'send to a friend']):
                        logger.log("SHARE")

                        response_msg.append(
                            messenger_manager.send_share_introduction_message(sender_id)
                        )
                        response_msg.append(
                            messenger_manager.send_share_message(sender_id)
                        )

                    # SEARCH HIGHLIGHT OPTION
                    elif accepted_messages(message, ['search', 'search again']):
                        logger.log("SEARCH HIGHLIGHTS")

                        response_msg.append(
                            view_message_helper.search_highlights(sender_id)
                        )

                    # SEARCHING HIGHLIGHTS
                    elif context_manager.is_searching_highlights_context(sender_id):
                        logger.log("SEARCHING HIGHLIGHTS")

                        response_msg.append(
                            messenger_manager.send_highlight_message_for_team(sender_id, message)
                        )

                if 'postback' in message:
                    postback = message['postback']['payload']

                    if postback == 'get_started':
                        logger.log("GET STARTED POSTBACK")

                        user = user_manager.get_user(sender_id)

                        response_msg.append(
                            messenger_manager.send_getting_started_message(sender_id, user.first_name)
                        )
                        response_msg.append(
                            messenger_manager.send_getting_started_message_2(sender_id)
                        )

                        # Set the user in tutorial context
                        context_manager.update_context(sender_id, ContextType.TUTORIAL_ADD_TEAM)

                    # SEARCH HIGHLIGHT SETTING POSTBACK
                    elif postback == 'search_highlights':
                        logger.log("SEARCH HIGHLIGHTS POSTBACK")

                        response_msg.append(
                            view_message_helper.search_highlights(sender_id)
                        )

                    # SUBSCRIPTION SETTING POSTBACK
                    elif postback == 'my_subscriptions':
                        logger.log("SUBSCRIPTION SETTING POSTBACK")

                        response_msg.append(
                            view_message_helper.send_subscriptions_settings(sender_id)
                        )

                    # SHARE POSTBACK
                    elif postback == 'share':
                        logger.log("SHARE POSTBACK")

                        response_msg.append(
                            messenger_manager.send_share_introduction_message(sender_id)
                        )
                        response_msg.append(
                            messenger_manager.send_share_message(sender_id)
                        )

                    # SEE RESULT SETTING POSTBACK
                    elif postback == 'see_result_setting':
                        logger.log("SEE RESULT SETTING POSTBACK")

                        response_msg.append(
                            view_message_helper.send_send_see_result_settings(sender_id)
                        )

                logger.log_for_user("Message sent: " + str(response_msg), sender_id)
                HighlightsBotView.LATEST_SENDER_ID = 0

        if not settings.DEBUG:
            return HttpResponse()

        else:
            # DEBUG MODE ONLY
            formatted_response = "[" + ", ".join(response_msg) + "]"
            return JsonResponse(formatted_response, safe=False)
Exemple #20
0
def check_highlight_validity():
    highlights = latest_highlight_manager.get_all_valid_highlights()
    logger.log("Checking validity of {} highlights (all)".format(
        len(highlights)))
    _check_validity(highlights)
Exemple #21
0
def check_recent_highlight_validity():
    recent_highlights = latest_highlight_manager.get_recent_valid_highlights(
        hours=72)
    logger.log("Checking validity of {} recent highlights (< 72 hours)".format(
        len(recent_highlights)))
    _check_validity(recent_highlights)