def send_text(self, text, disable_web_page_preview=False, parse_mode=None): if len(text) < 4096: self.telegram_bot.send_message( chat_id=self.t_channel, text=text, disable_web_page_preview=disable_web_page_preview, parse_mode=parse_mode) return SupplyResult.SUCCESSFULLY # If text is longer than 4096 symbols. next_text = text while len(next_text) > 0: list_of_words = next_text.split(' ') if len(list_of_words[0]) > 4096: new_text, next_text = self._split_4096(next_text) self.telegram_bot.send_message( chat_id=self.t_channel, text=new_text, disable_web_page_preview=disable_web_page_preview, parse_mode=parse_mode) elif len(list_of_words[0]) <= 4096: # If first word is less than 4096. words_to_send = list() while (len(list_of_words) > 0) and ( sum([len(x) for x in words_to_send]) + len(words_to_send) + len(list_of_words[0]) <= 4096): words_to_send.append(list_of_words.pop(0)) self.telegram_bot.send_message( chat_id=self.t_channel, text=' '.join(words_to_send), disable_web_page_preview=disable_web_page_preview, parse_mode=parse_mode) next_text = ' '.join(list_of_words) short_sleep() return SupplyResult.SUCCESSFULLY
def main(config_filename, sub, is_test=False): with open(config_filename) as config_file: config = yaml.safe_load(config_file.read()) if not is_test: supply(sub, config, is_test) else: for i in range(100): print('i =', i, '>') supply(sub, config, is_test) short_sleep(0.1)
def __init__(self, t_channel=None, config=None): super(Reddit2TelegramSender, self).__init__() if config is None: with open('configs/prod.yml') as f: config = yaml.safe_load(f.read()) self.config = config self.telegram_bot = telegram.Bot(self.config['telegram']['token']) if t_channel is None: t_channel = '@r_channels_test' self.t_channel = t_channel self._make_mongo_connections() short_sleep()
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
def send_post(submission, r2t): config_filename = 'configs/prod.yml' with open(config_filename) as config_file: config = yaml.safe_load(config_file.read()) settings = pymongo.MongoClient( host=config['db']['host'])[config['db']['name']]['settings'] settings.ensure_index([('setting', pymongo.ASCENDING)]) last_update_doc = settings.find_one({ 'setting': SETTING_NAME, }) if last_update_doc is None: last_update_doc = {'last_update': 0} settings.insert_one({'setting': SETTING_NAME, 'last_update': 0}) updates = r2t.telegram_bot.get_updates( offset=last_update_doc['last_update']) last_update = 0 for update in updates: update = update.to_dict() short_sleep() last_update = update['update_id'] if 'message' not in update: continue if 'chat' not in update['message']: continue if 'text' not in update['message']: continue user_id = update['message']['chat']['id'] if not isinstance(user_id, int) or user_id < 0: continue message_id = update['message']['message_id'] r2t.telegram_bot.forward_message(chat_id=get_dev_channel(), from_chat_id=user_id, message_id=message_id) settings.find_one_and_update({'setting': SETTING_NAME}, {'$set': { 'last_update': last_update }}) # It's not a proper supply, so just stop. return SupplyResult.STOP_THIS_SUPPLY
def send_gif(self, url, ext, text, parse_mode=None): filename = self._get_file_name(ext) # Download gif if not download_file(url, filename): return SupplyResult.DO_NOT_WANT_THIS_SUBMISSION # Telegram will not autoplay big gifs if os.path.getsize(filename) > TELEGRAM_AUTOPLAY_LIMIT: return SupplyResult.DO_NOT_WANT_THIS_SUBMISSION next_text = '' if len(text) > TELEGRAM_CAPTION_LIMIT: text, next_text = self._split_1024(text) f = open(filename, 'rb') self.telegram_bot.send_document(chat_id=self.t_channel, document=f, caption=text, parse_mode=parse_mode) f.close() if len(next_text) > 1: short_sleep() self.send_text(next_text, disable_web_page_preview=True, parse_mode=parse_mode) return SupplyResult.SUCCESSFULLY
def send_video(self, url, text, parse_mode=None): filename = self._get_file_name('.mp4') # Download video if not download_file(url, filename): return SupplyResult.DO_NOT_WANT_THIS_SUBMISSION try: # Download audio audio_playlist_url = url[:url.rfind('/')] + '/HLS_AUDIO_160_K.m3u8' audio_playlist = m3u8.load(audio_playlist_url) segment_0 = audio_playlist.segments[0] audio_url = segment_0.absolute_uri audio_filename = filename + '.aac' if not download_file(audio_url, audio_filename): return SupplyResult.DO_NOT_WANT_THIS_SUBMISSION except: # Looks like no audio return self.send_gif(url, '.mp4', text, parse_mode=parse_mode) # Combine it audio and video video_with_audio_filename = self._get_file_name('.1.mp4') cmd = 'ffmpeg -i %s -i %s -c:v copy -c:a aac -strict experimental %s -hide_banner -loglevel panic -y' % ( filename, audio_filename, video_with_audio_filename) subprocess.call(cmd, shell=True) # Telegram will not autoplay big gifs if os.path.getsize(video_with_audio_filename) > TELEGRAM_VIDEO_LIMIT: return SupplyResult.DO_NOT_WANT_THIS_SUBMISSION next_text = '' if len(text) > TELEGRAM_CAPTION_LIMIT: text, next_text = self._split_1024(text) f = open(video_with_audio_filename, 'rb') self.telegram_bot.send_video(chat_id=self.t_channel, video=f, caption=text, parse_mode=parse_mode) f.close() if len(next_text) > 1: short_sleep() self.send_text(next_text, disable_web_page_preview=True, parse_mode=parse_mode) return SupplyResult.SUCCESSFULLY
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 send_album(self, story): def just_send(num, item): text = '# {}\n{}'.format(num, item['url']) self.send_text(text) for num, item in sorted(story.items(), key=lambda x: x[0]): text = '# {}'.format(num) if item['what'] == TYPE_GIF: if not self.send_gif(item['url'], item['ext'], text): just_send(num, item) elif item['what'] == TYPE_IMG: if not self.send_img(item['url'], item['ext'], text): just_send(num, item) elif item['what'] == 'text': just_send(num, item) if num >= ALBUM_LIMIT: self.send_text('...') return SupplyResult.SUCCESSFULLY short_sleep() return SupplyResult.SUCCESSFULLY
def say_congrats(submodule_name, channel, achievement): short_sleep() config = get_config() submodule = utils.channels_stuff.import_submodule(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)) short_sleep() bot = utils.Reddit2TelegramSender(submodule.t_channel) bot.forward_last_message_from_the_channel('@reddit2telegram') long_sleep()
def send_gif(self, url, text, parse_mode=None): # Telegram will not autoplay big gifs if get_url_size(url) > TELEGRAM_AUTOPLAY_LIMIT: print('Too big.') return SupplyResult.DO_NOT_WANT_THIS_SUBMISSION next_text = '' if len(text) > TELEGRAM_CAPTION_LIMIT: text, next_text = self._split_1024(text) try: self.telegram_bot.send_document(chat_id=self.t_channel, document=url, caption=text, parse_mode=parse_mode) except BadRequest as e: logging.info('Unknown error.') return SupplyResult.SKIP_FOR_NOW if len(next_text) > 1: short_sleep() self.send_text(next_text, disable_web_page_preview=True, parse_mode=parse_mode) return SupplyResult.SUCCESSFULLY
def say_congrats(submodule_name, channel, archivement): short_sleep() config = get_config() r2t_main_chat = utils.Reddit2TelegramSender('@r_channels', config) r2t_main_chat.send_text( 'π Great achivement!\nπͺ {channel} just passed the milestone of {number} subscribers.' .format(channel=channel, number=archivement)) short_sleep() 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 achivement!\nπͺ Milestone of {number} subscribers.'.format( number=archivement)) long_sleep()
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
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