diff --git a/apps/content/tasks.py b/apps/content/tasks.py new file mode 100644 index 00000000..898266bf --- /dev/null +++ b/apps/content/tasks.py @@ -0,0 +1,19 @@ +import json +from time import sleep +from project.celery import app +from constance import config +from InstagramAPI import InstagramAPI + + +@app.task +def retrieve_photos(): + instagram = InstagramAPI( + config.INSTAGRAM_CLIENT_LOGIN, + config.INSTAGRAM_CLIENT_PASSWORD, + ) + 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 diff --git a/docker-compose.yml b/docker-compose.yml index 5ed0b737..647430e4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -35,3 +35,22 @@ services: links: - db - redis + + workers: + build: . + restart: always + volumes: + - .:/lilcity + command: bash -c "celery worker -A project -B" + environment: + - DJANGO_SETTINGS_MODULE=project.settings + - DATABASE_SERVICE_HOST=db + - REDIS_SERVICE_HOST=redis + depends_on: + - db + - redis + - web + links: + - db + - redis + - web diff --git a/project/celery.py b/project/celery.py index 8cbb640c..bbc97ed5 100644 --- a/project/celery.py +++ b/project/celery.py @@ -10,7 +10,7 @@ app = Celery('project') # the configuration object to child processes. # - namespace='CELERY' means all celery-related configuration keys # should have a `CELERY_` prefix. -app.config_from_object('project.celery_settings') +app.config_from_object('project.settings', namespace='CELERY') # Load task modules from all registered Django app configs. app.autodiscover_tasks() diff --git a/project/celery_settings.py b/project/celery_settings.py deleted file mode 100644 index 8afa610e..00000000 --- a/project/celery_settings.py +++ /dev/null @@ -1,3 +0,0 @@ -broker_url = 'redis://redis:6379/0' -result_backend = 'redis://redis:6379/1' -task_serializer = 'json' diff --git a/project/settings.py b/project/settings.py index 754f766b..8bfd8c00 100644 --- a/project/settings.py +++ b/project/settings.py @@ -11,6 +11,7 @@ https://docs.djangoproject.com/en/2.0/ref/settings/ """ import os +from collections import OrderedDict # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -47,6 +48,8 @@ INSTALLED_APPS = [ 'rest_framework.authtoken', 'drf_yasg', 'corsheaders', + 'constance', + 'constance.backends.database', ] + [ 'apps.auth.apps', 'apps.user', @@ -206,6 +209,29 @@ REST_FRAMEWORK = { 'PAGE_SIZE': 10, } +# Celery settings + +CELERY_BROKER_URL = 'redis://redis:6379/0' +CELERY_RESULT_BACKEND = 'redis://redis:6379/1' +CELERY_TASK_SERIALIZER = 'json' + + +# Dynamic settings + +CONSTANCE_BACKEND = 'constance.backends.database.DatabaseBackend' +CONSTANCE_CONFIG = OrderedDict(( + ('INSTAGRAM_CLIENT_LOGIN', ('', '')), + ('INSTAGRAM_CLIENT_PASSWORD', ('', '')), + ('INSTAGRAM_RESULTS_TAG', ('#lil_акварель', 'Тэг результатов работ.')), + ('INSTAGRAM_RESULTS_PATH', ('media/instagram/results/', 'Путь до результатов работ.')), +)) + +try: + from .local_settings import * +except ImportError: + pass + + # CORS settings if DEBUG: diff --git a/requirements.txt b/requirements.txt index 201a16c1..293862f7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -17,3 +17,5 @@ djangorestframework==3.7.7 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