Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.61 KB | None | 0 0
  1. from core.cache.api import Cache
  2. from core.database.api import Database
  3. from core.database.models import Agent
  4. from datetime import datetime, timedelta
  5. from instagram import Account, AsyncWebAgent, AsyncWebAgentAccount
  6. import json
  7. import logging
  8. from random import randint
  9.  
  10.  
  11. class Logic:
  12.     delay_groups = [
  13.         ("update_account_meta", "make_account_decision"),
  14.         ("update_follows", "update_followers", "update_likes", "update_comments"),
  15.         ("get_follows", "get_followers", "get_media", "get_likes", "get_comments"),
  16.         ("follow", "unfollow", "like", "unlike", "add_comment", "delete_comment"),
  17.         ("check_account",),
  18.     ]
  19.  
  20.     logger = logging.getLogger("tasks")
  21.     database=None
  22.     cache=None
  23.  
  24.     @classmethod
  25.     def set_database(cls, database: Database):
  26.         cls.datatabase = database
  27.  
  28.     @classmethod
  29.     def set_cache(cls, cache: Cache):
  30.         cls.cache = cache
  31.  
  32.     # Common methods
  33.     @classmethod
  34.     async def get_target(cls, name: str, db_agent=None, only_agent=False):
  35.         target = None
  36.         if not db_agent is None:
  37.             target = await cls.cache.get_target(db_agent=db_agent, name=name)
  38.         if not target and not only_agent:
  39.             target = await cls.cache.get_target(name=name)
  40.         return target
  41.  
  42.     @classmethod
  43.     async def add_target(cls, name: str, value, db_agent=None, first=False):
  44.         return await cls.cache.add_target(
  45.             name=name,
  46.             value=value,
  47.             db_agent=db_agent,
  48.             first=first,
  49.         )
  50.  
  51.     @classmethod
  52.     async def get_delay(cls, action: str, db_agent=None):
  53.         if not db_agent is None:
  54.             last = datetime(1970, 1, 1)
  55.             delay_group = []
  56.             for group in cls.delay_groups:
  57.                 if action in group:
  58.                     delay_group = group
  59.                     for a in delay_group:
  60.                         data = await cls.cache.get_agent_action_time(db_agent=db_agent, name=a)
  61.                         if not data is None and data > last:
  62.                             last = data
  63.                     break
  64.  
  65.         delay = None
  66.         if not db_agent is None:
  67.             try:
  68.                 delay = randint(
  69.                     getattr(db_agent.settings, f"{action}_min_delay").value,
  70.                     getattr(db_agent.settings, f"{action}_max_delay").value,
  71.                 )
  72.             except (AttributeError, TypeError, ValueError):
  73.                 pass
  74.  
  75.         if delay is None:
  76.             delay = randint(
  77.                 int(cls.database.get_setting(key=f"{action}_min_delay").value),
  78.                 int(cls.database.get_setting(key=f"{action}_max_delay").value),
  79.             )
  80.  
  81.         if db_agent is None:
  82.             return delay
  83.  
  84.         if last + timedelta(seconds=delay) > datetime.now():
  85.             await cls.cache.set_agent_action_time(
  86.                 db_agent=db_agent,
  87.                 name=action,
  88.                 dt=last + timedelta(seconds=delay),
  89.             )
  90.             return (last - datetime.now() + timedelta(seconds=delay)).total_seconds()
  91.         await cls.cache.set_agent_action_time(db_agent=db_agent, name=action, dt=datetime.now())
  92.         return 0
  93.  
  94.     # Logic
  95.     @classmethod
  96.     async def start(cls, db_agent: Agent, loop):
  97.         pass
  98.  
  99.     @classmethod
  100.     async def stop(cls, db_agent: Agent, loop):
  101.         pass
  102.  
  103.     @classmethod
  104.     async def follow(cls, db_agent: Agent, account: Account):
  105.         pass
  106.  
  107.     @classmethod
  108.     async def unfollow(cls, db_agent: Agent, account: Account):
  109.         pass
  110.  
  111.     @classmethod
  112.     async def get_followers_count(cls):
  113.         raise NotImplementedError
  114.  
  115.     @classmethod
  116.     async def set_followers(cls, db_agent: Agent, account: Account, followers: list):
  117.         pass
  118.  
  119.     @classmethod
  120.     async def get_follows_count(cls):
  121.         raise NotImplementedError
  122.  
  123.     @classmethod
  124.     async def set_follows(cls, db_agent: Agent, account: Account, follows: list):
  125.         pass
  126.  
  127.     @classmethod
  128.     async def get_account_media_count(cls):
  129.         raise NotImplementedError
  130.  
  131.     @classmethod
  132.     async def set_account_media(cls, db_agent: Agent, account: Account, media: list):
  133.         pass
  134.  
  135.     @classmethod
  136.     async def check_account(cls, db_agent: Agent, account: Account, data):
  137.         pass
  138.  
  139.     @classmethod
  140.     async def update_account_meta(cls, account: Account):
  141.         pass
  142.    
  143.     @classmethod
  144.     async def make_account_decision(cls, account: Account):
  145.         pass
  146.  
  147.     @classmethod
  148.     async def add_account_for_scrap(cls, account: Account, db_agent=None, first=False):
  149.         pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement