import os import json import requests import shutil from instagram.client import InstagramAPI from project.celery import app from time import sleep from django.conf import settings from apps.config.models import Config @app.task def retrieve_photos(): config = Config.load() api = InstagramAPI( access_token=config.INSTAGRAM_CLIENT_ACCESS_TOKEN, client_secret=config.INSTAGRAM_CLIENT_SECRET, ) recent_media, next_ = api.user_recent_media(user_id='self', count=20) path = os.path.join(settings.BASE_DIR, settings.INSTAGRAM_RESULTS_PATH) for idx, media in enumerate(recent_media): try: fname = os.path.join(path, f'{idx}.jpg') r = requests.get(media.images['standard_resolution'].url, stream=True) if r.status_code == 200: with open(fname, 'wb') as f: r.raw.decode_content = True shutil.copyfileobj(r.raw, f) except AttributeError: pass