parent
09e8ed5611
commit
3ba9d395ca
4 changed files with 49 additions and 27 deletions
@ -1,19 +1,31 @@ |
||||
import os |
||||
import json |
||||
from time import sleep |
||||
from project.celery import app |
||||
import requests |
||||
import shutil |
||||
|
||||
from constance import config |
||||
from InstagramAPI import InstagramAPI |
||||
from instagram.client import InstagramAPI |
||||
from project.celery import app |
||||
from time import sleep |
||||
|
||||
from django.conf import settings |
||||
|
||||
|
||||
@app.task |
||||
def retrieve_photos(): |
||||
instagram = InstagramAPI( |
||||
config.INSTAGRAM_CLIENT_LOGIN, |
||||
config.INSTAGRAM_CLIENT_PASSWORD, |
||||
api = InstagramAPI( |
||||
access_token=config.INSTAGRAM_CLIENT_ACCESS_TOKEN, |
||||
client_secret=config.INSTAGRAM_CLIENT_SECRET, |
||||
) |
||||
instagram.login() |
||||
sleep(1) |
||||
if instagram.isLoggedIn and instagram.getHashtagFeed(config.INSTAGRAM_RESULTS_TAG): |
||||
with open('s.json', 'w') as f: |
||||
f.write(json.dumps(instagram.LastJson)) |
||||
return instagram.LastJson |
||||
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 |
||||
|
||||
Loading…
Reference in new issue