Exemplo n.º 1
0
def send_post(submission, r2t):
    config_filename = 'configs/prod.yml'
    with open(config_filename) as config_file:
        config = yaml.load(config_file.read())
    with open(config['cron_file']) as tsv_file:
        tsv_reader = csv.DictReader(tsv_file, delimiter='\t')
        for row in tsv_reader:
            time.sleep(10)
            submodule_name = row['submodule_name']
            submodule = importlib.import_module(
                'channels.{}.app'.format(submodule_name))
            channel_name = submodule.t_channel
            stat_to_store = {
                'channel': channel_name.lower(),
                'ts': datetime.utcnow(),
                'submodule': submodule_name
            }
            try:
                stat_to_store['admins'] = get_admins_list(r2t, channel_name)
            except Exception as e:
                logging.error('Failed to get admins for {channel}.'.format(
                    channel=channel_name))
            time.sleep(2)
            try:
                stat_to_store[
                    'members_cnt'] = r2t.telepot_bot.getChatMembersCount(
                        channel_name)
            except Exception as e:
                logging.error(
                    'Failed to get members count for {channel}.'.format(
                        channel=channel_name))
            r2t.stats.insert_one(stat_to_store)
    # It's not a proper supply, so just stop.
    return SupplyResult.STOP_THIS_SUPPLY
Exemplo n.º 2
0
def send_post(submission, r2t):
    r2t.send_text('Regular bypass started.')
    time.sleep(1)
    config_filename = 'configs/prod.yml'
    with open(config_filename) as config_file:
        config = yaml.load(config_file.read())
    total = {'channels': 0, 'members': 0, 'admins': 0, 'errors': 0}
    all_channels = set()
    with open(config['cron_file']) as tsv_file:
        tsv_reader = csv.DictReader(tsv_file, delimiter='\t')
        for row in tsv_reader:
            submodule_name = row['submodule_name']
            all_channels.add(submodule_name)
    for submodule_name in all_channels:
        time.sleep(10)
        submodule = importlib.import_module(
            'channels.{}.app'.format(submodule_name))
        channel_name = submodule.t_channel
        stat_to_store = {
            'channel': channel_name.lower(),
            'ts': datetime.utcnow(),
            'submodule': submodule_name
        }
        total['channels'] += 1
        try:
            admins = get_admins_list(r2t, channel_name)
            stat_to_store['admins'] = admins
            total['admins'] += len(admins)
        except Exception as e:
            total['errors'] += 1
            logging.error('Failed to get admins for {channel}.'.format(
                channel=channel_name))
        time.sleep(2)
        try:
            members = r2t.telepot_bot.getChatMembersCount(channel_name)
            stat_to_store['members_cnt'] = members
            total['members'] += members
        except Exception as e:
            total['errors'] += 1
            logging.error('Failed to get members count for {channel}.'.format(
                channel=channel_name))
        r2t.stats.insert_one(stat_to_store)
    text_to_send = 'Ok, regular bypass results.\n\n'
    text_to_send += '<pre>Active channels: {n}.</pre>\n'.format(
        n=total['channels'])
    text_to_send += '<pre>Subscribers: {n}.</pre>\n'.format(n=total['members'])
    text_to_send += '<pre>Admins: {n}.</pre>\n'.format(n=total['admins'])
    text_to_send += '<pre>Errors during bypass: {n}.</pre>\n'.format(
        n=total['errors'])
    text_to_send += '\n<i>See you!</i>'
    r2t.send_text(text_to_send, parse_mode='HTML')
    # It's not a proper supply, so just stop.
    return SupplyResult.STOP_THIS_SUPPLY
Exemplo n.º 3
0
def send_post(submission, r2t):
    r2t.send_text('Regular bypass started.')
    time.sleep(1)
    total = {'channels': 0, 'members': 0, 'admins': 0, 'errors': 0}
    all_submodules = get_all_submodules()
    for submodule_name in random.sample(all_submodules, k=len(all_submodules)):
        time.sleep(10)
        submodule = importlib.import_module(
            'channels.{}.app'.format(submodule_name))
        channel_name = submodule.t_channel
        stat_to_store = {
            'channel': channel_name.lower(),
            'ts': datetime.utcnow(),
            'submodule': submodule_name
        }
        total['channels'] += 1
        try:
            admins = get_admins_list(r2t, channel_name)
            stat_to_store['admins'] = admins
            total['admins'] += len(admins)
        except Exception as e:
            total['errors'] += 1
            logging.error('Failed to get admins for {channel}.'.format(
                channel=channel_name))
        time.sleep(2)
        try:
            members = r2t.telepot_bot.getChatMembersCount(channel_name)
            stat_to_store['members_cnt'] = members
            total['members'] += members
        except Exception as e:
            total['errors'] += 1
            logging.error('Failed to get members count for {channel}.'.format(
                channel=channel_name))
        r2t.stats.insert_one(stat_to_store)
    text_to_send = 'Ok, regular bypass results.\n\n'
    text_to_send += '<pre>Active channels: {n}.</pre>\n'.format(
        n=total['channels'])
    text_to_send += '<pre>Subscribers: {n}.</pre>\n'.format(n=total['members'])
    text_to_send += '<pre>Admins: {n}.</pre>\n'.format(n=total['admins'])
    text_to_send += '<pre>Errors during bypass: {n}.</pre>\n'.format(
        n=total['errors'])
    text_to_send += '\n<i>See you!</i>'
    r2t.send_text(text_to_send, parse_mode='HTML')
    # It's not a proper supply, so just stop.
    return SupplyResult.STOP_THIS_SUPPLY
Exemplo n.º 4
0
def send_post(submission, r2t):
    # To check previous achievements
    config = get_config()
    db = pymongo.MongoClient(host=config['db']['host'])[config['db']['name']]
    settings = db['settings']
    # Start logging!
    r2t.send_text('Regular bypass started.')
    short_sleep()
    total = {
        'channels': 0,
        'members': 0,
        'admins': 0,
        'errors': 0,
        'prev_members': 0
    }
    all_submodules = get_all_submodules()
    number_of_modules = len(all_submodules)
    # sleep_coef = math.log(450 / number_of_modules) / math.log(2.718281828 / 3.14159)
    sleep_coef = 2000 / 3 / 2 / number_of_modules
    channels_stat = dict()
    for submodule_name in random.sample(all_submodules, k=number_of_modules):
        short_sleep(sleep_coef)
        submodule = utils.channels_stuff.import_submodule(submodule_name)
        channel_name = submodule.t_channel
        stat_to_store = {
            'channel': channel_name.lower(),
            'ts': datetime.utcnow(),
            'submodule': submodule_name
        }
        total['channels'] += 1
        try:
            admins = get_admins_list(r2t, channel_name)
            stat_to_store['admins'] = admins
            total['admins'] += len(admins)
        except Exception as e:
            total['errors'] += 1
            err_to_send = 'Failed to get admins for {channel}.'.format(
                channel=channel_name)
            r2t.send_text(err_to_send)
            logging.error(err_to_send)
        short_sleep(sleep_coef)
        try:
            current_members_cnt = r2t.telegram_bot.get_chat_members_count(
                chat_id=channel_name)
            stat_to_store['members_cnt'] = current_members_cnt
            total['members'] += current_members_cnt
            prev_members_cnt = get_last_members_cnt(r2t, channel_name)
            total['prev_members'] += prev_members_cnt
            channels_stat[channel_name] = {
                'prev_cnt':
                prev_members_cnt,
                'curr_cnt':
                current_members_cnt,
                'diff':
                current_members_cnt - prev_members_cnt,
                'perc_diff':
                (current_members_cnt - prev_members_cnt) / prev_members_cnt
            }
        except Exception as e:
            total['errors'] += 1
            err_to_send = 'Failed to get members count for {channel}.'.format(
                channel=channel_name)
            r2t.send_text(err_to_send)
            logging.error(err_to_send)
        else:
            # If they pass something special
            for achievement in GREAT_ACHIEVEMENTS:
                if (prev_members_cnt < achievement) and (achievement <=
                                                         current_members_cnt):
                    # Achievement reached
                    r2t.send_text('🏆 {channel}\n{n1} ➡️ {n2}'.format(
                        n1=prev_members_cnt,
                        n2=current_members_cnt,
                        channel=channel_name))
                    setting_result = settings.find_one(
                        {'setting': SETTING_NAME})
                    if setting_result is None:
                        set_achievement(submodule_name, channel_name,
                                        achievement, settings)
                    elif channel_name.lower(
                    ) not in setting_result['channels']:
                        set_achievement(submodule_name, channel_name,
                                        achievement, settings)
                    elif achievement not in setting_result['channels'][
                            channel_name.lower()]:
                        set_achievement(submodule_name, channel_name,
                                        achievement, settings)
                    else:
                        # Was already archived
                        long_sleep()

        r2t.stats.insert_one(stat_to_store)

    members_diff = total['members'] - total['prev_members']
    perc_diff = round((members_diff / total['prev_members']) * 100, 2)
    if members_diff < 0:
        sign = ''
    elif members_diff == 0:
        sign = '±'
    else:
        sign = '+'

    text_to_send = 'Ok, regular bypass results.\n\n'
    text_to_send += '<pre>Active channels: {n}.</pre>\n'.format(
        n=total['channels'])
    text_to_send += '<pre>Subscribers: {n}.</pre>\n'.format(n=total['members'])
    text_to_send += '<pre>Cnt diff: {sign}{diff} ({sign}{perc_diff}%).</pre>\n'.format(
        n=total['members'], sign=sign, diff=members_diff, perc_diff=perc_diff)
    text_to_send += '<pre>Admins: {n}.</pre>\n'.format(n=total['admins'])
    text_to_send += '<pre>Errors during bypass: {n}.</pre>\n'.format(
        n=total['errors'])
    text_to_send += '\n<i>See you!</i>'
    r2t.send_text(text_to_send, parse_mode='HTML')
    short_sleep()
    text_to_send = '📈 <b>TOP 5 GAIN</b> 📈\n'
    top_5_gain = dict(
        sorted(channels_stat.items(),
               key=lambda item: item[1]['diff'],
               reverse=True)[:5])
    bottom_5_gain = dict(
        sorted(channels_stat.items(), key=lambda item: item[1]['diff'])[:5])
    for k, v in top_5_gain.items():
        if v['diff'] < 0:
            sign = ''
        elif v['diff'] == 0:
            sign = '±'
        else:
            sign = '+'
        text_to_send += '{channel}: {from_cnt} {s}{diff} ({s}{perc_diff}%) → {to_cnt}\n'.format(
            channel=k,
            from_cnt=v['prev_cnt'],
            s=sign,
            diff=v['diff'],
            to_cnt=v['curr_cnt'],
            perc_diff=round(v['perc_diff'] * 100, 2))
    r2t.send_text(text_to_send, parse_mode='HTML')
    short_sleep()
    text_to_send = '📉 <b>TOP 5 FALL</b> 📉\n'
    for k, v in bottom_5_gain.items():
        if v['diff'] < 0:
            sign = ''
        elif v['diff'] == 0:
            sign = '±'
        else:
            sign = '+'
        text_to_send += '{channel}: {from_cnt} {s}{diff} ({s}{perc_diff}%) → {to_cnt}\n'.format(
            channel=k,
            from_cnt=v['prev_cnt'],
            s=sign,
            diff=v['diff'],
            to_cnt=v['curr_cnt'],
            perc_diff=round(v['perc_diff'] * 100, 2))
    r2t.send_text(text_to_send, parse_mode='HTML')
    # It's not a proper supply, so just stop.
    return SupplyResult.STOP_THIS_SUPPLY
Exemplo n.º 5
0
def send_post(submission, r2t):
    def say_congrats(submodule_name, channel, achievement):
        short_sleep()
        config = get_config()
        submodule = importlib.import_module('channels.{}.app'.format(submodule_name))
        subreddit_name = submodule.subreddit
        send_to_channel_from_subreddit(
            how_to_post=make_nice_submission,
            channel_to_post='@reddit2telegram',
            subreddit=subreddit_name,
            submodule_name_to_promte=submodule_name,
            submissions_ranking='top',
            submissions_limit=1000,
            config=config,
            extra_args=True,
            extra_ending='🏆 Great achievement!\n💪 Milestone of {number} subscribers.'.format(
                number=achievement
            )
        )
        long_sleep()
    def set_achievement(submodule_name, channel, achievement):
        if settings.find_one({'setting': SETTING_NAME}) is None:
            settings.insert_one({
                'setting': SETTING_NAME,
                'channels': {
                    channel.lower(): [achievement]
                }
            })
        else:
            current_state = settings.find_one({'setting': SETTING_NAME})
            channels = current_state['channels']
            if channel.lower() in channels:
                channels[channel.lower()].append(achievement)
            else:
                channels[channel.lower()] = [achievement]
            settings.find_one_and_update(
                {
                    'setting': SETTING_NAME
                },
                {
                    '$set': 
                    {
                        'channels': channels
                    }
                }
            )
        say_congrats(submodule_name, channel, achievement)
    # To check previous achievements
    config = get_config()
    db = pymongo.MongoClient(host=config['db']['host'])[config['db']['name']]
    settings = db['settings']
    # Start logging!
    r2t.send_text('Regular bypass started.')
    short_sleep()
    total = {
        'channels': 0,
        'members': 0,
        'admins': 0,
        'errors': 0,
        'prev_members': 0
    }
    all_submodules = get_all_submodules()
    for submodule_name in random.sample(all_submodules, k=len(all_submodules)):
        short_sleep(SLEEP_COEF)
        submodule = importlib.import_module('channels.{}.app'.format(submodule_name))
        channel_name = submodule.t_channel
        stat_to_store = {
            'channel': channel_name.lower(),
            'ts': datetime.utcnow(),
            'submodule': submodule_name
        }
        total['channels'] += 1
        try:
            admins = get_admins_list(r2t, channel_name)
            stat_to_store['admins'] = admins
            total['admins'] += len(admins)
        except Exception as e:
            total['errors'] += 1
            err_to_send = 'Failed to get admins for {channel}.'.format(channel=channel_name)
            r2t.send_text(err_to_send)
            logging.error(err_to_send)
        short_sleep(SLEEP_COEF)
        try:
            current_members_cnt = r2t.telegram_bot.get_chat_members_count(chat_id=channel_name)
            stat_to_store['members_cnt'] = current_members_cnt
            total['members'] += current_members_cnt
            prev_members_cnt = get_last_members_cnt(r2t, channel_name)
            total['prev_members'] += prev_members_cnt
        except Exception as e:
            total['errors'] += 1
            err_to_send = 'Failed to get members count for {channel}.'.format(channel=channel_name)
            r2t.send_text(err_to_send)
            logging.error(err_to_send)
        else:
            # If they pass something special
            for achievement in GREAT_ACHIEVEMENTS:
                if (prev_members_cnt < achievement) and (achievement <= current_members_cnt):
                    # Achievement reached
                    r2t.send_text('🏆 {channel}\n{n1} ➡️ {n2}'.format(
                        n1=prev_members_cnt,
                        n2=current_members_cnt,
                        channel=channel_name
                    ))
                    setting_result = settings.find_one({'setting': SETTING_NAME})
                    if setting_result is None:
                        set_achievement(submodule_name, channel_name, achievement)
                    elif channel_name.lower() not in setting_result['channels']:
                        set_achievement(submodule_name, channel_name, achievement)
                    elif achievement not in setting_result['channels'][channel_name.lower()]:
                        set_achievement(submodule_name, channel_name, achievement)
                    else:
                        # Was already archived
                        long_sleep()
        
        r2t.stats.insert_one(stat_to_store)

    members_diff = total['members'] - total['prev_members']
    perc_diff = round((members_diff / total['prev_members']) * 100, 2)
    if members_diff < 0:
        sign = ''
    elif members_diff == 0:
        sign = '±'
    else:
        sign = '+'

    text_to_send = 'Ok, regular bypass results.\n\n'
    text_to_send += '<pre>Active channels: {n}.</pre>\n'.format(n=total['channels'])
    text_to_send += '<pre>Subscribers: {n}.</pre>\n'.format(n=total['members'])
    text_to_send += '<pre>Cnt diff: {sign}{diff} ({sign}{perc_diff}%).</pre>\n'.format(
        n=total['members'],
        sign=sign,
        diff=members_diff,
        perc_diff=perc_diff
    )
    text_to_send += '<pre>Admins: {n}.</pre>\n'.format(n=total['admins'])
    text_to_send += '<pre>Errors during bypass: {n}.</pre>\n'.format(n=total['errors'])
    text_to_send += '\n<i>See you!</i>'
    r2t.send_text(text_to_send, parse_mode='HTML')
    # It's not a proper supply, so just stop.
    return SupplyResult.STOP_THIS_SUPPLY