You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

34 lines
995 B

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
config = Config.load()
@app.task
def retrieve_photos():
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, config.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