def send_report_to_dev_chat(exc): link = None r2t = utils.Reddit2TelegramSender(config['telegram']['dev_chat'], config) frame = sys.exc_info()[2] frame = frame.tb_next while frame: local_vars = frame.tb_frame.f_locals if ('submodule_name' in local_vars) and ('submodule' in local_vars): submodule = local_vars['submodule_name'] channel = local_vars['submodule'].t_channel title = 'submodule: {}\nchannel: {}'.format(submodule, channel) if 'submission' in local_vars: link = local_vars['submission'].shortlink frame = frame.tb_next if link is not None: error_cnt = r2t.store_error_link(channel, link) title = '{title}\nlink: {link}\nerror_cnt: {cnt}'.format( title=title, link=link, cnt=error_cnt['cnt'] ) else: error_cnt = r2t.store_error_no_link(channel) title = '{title}\nerror_cnt: {cnt}'.format( title=title, cnt=error_cnt['cnt'] ) report = '<b>r2t error</b>\n{t}\n\n\n<pre>{e}</pre>'.format( t=title, e=exc ) r2t.send_text(report, parse_mode='HTML')
def supply(submodule_name, config): submodule = importlib.import_module( 'channels.{}.app'.format(submodule_name)) reddit = praw.Reddit(user_agent=config['reddit']['user_agent'], client_id=config['reddit']['client_id'], client_secret=config['reddit']['client_secret']) submissions = reddit.subreddit(submodule.subreddit).hot(limit=100) r2t = utils.Reddit2TelegramSender(submodule.t_channel, config) success = False for submission in submissions: link = submission.shortlink if r2t.was_before(link): continue success = submodule.send_post(submission, r2t) if success == utils.SupplyResult.SUCCESSFULLY: # Every thing is ok, post was sent r2t.mark_as_was_before(link) break elif success == utils.SupplyResult.DO_NOT_WANT_THIS_SUBMISSION: # Do not want to send this post r2t.mark_as_was_before(link) continue elif success == utils.SupplyResult.SKIP_FOR_NOW: # Do not want to send now continue elif success == utils.SupplyResult.STOP_THIS_SUPPLY: # If None — do not want to send anything this time break else: logging.error('Unknown SupplyResult. {}'.format(success)) if success is False: logging.info('Nothing to post from {sub} to {channel}.'.format( sub=submodule.subreddit, channel=submodule.t_channel))
def supply(subreddit, config): submodule = importlib.import_module('channels.{}.app'.format(subreddit)) reddit = praw.Reddit(user_agent=config['reddit']['user_agent'], client_id=config['reddit']['client_id'], client_secret=config['reddit']['client_secret']) submissions = reddit.subreddit(submodule.subreddit).hot(limit=100) bot = telepot.Bot(config['telegram_token']) store_stats(submodule.t_channel, bot, config) r2t = utils.Reddit2TelegramSender(submodule.t_channel, bot) success = False for submission in submissions: link = submission.shortlink if was_before(link, submodule.t_channel, config): continue success = submodule.send_post(submission, r2t) if success is True: # Every thing is ok, post was sent mark_as_was_before(link, submodule.t_channel, config) break elif success is False: # Do not want to send this post mark_as_was_before(link, submodule.t_channel, config) continue else: break if not success: logger.info('Nothing to post from {sub} to {channel}.'.format( sub=submodule.subreddit, channel=submodule.t_channel))
def supply(subreddit, config): submodule = importlib.import_module('channels.{}.app'.format(subreddit)) reddit = praw.Reddit(user_agent=config['reddit']['user_agent'], client_id=config['reddit']['client_id'], client_secret=config['reddit']['client_secret']) submissions = reddit.subreddit(submodule.subreddit).search( "flair_text:mv", sort='new', time_filter='day') r2t = utils.Reddit2TelegramSender(submodule.t_channel, config) success = False for submission in submissions: link = submission.shortlink if r2t.was_before(link): continue success = submodule.send_post(submission, r2t) if success is True: # Every thing is ok, post was sent r2t.mark_as_was_before(link) break elif success is False: # Do not want to send this post r2t.mark_as_was_before(link) continue else: # If None — do not want to send anything this time break if success is False: logger.info('Nothing to post from {sub} to {channel}.'.format( sub=submodule.subreddit, channel=submodule.t_channel))
def wrapper(*args, **kwargs): try: fn(*args, **kwargs) except Exception as e: r2t = utils.Reddit2TelegramSender(config['telegram_dev_chat'], config) r2t.send_text(str(e)) if client: # has sentry instance client.captureException() else: logging.exception("Exception Ignored.")
def supply(submodule_name, config, is_test=False): if not is_test: time.sleep(random.randrange(0, 40)) submodule = importlib.import_module( 'channels.{}.app'.format(submodule_name)) reddit = praw.Reddit(user_agent=config['reddit']['user_agent'], client_id=config['reddit']['client_id'], client_secret=config['reddit']['client_secret'], username=config['reddit']['username'], password=config['reddit']['password']) submissions_ranking = getattr(submodule, 'submissions_ranking', 'hot') submissions_limit = getattr(submodule, 'submissions_limit', 100) if submissions_ranking == 'top': submissions = reddit.subreddit( submodule.subreddit).top(limit=submissions_limit) elif submissions_ranking == 'hot': submissions = reddit.subreddit( submodule.subreddit).hot(limit=submissions_limit) elif submissions_ranking == 'new': submissions = reddit.subreddit( submodule.subreddit).new(limit=submissions_limit) else: logging.error( 'Unknown submissions_ranking. {}'.format(submissions_ranking)) channel_to_post = submodule.t_channel if not is_test else '@r_channels_test' r2t = utils.Reddit2TelegramSender(channel_to_post, config) success = False for submission in submissions: link = submission.shortlink if r2t.was_before(link): continue if r2t.too_much_errors(link): continue success = submodule.send_post(submission, r2t) if success == utils.SupplyResult.SUCCESSFULLY: # Every thing is ok, post was sent r2t.mark_as_was_before(link, sent=True) break elif success == utils.SupplyResult.DO_NOT_WANT_THIS_SUBMISSION: # Do not want to send this post r2t.mark_as_was_before(link, sent=False) continue elif success == utils.SupplyResult.SKIP_FOR_NOW: # Do not want to send now continue elif success == utils.SupplyResult.STOP_THIS_SUPPLY: # If None — do not want to send anything this time break else: logging.error('Unknown SupplyResult. {}'.format(success)) if success is False: logging.info('Nothing to post from {sub} to {channel}.'.format( sub=submodule.subreddit, channel=submodule.t_channel))
def send_report_to_dev_chat(exc): r2t = utils.Reddit2TelegramSender(config['telegram']['dev_chat'], config) local_vars = sys.exc_info()[2].tb_next.tb_frame.f_locals submodule = local_vars['submodule_name'] channel = local_vars['submodule'].t_channel title = 'submodule: {}\nchannel: {}'.format(submodule, channel) if 'submission' in local_vars: link = local_vars['submission'].shortlink error_cnt = r2t.store_error_link(channel, link) title = '{title}\nlink: {link}\nerror_cnt: {cnt}'.format( title=title, link=link, cnt=error_cnt['cnt']) report = '{t}\n\n\n{e}'.format(t=title, e=exc) r2t.send_text(report)
def send_report_to_dev_chat(exc): r2t = utils.Reddit2TelegramSender(config['telegram']['dev_chat'], config) local_vars = sys.exc_info()[2].tb_next.tb_frame.f_locals submodule = local_vars['submodule_name'] channel = local_vars['submodule'].t_channel title = 'submodule: {}\nchannel: {}'.format(submodule, channel) if 'submission' in local_vars: link = local_vars['submission'].shortlink title = '{}\nlink: {}'.format(title, link) report = '{}\n\n\n{}'.format( title, str(exc) ) r2t.send_text(report)
def send_to_channel_from_subreddit(how_to_post, channel_to_post, subreddit, submissions_ranking, submissions_limit, config, **kwargs): reddit = praw.Reddit(user_agent=config['reddit']['user_agent'], client_id=config['reddit']['client_id'], client_secret=config['reddit']['client_secret'], username=config['reddit']['username'], password=config['reddit']['password']) if submissions_ranking == 'top': submissions = reddit.subreddit(subreddit).top(limit=submissions_limit) elif submissions_ranking == 'hot': submissions = reddit.subreddit(subreddit).hot(limit=submissions_limit) elif submissions_ranking == 'new': submissions = reddit.subreddit(subreddit).new(limit=submissions_limit) else: logging.error( 'Unknown submissions_ranking. {}'.format(submissions_ranking)) r2t = utils.Reddit2TelegramSender(channel_to_post, config) success = False for submission in submissions: link = submission.shortlink if r2t.was_before(link): continue if r2t.too_much_errors(link): continue if kwargs.get('extra_args', False): success = how_to_post(submission, r2t, **kwargs) else: success = how_to_post(submission, r2t) if success == utils.SupplyResult.SUCCESSFULLY: # Every thing is ok, post was sent r2t.mark_as_was_before(link, sent=True) break elif success == utils.SupplyResult.DO_NOT_WANT_THIS_SUBMISSION: # Do not want to send this post r2t.mark_as_was_before(link, sent=False) continue elif success == utils.SupplyResult.SKIP_FOR_NOW: # Do not want to send now continue elif success == utils.SupplyResult.STOP_THIS_SUPPLY: # If None — do not want to send anything this time break else: logging.error('Unknown SupplyResult. {}'.format(success))
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 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 supply(submodule_name, config): submodule = importlib.import_module( 'channels.{}.app'.format(submodule_name)) reddit = praw.Reddit(user_agent=config['reddit']['user_agent'], client_id=config['reddit']['client_id'], client_secret=config['reddit']['client_secret'], username=config['reddit']['username'], password=config['reddit']['password']) submissions = reddit.subreddit(submodule.subreddit).hot(limit=100) comments = reddit.subreddit( submodule.subreddit).hot(limit=100).comments(limit=25) r2t = utils.Reddit2TelegramSender(submodule.t_channel, config) success = False #check if it has send_comment implemented if (hasattr(submodule, "send_comment")): for top_level_comment in comments: #according to the tutorial http://praw.readthedocs.io/en/latest/tutorials/comments.html if isinstance(top_level_comment, MoreComments): continue link = top_level_comment.permalink() if r2t.was_before(link): continue success = submodule.send_comment(top_level_comment, r2t) if success == utils.SupplyResult.SUCCESSFULLY: # Every thing is ok, comment was sent r2t.mark_as_was_before(link) break elif success == utils.SupplyResult.DO_NOT_WANT_THIS_SUBMISSION: # Do not want to send this comment r2t.mark_as_was_before(link) continue elif success == utils.SupplyResult.SKIP_FOR_NOW: # Do not want to send now continue elif success == utils.SupplyResult.STOP_THIS_SUPPLY: # If None — do not want to send anything this time break else: logging.error('Unknown SupplyResult. {}'.format(success)) if success is False: logging.info('Nothing to post from {sub} to {channel}.'.format( sub=submodule.subreddit, channel=submodule.t_channel)) success = False for submission in submissions: link = submission.shortlink if r2t.was_before(link): continue if r2t.too_much_errors(link): continue success = submodule.send_post(submission, r2t) if success == utils.SupplyResult.SUCCESSFULLY: # Every thing is ok, post was sent r2t.mark_as_was_before(link, sent=True) break elif success == utils.SupplyResult.DO_NOT_WANT_THIS_SUBMISSION: # Do not want to send this post r2t.mark_as_was_before(link, sent=False) continue elif success == utils.SupplyResult.SKIP_FOR_NOW: # Do not want to send now continue elif success == utils.SupplyResult.STOP_THIS_SUPPLY: # If None — do not want to send anything this time break else: logging.error('Unknown SupplyResult. {}'.format(success)) if success is False: logging.info('Nothing to post from {sub} to {channel}.'.format( sub=submodule.subreddit, channel=submodule.t_channel))