def process_all_messages(messages):
    if ('error' in messages):
        print('Error = ', error)
        raise Exception('Error in Response')
    if ('data' not in messages):
        return
    while (len(messages['data']) > 0):
        for chat in messages['data']:
            if (len(chat['to']['data']) != 2):
                continue
            friend_name = chat['to']['data'][1]['name'].encode('utf-8')
            if (friend_name == OWNER_NAME):
                friend_name = chat['to']['data'][0]['name'].encode('utf-8')
            success = False
            while (not success):
                try:
                    stdout.write(('\rProcessing Chat With : %s                     ' % friend_name))
                    stdout.flush()
                    process_texts(chat['comments'], friend_name)
                    success = True
                except OAuthError:
                    stdout.write('\nCall Limit Exceeded ! Sleeping for 10 min before retrying !!')
                    stdout.flush()
                    no_of_messages[friend_name] = 0
                    total_chat_length[friend_name] = 0
                    stdout.write('\n')
                    for i in range(600):
                        stdout.write(('\rSleeing.......%d' % i))
                        stdout.flush()
                        time.sleep(1)
                    stdout.write('Woke Up! Retrying !!')
        if (('paging' in messages) and ('next' in messages['paging'])):
            request_str = messages['paging']['next'].replace('https://graph.facebook.com/', '')
            request_str = request_str.replace('limit=25', 'limit=400')
            messages = graph.get(request_str)
        else:
            mesages['data'] = []
def process_all_messages(messages):
    if ('error' in messages):
        print('Error = ', error)
        raise Exception('Error in Response')
    if ('data' not in messages):
        return
    while (len(messages['data']) > 0):
        for chat in messages['data']:
            if (len(chat['to']['data']) != 2):
                continue
            friend_name = chat['to']['data'][1]['name'].encode('utf-8')
            if (friend_name == OWNER_NAME):
                friend_name = chat['to']['data'][0]['name'].encode('utf-8')
            success = False
            while (not success):
                try:
                    stdout.write(('\rProcessing Chat With : %s                     ' % friend_name))
                    stdout.flush()
                    process_texts(chat['comments'], friend_name)
                    success = True
                except OAuthError:
                    stdout.write('\nCall Limit Exceeded ! Sleeping for 10 min before retrying !!')
                    stdout.flush()
                    no_of_messages[friend_name] = 0
                    total_chat_length[friend_name] = 0
                    stdout.write('\n')
                    for i in range(600):
                        stdout.write(('\rSleeing.......%d' % i))
                        stdout.flush()
                        time.sleep(1)
                    stdout.write('Woke Up! Retrying !!')
        if (('paging' in messages) and ('next' in messages['paging'])):
            request_str = messages['paging']['next'].replace('https://graph.facebook.com/', )
            request_str = request_str.replace('limit=25', 'limit=400')
            messages = graph.get(request_str)
        else:
            mesages['data'] = []