Advertisement
Algoritm211

Untitled

Jun 5th, 2020
615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.56 KB | None | 0 0
  1. ----------------tasks.py
  2.  
  3. from testbot.celery import app
  4. from .models import User
  5. import telebot
  6.  
  7. TOKEN = ''
  8.  
  9. bot = telebot.TeleBot(TOKEN)
  10.  
  11.  
  12. @app.task
  13. def send_daily_cryptocurrency():
  14.     all_users = User.objects.all()
  15.     for user in all_users:
  16.         if not user.send_daily_prices:
  17.             bot.send_message(user.user_id, 'У не вас стоит напоминание')
  18.  
  19. --------------celery.py
  20.  
  21. from __future__ import absolute_import, unicode_literals
  22. import os
  23. from celery import Celery
  24. from celery.schedules import crontab
  25. from django.conf import settings
  26.  
  27. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'testbot.settings')
  28. app = Celery('testbot')
  29. app.config_from_object('django.conf:settings')
  30. app.conf.timezone = 'UTC'
  31. app.conf.enable_utc = True
  32. app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
  33.  
  34. ------------views.py
  35. import telebot
  36. from telebot import types
  37. from collections import defaultdict
  38. import datetime
  39. from .models import User
  40. from . import market
  41.  
  42. TOKEN = ''
  43. bot = telebot.TeleBot(TOKEN)
  44. from .tasks import send_daily_cryptocurrency
  45.  
  46. send_daily_cryptocurrency.delay()
  47.  
  48. ----------settings.py
  49. """
  50. Django settings for testbot project.
  51.  
  52. Generated by 'django-admin startproject' using Django 3.0.3.
  53.  
  54. For more information on this file, see
  55. https://docs.djangoproject.com/en/3.0/topics/settings/
  56.  
  57. For the full list of settings and their values, see
  58. https://docs.djangoproject.com/en/3.0/ref/settings/
  59. """
  60.  
  61. import os
  62. import redis
  63. import urllib.parse as urlparse
  64. from celery.schedules import crontab
  65.  
  66. # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
  67. BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  68.  
  69. # Quick-start development settings - unsuitable for production
  70. # See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
  71.  
  72. # SECURITY WARNING: keep the secret key used in production secret!
  73.  
  74. SECRET_KEY = ''
  75.  
  76. # SECURITY WARNING: don't run with debug turned on in production!
  77. DEBUG = True
  78.  
  79. ALLOWED_HOSTS = ['*']
  80.  
  81. # Application definition
  82.  
  83. INSTALLED_APPS = [
  84.     'django.contrib.admin',
  85.     'django.contrib.auth',
  86.     'django.contrib.contenttypes',
  87.     'django.contrib.sessions',
  88.     'django.contrib.messages',
  89.     'django.contrib.staticfiles',
  90.     'bot',
  91.     'django_celery_beat',
  92. ]
  93. MIDDLEWARE = [
  94.     'django.middleware.security.SecurityMiddleware',
  95.     'django.contrib.sessions.middleware.SessionMiddleware',
  96.     'django.middleware.common.CommonMiddleware',
  97.     'django.middleware.csrf.CsrfViewMiddleware',
  98.     'django.contrib.auth.middleware.AuthenticationMiddleware',
  99.     'django.contrib.messages.middleware.MessageMiddleware',
  100.     'django.middleware.clickjacking.XFrameOptionsMiddleware',
  101. ]
  102.  
  103. ROOT_URLCONF = 'testbot.urls'
  104.  
  105. TEMPLATES = [
  106.     {
  107.         'BACKEND': 'django.template.backends.django.DjangoTemplates',
  108.         'DIRS': [],
  109.         'APP_DIRS': True,
  110.         'OPTIONS': {
  111.             'context_processors': [
  112.                 'django.template.context_processors.debug',
  113.                 'django.template.context_processors.request',
  114.                 'django.contrib.auth.context_processors.auth',
  115.                 'django.contrib.messages.context_processors.messages',
  116.             ],
  117.         },
  118.     },
  119. ]
  120.  
  121. WSGI_APPLICATION = 'testbot.wsgi.application'
  122.  
  123. # Database
  124. # https://docs.djangoproject.com/en/3.0/ref/settings/#databases
  125.  
  126. DATABASES = {
  127.     'default': {
  128.         'ENGINE': 'django.db.backends.postgresql_psycopg2',
  129.         'NAME': 'd8mcfajta6ksr',
  130.         'USER': 'bbutkkljbayvxw',
  131.         'PASSWORD': '78352d9a393074248f043e4538c7867e6a860f3957265f2c1836058fb9ce4022',
  132.         'HOST': 'ec2-54-81-37-115.compute-1.amazonaws.com',
  133.         'PORT': '5432'
  134.     }
  135. }
  136.  
  137. # Password validation
  138. # https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
  139.  
  140. AUTH_PASSWORD_VALIDATORS = [
  141.     {
  142.         'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
  143.     },
  144.     {
  145.         'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
  146.     },
  147.     {
  148.         'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
  149.     },
  150.     {
  151.         'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
  152.     },
  153. ]
  154.  
  155. # Internationalization
  156. # https://docs.djangoproject.com/en/3.0/topics/i18n/
  157.  
  158. LANGUAGE_CODE = 'en-us'
  159.  
  160. TIME_ZONE = 'UTC'
  161.  
  162. USE_I18N = True
  163.  
  164. USE_L10N = True
  165.  
  166. USE_TZ = True
  167.  
  168. # AUTH_USER_MODEL = 'core.User'
  169. # Static files (CSS, JavaScript, Images)
  170. # https://docs.djangoproject.com/en/3.0/howto/static-files/
  171.  
  172. STATIC_URL = '/static/'
  173.  
  174. # redis settings for celery
  175. os.environ.setdefault('REDIS_URL',
  176.                       'redis://h:p5067e3205757872a84ea31d841e6cf3ce88f7fcb568d463ff4dc1708d8f8c792@ec2-3-220-244-30.compute-1.amazonaws.com:14059')
  177.  
  178. r = redis.from_url(os.environ.get('REDIS_URL'))
  179. # redis_url = urlparse.urlparse(os.environ.get(REDIS_URL))
  180.  
  181. CACHES = {
  182.     "default": {
  183.         "BACKEND": "redis_cache.RedisCache",
  184.         "LOCATION": os.environ.get('REDIS_URL'),
  185.     }
  186. }
  187.  
  188. BROKER_URL = os.environ['REDIS_URL']
  189. CELERY_RESULT_BACKEND = os.environ['REDIS_URL']
  190. CELERY_BROKER_TRANSPORT_OPTIONS={'visibility_timeout': 3600}
  191. CELERY_TIMEZONE = 'UTC'
  192. CELERY_ENABLE_UTC = True
  193. CELERY_ACCEPT_CONTENT = ['json']
  194. CELERY_TASK_SERIALIZER = 'json'
  195. CELERY_RESULT_SERIALIZER = 'json'
  196. CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler'
  197. CELERY_BEAT_SCHEDULE = {
  198.     'send-daily-crypto': {
  199.         'task': 'bot.tasks.send_daily_cryptocurrency',
  200.         'schedule': crontab(minute='*/2'),
  201.     },
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement