parent
09e8ed5611
commit
3ba9d395ca
4 changed files with 49 additions and 27 deletions
@ -1,19 +1,31 @@ |
|||||||
|
import os |
||||||
import json |
import json |
||||||
from time import sleep |
import requests |
||||||
from project.celery import app |
import shutil |
||||||
|
|
||||||
from constance import config |
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 |
@app.task |
||||||
def retrieve_photos(): |
def retrieve_photos(): |
||||||
instagram = InstagramAPI( |
api = InstagramAPI( |
||||||
config.INSTAGRAM_CLIENT_LOGIN, |
access_token=config.INSTAGRAM_CLIENT_ACCESS_TOKEN, |
||||||
config.INSTAGRAM_CLIENT_PASSWORD, |
client_secret=config.INSTAGRAM_CLIENT_SECRET, |
||||||
) |
) |
||||||
instagram.login() |
recent_media, next_ = api.user_recent_media(user_id='self', count=20) |
||||||
sleep(1) |
path = os.path.join(settings.BASE_DIR, config.INSTAGRAM_RESULTS_PATH) |
||||||
if instagram.isLoggedIn and instagram.getHashtagFeed(config.INSTAGRAM_RESULTS_TAG): |
for idx, media in enumerate(recent_media): |
||||||
with open('s.json', 'w') as f: |
try: |
||||||
f.write(json.dumps(instagram.LastJson)) |
fname = os.path.join(path, f'{idx}.jpg') |
||||||
return instagram.LastJson |
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