<commit_before># -*- coding: utf-8 -*-
from datetime import datetime

import requests
from simplejson import JSONDecodeError

from . import logger, __version__
from .instagram import InstaUser, InstaMedia, InstaApiHashtag, InstaHashtag, InstaFeed
from .session import InstaSession


class InstaLoginRequiredError(Exception):
    pass


class InstaApiClientError(Exception):
    pass


class InstaWebClientError(Exception):
    pass


class BadRequestException(Exception):
    pass


class InvalidAccessTokenException(Exception):
    pass


class NotFoundException(Exception):
    pass


class RateLimitException(Exception):
    pass


class InstaWebClient(object):
    url = 'https://www.instagram.com/'
    url_tag = 'https://www.instagram.com/explore/tags/'
    url_likes = 'https://www.instagram.com/web/likes/%s/like/'
    url_unlike = 'https://www.instagram.com/web/likes/%s/unlike/'
    url_comment = 'https://www.instagram.com/web/comments/%s/add/'
    url_follow = 'https://www.instagram.com/web/friendships/%s/follow/'
    url_unfollow = 'https://www.instagram.com/web/friendships/%s/unfollow/'
    url_login = 'https://www.instagram.com/accounts/login/ajax/'
    url_logout = 'https://www.instagram.com/accounts/logout/'
    url_media_detail = 'https://www.instagram.com/p/%s/?__a=1'
    url_user_detail = 'https://www.instagram.com/%s/?__a=1'

    user_agent = ("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 "
                  "(KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36")
    accept_language = 'ko-KR,ko;q=0.8,en-US;q=0.6,en;q=0.4'

    # If instagram ban you - query return 400 error.
    error_400 = 0
    # If you have 3 400 error in row - looks like you banned.
    error_400_to_ban = 3
    # If InstaClient think you are banned - going to sleep.
    ban_sleep_time = 2 * 60 * 60

    # All counter.
    like_counter = 0
    follow_counter = 0
    unfollow_counter = 0
    comments_counter = 0

    # List of user_id, that bot follow
    bot_follow_list = []

    # Other.
    user_id = 0
    media_by_tag = 0
    login_status = False

    def _login_required(f):
        def check_login(self, *args, **kwargs):
            if not self.s.login_status:
                raise InstaLoginRequiredError('login required')

            return f(self, *args, **kwargs)

        return check_login

    def _check_ratelimit(key):
        def check_ratelimit(f):
            def func(self, *args, **kwargs):
                if self.ratelimit_enabled(key):
                    ratelimit_key = self.get_ratelimit_key(key)
                    if self.cache.get(ratelimit_key) > getattr(self, '%s_per_hour' % key):
                        raise RateLimitException(key)

                return f(self, *args, **kwargs)

            return func

        return check_ratelimit

    def get_ratelimit(self, key=None):
        if not self.cache:
            return

        if key:
            return self.cache.get(self.get_ratelimit_key(key))

        return {
            key: self.cache.get(self.get_ratelimit_key(key))
            for key in ('comment', 'like', 'follow')
        }

    def ratelimit_enabled(self, key):
        return self.cache and getattr(self, '%s_per_hour' % key)

    def get_ratelimit_key(self, key):
        current_hour = datetime.now().hour
        return '_IWC:%s:%s:%02d' % (self.user_login, key, current_hour)

    def inc_ratelimit(self, key):
        if not self.cache:
            return

        ratelimit_key = self.get_ratelimit_key(key)
        logger.debug('ratelimit_key %s' % ratelimit_key)
        self.cache.cache.inc(ratelimit_key)

        rv = self.cache.get(ratelimit_key)
        if rv:
            rv = self.cache.cache.inc(ratelimit_key)
        else:
            self.cache.set(ratelimit_key, 1, timeout=3600)
            rv = 1

        return rv

    def __init__(self, login=None, password=<PASSWORD>, access_token=None, session=None,
                 follow_per_hour=None, like_per_hour=None, comment_per_hour=None, cache=None,):
        if session:
            self.s = session
        else:
            self.s = InstaSession()

        self.api = InstaApiClient(access_token=access_token)

        # convert login to lower
        if login:<commit_msg>RateLimit 확인 키 변경<commit_after># -*- coding: utf-8 -*-
from datetime import datetime

import requests
from simplejson import JSONDecodeError

from . import logger, __version__
from .instagram import InstaUser, InstaMedia, InstaApiHashtag, InstaHashtag, InstaFeed
from .session import InstaSession


class InstaLoginRequiredError(Exception):
    pass


class InstaApiClientError(Exception):
    pass


class InstaWebClientError(Exception):
    pass


class BadRequestException(Exception):
    pass


class InvalidAccessTokenException(Exception):
    pass


class NotFoundException(Exception):
    pass


class RateLimitException(Exception):
    pass


class InstaWebClient(object):
    url = 'https://www.instagram.com/'
    url_tag = 'https://www.instagram.com/explore/tags/'
    url_likes = 'https://www.instagram.com/web/likes/%s/like/'
    url_unlike = 'https://www.instagram.com/web/likes/%s/unlike/'
    url_comment = 'https://www.instagram.com/web/comments/%s/add/'
    url_follow = 'https://www.instagram.com/web/friendships/%s/follow/'
    url_unfollow = 'https://www.instagram.com/web/friendships/%s/unfollow/'
    url_login = 'https://www.instagram.com/accounts/login/ajax/'
    url_logout = 'https://www.instagram.com/accounts/logout/'
    url_media_detail = 'https://www.instagram.com/p/%s/?__a=1'
    url_user_detail = 'https://www.instagram.com/%s/?__a=1'

    user_agent = ("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 "
                  "(KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36")
    accept_language = 'ko-KR,ko;q=0.8,en-US;q=0.6,en;q=0.4'

    # If instagram ban you - query return 400 error.
    error_400 = 0
    # If you have 3 400 error in row - looks like you banned.
    error_400_to_ban = 3
    # If InstaClient think you are banned - going to sleep.
    ban_sleep_time = 2 * 60 * 60

    # All counter.
    like_counter = 0
    follow_counter = 0
    unfollow_counter = 0
    comments_counter = 0

    # List of user_id, that bot follow
    bot_follow_list = []

    # Other.
    user_id = 0
    media_by_tag = 0
    login_status = False

    def _login_required(f):
        def check_login(self, *args, **kwargs):
            if not self.s.login_status:
                raise InstaLoginRequiredError('login required')

            return f(self, *args, **kwargs)

        return check_login

    def _check_ratelimit(key):
        def check_ratelimit(f):
            def func(self, *args, **kwargs):
                if self.ratelimit_enabled(key):
                    ratelimit_key = self.get_ratelimit_key(key)
                    if self.cache.get(ratelimit_key) > getattr(self, '%s_per_hour' % key):
                        raise RateLimitException(key)

                return f(self, *args, **kwargs)

            return func

        return check_ratelimit

    def get_ratelimit(self, key=None):
        if not self.cache:
            return

        if key:
            return self.cache.get(self.get_ratelimit_key(key))

        return {
            key: self.cache.get(self.get_ratelimit_key(key))
            for key in ('comment', 'like', 'follow')
        }

    def ratelimit_enabled(self, key):
        return self.cache and getattr(self, '%s_per_hour' % key)

    def get_ratelimit_key(self, key):
        return 'IWC:RATELIMIT:%s:%s:%s' % (self.user_login, key, datetime.now().strptime('%Y%m%d:%H'))

    def inc_ratelimit(self, key):
        if not self.cache:
            return

        ratelimit_key = self.get_ratelimit_key(key)
        logger.debug('ratelimit_key %s' % ratelimit_key)
        self.cache.cache.inc(ratelimit_key)

        rv = self.cache.get(ratelimit_key)
        if rv:
            rv = self.cache.cache.inc(ratelimit_key)
        else:
            self.cache.set(ratelimit_key, 1, timeout=3600)
            rv = 1

        return rv

    def __init__(self, login=None, password=<PASSWORD>, access_token=None, session=None,
                 follow_per_hour=None, like_per_hour=None, comment_per_hour=None, cache=None,):
        if session:
            self.s = session
        else:
            self.s = InstaSession()

        self.api = InstaApiClient(access_token=access_token)

        # convert login to lower
        if login:
{'fraction_non_alphanumeric': 0.11309383124556842, 'fraction_numerical': 0.01630820137083432, 'mean_word_length': 2.4898969072164947, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 22, 'lorem ipsum': 0, 'www.': 22, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7449488', 'n_tokens_mistral': 2907, 'n_tokens_neox': 2737, 'n_words': 570}