<commit_before>import facebook
from celery.utils.log import get_task_logger
from django.core.exceptions import ImproperlyConfigured

from .utils import get_cached_access_token

try:
    from celery import shared_task, subtask
except ImportError:
    raise ImproperlyConfigured('You need to install a recent version of celery'
                               ' to use the tasks!')

log = get_task_logger(__name__)


@shared_task(bind=True, default_retry_delay=60)
def get_friends_for_user(self, fb_id, callback, next_uri=None):
    """
    Get the facebook friends for the user with fb_id.

    1. Needs a valid access_token in the cache
    2. Needs 'user_friends' permission
    3. Needs a callback function that can store the friends somewhere

    If 1. is not present, the task is delayed
    If 2. is not the case, you're out of luck
    """
    access_token = get_cached_access_token(fb_id)
    if access_token is None:
        raise self.retry(exc=ValueError("Failed to fetch facebook data for %s. "
                                        "No access_token found in cache" % fb_id))

    graph = facebook.GraphAPI(access_token)

    try:
        if next_uri:
            data = graph.bare_request(next_uri)
        else:
            data = graph.get_connections('me', 'friends', limit=500)
    except facebook.GraphAPIError as exc:
        raise self.retry(exc=exc)

    subtask(callback).delay(data['data'])
    if data['paging'].get('next'):
        self.delay(fb_id, callback, next_uri=data['paging']['next'])<commit_msg>Fix KeyError in celery task<commit_after>import facebook
from celery.utils.log import get_task_logger
from django.core.exceptions import ImproperlyConfigured

from .utils import get_cached_access_token

try:
    from celery import shared_task, subtask
except ImportError:
    raise ImproperlyConfigured('You need to install a recent version of celery'
                               ' to use the tasks!')

log = get_task_logger(__name__)


@shared_task(bind=True, default_retry_delay=60)
def get_friends_for_user(self, fb_id, callback, next_uri=None):
    """
    Get the facebook friends for the user with fb_id.

    1. Needs a valid access_token in the cache
    2. Needs 'user_friends' permission
    3. Needs a callback function that can store the friends somewhere

    If 1. is not present, the task is delayed
    If 2. is not the case, you're out of luck
    """
    access_token = get_cached_access_token(fb_id)
    if access_token is None:
        raise self.retry(exc=ValueError("Failed to fetch facebook data for %s. "
                                        "No access_token found in cache" % fb_id))

    graph = facebook.GraphAPI(access_token)

    try:
        if next_uri:
            data = graph.bare_request(next_uri)
        else:
            data = graph.get_connections('me', 'friends', limit=500)
    except facebook.GraphAPIError as exc:
        raise self.retry(exc=exc)

    subtask(callback).delay(data['data'])
    if 'paging' in data and data['paging'].get('next'):
        self.delay(fb_id, callback, next_uri=data['paging']['next'])
{'fraction_non_alphanumeric': 0.0840064620355412, 'fraction_numerical': 0.006462035541195477, 'mean_word_length': 2.994838709677419, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '84349', 'n_tokens_mistral': 946, 'n_tokens_neox': 894, 'n_words': 287}