def test_submit_failure(self): message = 'Either `selftext` or `url` must be provided.' subreddit = Subreddit(self.reddit, display_name='name') with pytest.raises(TypeError) as excinfo: subreddit.submit('Cool title') assert str(excinfo.value) == message with pytest.raises(TypeError) as excinfo: subreddit.submit('Cool title', selftext='a', url='b') assert str(excinfo.value) == message
def test_submit_gallery__too_long_caption(self): message = "Caption must be 180 characters or less." subreddit = Subreddit(self.reddit, display_name="name") caption = "wayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy too long caption" with pytest.raises(TypeError) as excinfo: subreddit.submit_gallery("Cool title", images=[{ "image_path": __file__, "caption": caption }]) assert str(excinfo.value) == message
def test_submit_inline_media__invalid_path(self): message = "'invalid_image_path' is not a valid file path." subreddit = Subreddit(self.reddit, display_name="name") gif = InlineGif("invalid_image_path", "optional caption") image = InlineImage("invalid_image_path", "optional caption") video = InlineVideo("invalid_image_path", "optional caption") selftext = "Text with {gif1}, {image1}, and {video1} inline" media = {"gif1": gif, "image1": image, "video1": video} with pytest.raises(ValueError) as excinfo: subreddit.submit("title", selftext=selftext, inline_media=media) assert str(excinfo.value) == message
def test_equality(self): reason1 = RemovalReason(self.reddit, Subreddit(self.reddit, 'a'), '11qiw1rp2mlqd') reason2 = RemovalReason(self.reddit, Subreddit(self.reddit, 'a'), '11qiw1rp2mlqd') reason3 = RemovalReason(self.reddit, Subreddit(self.reddit, 'b'), '11qjnfc3wdghj') assert reason1 == reason1 assert reason1 == reason2 assert reason2 != reason3 assert reason3 == '11qjnfc3wdghj' assert reason2 != '11qjnfc3wdghj'
def test_submit_gallery__missing_path(self): message = "'image_path' is required." subreddit = Subreddit(self.reddit, display_name="name") with pytest.raises(TypeError) as excinfo: subreddit.submit_gallery("Cool title", images=[{ "caption": "caption" }, { "caption": "caption2" }]) assert str(excinfo.value) == message
def test_equality(self): subreddit1 = Subreddit(self.reddit, _data={"display_name": "dummy1", "n": 1}) subreddit2 = Subreddit(self.reddit, _data={"display_name": "Dummy1", "n": 2}) subreddit3 = Subreddit(self.reddit, _data={"display_name": "dummy3", "n": 2}) assert subreddit1 == subreddit1 assert subreddit2 == subreddit2 assert subreddit3 == subreddit3 assert subreddit1 == subreddit2 assert subreddit2 != subreddit3 assert subreddit1 != subreddit3 assert "dummy1" == subreddit1 assert subreddit2 == "dummy1"
def test_construct_failure(self): message = "Either `display_name` or `_data` must be provided." with pytest.raises(TypeError) as excinfo: Subreddit(self.reddit) assert str(excinfo.value) == message with pytest.raises(TypeError) as excinfo: Subreddit(self.reddit, "dummy", {"id": "dummy"}) assert str(excinfo.value) == message with pytest.raises(ValueError): Subreddit(self.reddit, "")
def test_hash(self): subreddit1 = Subreddit(self.reddit, _data={'display_name': 'dummy1', 'n': 1}) subreddit2 = Subreddit(self.reddit, _data={'display_name': 'Dummy1', 'n': 2}) subreddit3 = Subreddit(self.reddit, _data={'display_name': 'dummy3', 'n': 2}) assert hash(subreddit1) == hash(subreddit1) assert hash(subreddit2) == hash(subreddit2) assert hash(subreddit3) == hash(subreddit3) assert hash(subreddit1) == hash(subreddit2) assert hash(subreddit2) != hash(subreddit3) assert hash(subreddit1) != hash(subreddit3)
def test_equality(self): subreddit1 = Subreddit(self.reddit, _data={'display_name': 'dummy1', 'n': 1}) subreddit2 = Subreddit(self.reddit, _data={'display_name': 'Dummy1', 'n': 2}) subreddit3 = Subreddit(self.reddit, _data={'display_name': 'dummy3', 'n': 2}) assert subreddit1 == subreddit1 assert subreddit2 == subreddit2 assert subreddit3 == subreddit3 assert subreddit1 == subreddit2 assert subreddit2 != subreddit3 assert subreddit1 != subreddit3 assert 'dummy1' == subreddit1 assert subreddit2 == 'dummy1'
def test_fullname(self): subreddit = Subreddit(self.reddit, _data={ 'display_name': 'name', 'id': 'dummy' }) assert subreddit.fullname == 't5_dummy'
def test_str(self): subreddit = Subreddit(self.reddit, _data={ 'display_name': 'name', 'id': 'dummy' }) assert str(subreddit) == 'name'
def test_pickle(self): page = WikiPage(self.reddit, subreddit=Subreddit(self.reddit, "a"), name="x") for level in range(pickle.HIGHEST_PROTOCOL + 1): other = pickle.loads(pickle.dumps(page, protocol=level)) assert page == other
def test_fullname(self): subreddit = Subreddit(self.reddit, _data={ "display_name": "name", "id": "dummy" }) assert subreddit.fullname == "t5_dummy"
def test_hash(self): subreddit1 = Subreddit( self.reddit, _data={"display_name": "dummy1", "n": 1} ) subreddit2 = Subreddit( self.reddit, _data={"display_name": "Dummy1", "n": 2} ) subreddit3 = Subreddit( self.reddit, _data={"display_name": "dummy3", "n": 2} ) assert hash(subreddit1) == hash(subreddit1) assert hash(subreddit2) == hash(subreddit2) assert hash(subreddit3) == hash(subreddit3) assert hash(subreddit1) == hash(subreddit2) assert hash(subreddit2) != hash(subreddit3) assert hash(subreddit1) != hash(subreddit3)
def _notify_successful_load(self, subreddit: Subreddit) -> bool: log.info('Sending notification for successful config update to %s', subreddit.display_name) if self.notification_svc: self.notification_svc.send_notification( f'New config loaded for r/{subreddit.display_name}', subject=f'Subreddit Config Load Success') try: subreddit.message( 'Repost Sleuth Has Loaded Your New Config!', 'I saw your config changes and have loaded them! \n\n I\'ll start using them now.' ) return True except Exception as e: log.exception('Failed to send PM to %s', subreddit.display_name) return False
def dump(sub: Subreddit, outfile: TextIO): import csv writer = csv.DictWriter(outfile, ['username', 'flair_text', 'flair_css_class'], extrasaction='ignore') writer.writeheader() for user in sub.flair(): user['username'] = user['user'].name writer.writerow(user)
def test_pickle(self): emoji = Emoji(self.reddit, subreddit=Subreddit(self.reddit, "a"), name="x") for level in range(pickle.HIGHEST_PROTOCOL + 1): other = pickle.loads(pickle.dumps(emoji, protocol=level)) assert emoji == other
def _notify_failed_load(self, subreddit: Subreddit, error: Text, revision_id: Text) -> bool: if self.notification_svc: self.notification_svc.send_notification( f'Failed to load config for r/{subreddit.display_name}\n{error}', subject=f'Subreddit Config Load Failed') body = f'I\'m unable to load your new config for r/{subreddit.display_name}. Your recent changes are invalid. \n\n' \ f'Error: {error} \n\n' \ 'Please validate your changes and try again' try: subreddit.message('Repost Sleuth Failed To Load Config', body) return True except Exception as e: log.exception('Failed to send PM to %s', subreddit.display_name) return False
def test_pickle(self): reason = RemovalReason(self.reddit, subreddit=Subreddit(self.reddit, "a"), reason_id="x") for level in range(pickle.HIGHEST_PROTOCOL + 1): other = pickle.loads(pickle.dumps(reason, protocol=level)) assert reason == other
def test_update(self): emoji = Emoji(self.reddit, subreddit=Subreddit(self.reddit, "a"), name="x") with pytest.raises(TypeError) as excinfo: emoji.update() assert str(excinfo.value) == "At least one attribute must be provided"
def test_str(self): subreddit = Subreddit(self.reddit, _data={ "display_name": "name", "id": "dummy" }) assert str(subreddit) == "name"
def updatestats(sub: Subreddit, flair_config: Dict, dry_run: bool) -> None: stats = {} for f in sub.flair(limit=None): classes = f['flair_css_class'].lower().strip() if f['flair_css_class'] else None if not classes: continue primary = find_flair(classes, flair_config['primary']) secondary = find_flair(classes, flair_config['secondary']) if primary is None: continue if primary not in stats: stats[primary] = {'_total': 0} stats[primary]['_total'] += 1 if secondary is not None: if secondary not in stats[primary]: stats[primary][secondary] = 0 stats[primary][secondary] += 1 renderer = RenderHelper(sub.display_name) rdr = renderer.render('flair_stats.md', {'stats': stats}) page = sub.wiki['/flair/stats'] update_or_create(sub, page, rdr, dry_run, edit_reason="Updated flair stats")
def test_pickle(self): subreddit = Subreddit( self.reddit, _data={"display_name": "name", "id": "dummy"} ) for level in range(pickle.HIGHEST_PROTOCOL + 1): other = pickle.loads(pickle.dumps(subreddit, protocol=level)) assert subreddit == other
def get_parent_post_id(post: Comment, subreddit: Subreddit) -> Submission: """ Takes any given comment object and returns the object of the original post, no matter how far up the chain it is. This is a very time-intensive function because of how Reddit handles rate limiting and the fact that you can't just request the top parent -- you have to just loop your way to the top. :param post: comment object :param r: the instantiated reddit object :return: submission object of the top post. """ if not post.is_root: parent = subreddit.comment(id=clean_id(post.parent_id)) return get_parent_post_id(parent, subreddit) else: return subreddit.submission(id=clean_id(post.parent_id))
def make_post(reddit: Reddit, subreddit: Subreddit, invited, removed): date = datetime.date.today() date_str = date.strftime("%Y-%m-%d") title = date_str + ' - Bot Recap' selftext = '' if config.remove_users: if not len(removed) == 0: selftext = selftext + 'Kicked users: \n' for user in removed: selftext = selftext + '* u/' + user + '\n' if config.invite_users: selftext = selftext + '\nAdded users: \n' for user in invited: selftext = selftext + '* u/' + user + '\n' reddit.validate_on_submit = True subreddit.submit(title, selftext=selftext) print('Post made\n')
def _notify_config_created(self, subreddit: Subreddit) -> bool: """ Send a private message to a sub's mod mail letting them know their config has been created :rtype: bool :param subreddit: Subreddit to notify :return: bool for successful or failed message """ log.info('Sending config created notification to %s', subreddit.display_name) try: subreddit.message( 'Repost Sleuth Has Loaded Your New Config!', 'I saw your config changes and have loaded them! \n\n I\'ll start using them now.' ) return True except Exception as e: log.exception('Failed to send config created notification') return False
def test_pickle(self): subreddit = Subreddit(self.reddit, _data={ 'display_name': 'name', 'id': 'dummy' }) for level in range(pickle.HIGHEST_PROTOCOL + 1): other = pickle.loads(pickle.dumps(subreddit, protocol=level)) assert subreddit == other
def test_set__redundant_arguments(self): message = '`redditor` must be provided.' subreddit = Subreddit(self.reddit, display_name='name') with pytest.raises(TypeError) as excinfo: subreddit.flair.set() assert str(excinfo.value) == message with pytest.raises(TypeError) as excinfo: subreddit.flair.set('bboe', thing='bboe') assert str(excinfo.value) == message
def _notify_new_options(self, subreddit: Subreddit, config_keys: List[Text]) -> bool: log.info( 'Sending notification for new config keys being added to %s. %s', config_keys, subreddit.display_name) if self.notification_svc: self.notification_svc.send_notification( f'Added now config keys to r/{subreddit.display_name}\n{config_keys}', subject='New Config Options Notification Sent') try: subreddit.message( 'New Repost Sleuth Options Available!', 'Your Repost Sleuth config was missing some newly available options.\n\n ' f'I\'ve added the following options to your config: {config_keys}\n\n' 'You can read more about them here: https://www.reddit.com/r/RepostSleuthBot/wiki/add-you-sub/configure-repost-sleuth#wiki_config_value_explanation' ) return True except Exception as e: log.exception('Failed to send PM to %s', subreddit.display_name) return False
def test_hash(self): emoji1 = Emoji(self.reddit, subreddit=Subreddit(self.reddit, "a"), name="x") emoji2 = Emoji(self.reddit, subreddit=Subreddit(self.reddit, "a"), name="2") emoji3 = Emoji(self.reddit, subreddit=Subreddit(self.reddit, "b"), name="1") emoji4 = Emoji(self.reddit, subreddit=Subreddit(self.reddit, "A"), name="x") emoji5 = Emoji(self.reddit, subreddit=Subreddit(self.reddit, "a"), name="X") emoji6 = Emoji(self.reddit, subreddit=Subreddit(self.reddit, "b"), name="x") assert hash(emoji1) == hash(emoji1) assert hash(emoji2) == hash(emoji2) assert hash(emoji3) == hash(emoji3) assert hash(emoji1) != hash(emoji2) assert hash(emoji1) != hash(emoji3) assert hash(emoji1) == hash(emoji4) assert hash(emoji1) != hash(emoji5) assert hash(emoji1) != hash(emoji6)
def test_equality(self): emoji1 = Emoji(self.reddit, subreddit=Subreddit(self.reddit, "a"), name="x") emoji2 = Emoji(self.reddit, subreddit=Subreddit(self.reddit, "a"), name="2") emoji3 = Emoji(self.reddit, subreddit=Subreddit(self.reddit, "b"), name="1") emoji4 = Emoji(self.reddit, subreddit=Subreddit(self.reddit, "A"), name="x") emoji5 = Emoji(self.reddit, subreddit=Subreddit(self.reddit, "a"), name="X") emoji6 = Emoji(self.reddit, subreddit=Subreddit(self.reddit, "b"), name="x") assert emoji1 == emoji1 assert emoji1 == "x" assert emoji2 == emoji2 assert emoji3 == emoji3 assert emoji1 != emoji2 assert emoji1 != emoji3 assert emoji1 == emoji4 assert emoji1 != emoji5 assert emoji1 != emoji6 assert emoji1 != 5
def test_submit_failure(self): message = "Either `selftext` or `url` must be provided." subreddit = Subreddit(self.reddit, display_name="name") with pytest.raises(TypeError) as excinfo: subreddit.submit("Cool title") assert str(excinfo.value) == message with pytest.raises(TypeError) as excinfo: subreddit.submit("Cool title", selftext="a", url="b") assert str(excinfo.value) == message with pytest.raises(TypeError) as excinfo: subreddit.submit("Cool title", selftext="", url="b") assert str(excinfo.value) == message
def test_search__params_not_modified(self): params = {'dummy': 'value'} subreddit = Subreddit(self.reddit, display_name='name') generator = subreddit.search(None, params=params) assert generator.params['dummy'] == 'value' assert params == {'dummy': 'value'}
def test_search__params_not_modified(self): params = {"dummy": "value"} subreddit = Subreddit(self.reddit, display_name="name") generator = subreddit.search(None, params=params) assert generator.params["dummy"] == "value" assert params == {"dummy": "value"}