# Path: helper/scheduler.py
# def schedule(every):
#     def decorator(task):
#         task.every = every
#         return task
#     return decorator
# 
# Path: helper/utils/dedup/decorators.py
# def dedup(key):
#     def decorator(task):
#         task.dedup_key = key
#         return task
#     return decorator


import os
import requests
from celery import shared_task
from helper.scheduler import schedule
from helper.utils.dedup.decorators import dedup




def _check_photos(access_token, paginate=False, extra_params=None,
                  taskname=None, task_pair_id=None):
    params = {'access_token': access_token}
    if extra_params is not None:
        params.update(extra_params)

    while True:
        resp = requests.get('https://graph.facebook.com/me/photos',
                            params=params)
        resp.raise_for_status()
        for image in resp.json()['data']:
            yield {
                'user': image['from']['name'],
                'name': ''.join(c for c in image.get('name', '(nocaption)')
                                if c not in '?#'+os.path.sep)[:75],
                'image': image['images'][0]['source'],
                'date': image['created_time'],
                'fbid': image['id'],
            }

        if 'next' in resp.json()['paging'] and paginate:
            params['after'] = resp.json()['paging']['cursors']['after']
        else:
            break


# Path: helper/scheduler.py
# def schedule(every):
#     def decorator(task):
#         task.every = every
#         return task
#     return decorator
# 
# Path: helper/utils/dedup/decorators.py
# def dedup(key):
#     def decorator(task):
#         task.dedup_key = key
#         return task
#     return decorator


# Path: helper/agents/facebook/tasks.py
import os
import requests
from celery import shared_task
from helper.scheduler import schedule
from helper.utils.dedup.decorators import dedup





def _check_photos(access_token, paginate=False, extra_params=None,
                  taskname=None, task_pair_id=None):
    params = {'access_token': access_token}
    if extra_params is not None:
        params.update(extra_params)

    while True:
        resp = requests.get('https://graph.facebook.com/me/photos',
                            params=params)
        resp.raise_for_status()
        for image in resp.json()['data']:
            yield {
                'user': image['from']['name'],
                'name': ''.join(c for c in image.get('name', '(nocaption)')
                                if c not in '?#'+os.path.sep)[:75],
                'image': image['images'][0]['source'],
                'date': image['created_time'],
                'fbid': image['id'],
            }

        if 'next' in resp.json()['paging'] and paginate:
            params['after'] = resp.json()['paging']['cursors']['after']
        else:
            break


@dedup('fbid')