Exemple #1
0
def update_promotion_order():
    config = get_config()
    db = pymongo.MongoClient(host=config['db']['host'])[config['db']['name']]
    settings = db['settings']
    first_date_view = db['view_with_first_dates']
    all_submodules = get_all_public_submodules()
    all_submodules.remove('reddit2telegram')
    submodules_and_dates = dict()
    for submodule in all_submodules:
        imported = importlib.import_module('channels.{}.app'.format(submodule))
        channel = imported.t_channel
        first_date_result = first_date_view.find_one({'_id': channel.lower()})
        if first_date_result is None:
            continue
        submodules_and_dates[submodule] = first_date_result['first_date']
    if settings.find_one({'setting': SETTING_NAME}) is None:
        settings.insert_one({
            'setting': SETTING_NAME,
            'promotion_order': submodules_and_dates,
            'already_promoted': list(),
            'counter': 0
        })
    settings.find_one_and_update(
        {'setting': SETTING_NAME},
        {'$set': {
            'promotion_order': submodules_and_dates
        }})
Exemple #2
0
def what_submodule():
    now = datetime.now()
    if now.weekday() == 6:  # if Sunday then random
        all_submodules = get_all_public_submodules()
        all_submodules.remove('reddit2telegram')
        return random.choice(all_submodules)

    config = get_config()
    db = pymongo.MongoClient(host=config['db']['host'])[config['db']['name']]
    settings = db['settings']
    if settings.find_one({'setting': SETTING_NAME}) is None:
        update_promotion_order()
    setting_result = settings.find_one({'setting': SETTING_NAME})

    already_promoted = setting_result['already_promoted']
    promotion_order = setting_result['promotion_order']

    for submodule in sorted(promotion_order.keys(),
                            key=promotion_order.get,
                            reverse=1):
        if submodule not in already_promoted:
            return submodule

    # If every is promoted.
    settings.find_one_and_update({'setting': SETTING_NAME}, {
        '$inc': {
            'counter': 1
        },
        '$set': {
            'already_promoted': list()
        }
    })
Exemple #3
0
def send_post(submission, r2t):
    channels_list = get_all_public_channels(r2t)

    for submodule_name in get_all_public_submodules():
        submodule = importlib.import_module(
            'channels.{}.app'.format(submodule_name))
        channel = submodule.t_channel
        bd_party, years = is_birthday_today(r2t, channel)
        if bd_party and years > 0:
            plural = 's' if years != 1 else ''
            # To the @r_channels
            long_sleep()
            r2t.t_channel = '@r_channels'
            cakes = '🎂' * years
            text_to_send = '{cake}\n🎁 Today {channel} is {years_cnt} year{s} old.\n🎉 Congratulations! 🎈'.format(
                channel=channel, years_cnt=years, s=plural, cake=cakes)
            r2t.send_text(text_to_send)
            # To the dev channel
            long_sleep()
            r2t.t_channel = get_dev_channel()
            r2t.send_text(text_to_send)
            # To the channels itself
            long_sleep()
            r2t.t_channel = channel
            text1_to_send = text_to_send
            list_of_channels = generate_list_of_channels(
                channels_list, random_permutation=True)
            text3_to_send = default_ending()
            r2t.send_text(text1_to_send)
            short_sleep()
            text2_to_send = 'Other @reddit2telegram channels powered by @r_channels:\n'
            for l in chunker(list_of_channels, 100):
                text2_to_send += '\n'.join(l)
                r2t.send_text(text2_to_send)
                text2_to_send = ''
                short_sleep()
            r2t.send_text(text3_to_send)
            long_sleep()
            if channel != '@reddit2telegram':
                config = get_config()
                send_to_channel_from_subreddit(
                    how_to_post=make_nice_submission,
                    channel_to_post='@reddit2telegram',
                    subreddit=submodule.subreddit,
                    submodule_name_to_promte=submodule_name,
                    submissions_ranking='top',
                    submissions_limit=1000,
                    config=config,
                    extra_args=True,
                    extra_ending=text_to_send)
                long_sleep()
    # It's not a proper supply, so just stop.
    return SupplyResult.STOP_THIS_SUPPLY