diff --git a/apps/content/tasks.py b/apps/content/tasks.py index 898266bf..9f58dcaa 100644 --- a/apps/content/tasks.py +++ b/apps/content/tasks.py @@ -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 diff --git a/project/settings.py b/project/settings.py index 2af789b3..c9b37278 100644 --- a/project/settings.py +++ b/project/settings.py @@ -12,6 +12,7 @@ https://docs.djangoproject.com/en/2.0/ref/settings/ import os from collections import OrderedDict +from datetime import timedelta # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -215,13 +216,20 @@ CELERY_BROKER_URL = 'redis://redis:6379/0' CELERY_RESULT_BACKEND = 'redis://redis:6379/1' CELERY_TASK_SERIALIZER = 'json' +CELERY_BEAT_SCHEDULE = { + 'retrieve_photos_from_instagram': { + 'task': 'apps.content.tasks.retrieve_photos', + 'schedule': timedelta(minutes=2), + 'args': (), + }, +} # Dynamic settings CONSTANCE_BACKEND = 'constance.backends.database.DatabaseBackend' CONSTANCE_CONFIG = OrderedDict(( - ('INSTAGRAM_CLIENT_LOGIN', ('', '')), - ('INSTAGRAM_CLIENT_PASSWORD', ('', '')), + ('INSTAGRAM_CLIENT_ACCESS_TOKEN', ('7145314808.f6fa114.ce354a5d876041fc9d3db04b0045587d', '')), + ('INSTAGRAM_CLIENT_SECRET', ('2334a921425140ccb180d145dcd35b25', '')), ('INSTAGRAM_RESULTS_TAG', ('#lil_акварель', 'Тэг результатов работ.')), ('INSTAGRAM_RESULTS_PATH', ('media/instagram/results/', 'Путь до результатов работ.')), ('SERVICE_COMMISSION', (10, 'Комиссия сервиса в процентах.')) diff --git a/project/templates/lilcity/main.html b/project/templates/lilcity/main.html index ece0bbcf..96de641c 100644 --- a/project/templates/lilcity/main.html +++ b/project/templates/lilcity/main.html @@ -192,42 +192,42 @@ diff --git a/requirements.txt b/requirements.txt index 293862f7..a2b3cb6e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ # Python-3.6 +requests==2.18.4 Django==2.0.2 django-anymail[mailgun]==1.2 # paymentwall-python==1.0.7 @@ -18,4 +19,5 @@ drf-yasg[validation]==1.4.0 django-silk==2.0.0 django-cors-headers==2.1.0 django-constance[database]==2.1.0 -InstagramAPI==1.0.2 +# python-instagram==1.3.2 +git+https://github.com/ivlevdenis/python-instagram.git