How to use the prawcore.exceptions.Forbidden function in prawcore

To help you get started, we’ve selected a few prawcore examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Watchful1 / UpdateMeBot / scripts / migrate_subreddits.py View on Github external
log.info(f"Starting subreddits")

for row in c.execute('''
	SELECT Subreddit, Status, DefaultSubscribe, NextNotice, AlwaysPM, Filter, PostsPerDay, LastProfiled
	FROM subredditWhitelist
'''):
	subreddit_name = row[0]
	if subreddit_name in blacklist:
		continue
	reddit_subreddit = r.subreddit(subreddit_name)
	try:
		reddit_subreddit._fetch()
	except (prawcore.exceptions.Redirect, prawcore.exceptions.NotFound):
		log.info(f"Subreddit r/{subreddit_name} doesn't exist")
		continue
	except prawcore.exceptions.Forbidden:
		log.info(f"Subreddit r/{subreddit_name} forbidden")
		continue
	subreddit = new_db.get_or_add_subreddit(reddit_subreddit.display_name)
	subreddit.is_enabled = row[1] == 1
	if subreddit.is_enabled:
		subreddit.last_scanned = utils.datetime_now()
		subreddit.date_enabled = utils.datetime_now()
	subreddit.default_recurring = row[2]
	subreddit.no_comment = row[4]
	if subreddit_name == 'hfy':
		subreddit.flair_blacklist = 'meta,wp,video,misc,text'
		subreddit.prompt_type = SubredditPromptType.ALL
	elif subreddit_name == 'written4reddit':
		subreddit.prompt_type = SubredditPromptType.ALLOWED
		subreddit.prompt_users.append(new_db.get_or_add_user("written4reddit"))
	elif subreddit_name == 'luna_lovewell':
github mcandocia / TreeGrabForReddit / subreddit_scrape.py View on Github external
def validate_subreddit(entry, opts, scraper):
    try:
        if entry['related_subreddit'] in opts.RELATED_SUBREDDIT_SET:
            return None
        subreddit = scraper.subreddit(entry['related_subreddit'])
        #need to force update of display_name
        num_subscribers = subreddit.subscribers
        display_name = subreddit.display_name
        return subreddit
    except Forbidden:
        #at this time display_name is not updated, so just ignore it
        return None
    except (NotFound, Redirect):
        return None
    except BadRequest:
        return None
github praw-dev / prawcore / prawcore / util.py View on Github external
"""Provide utility for the prawcore package."""
from .exceptions import Forbidden, InsufficientScope, InvalidToken


_auth_error_mapping = {
    403: Forbidden,
    "insufficient_scope": InsufficientScope,
    "invalid_token": InvalidToken,
}


def authorization_error_class(response):
    """Return an exception instance that maps to the OAuth Error.

    :param response: The HTTP response containing a www-authenticate error.

    """
    message = response.headers.get("www-authenticate")
    if message:
        error = message.replace('"', "").rsplit("=", 1)[1]
    else:
        error = response.status_code
github GrafeasGroup / tor / tor / archiver.py View on Github external
configure_logging(config, log_name='archiver.log')

    tor = configure_tor(r, config)
    initialize(tor, config)
    logging.info('Initialization complete.')
    archive = r.subreddit('ToR_Archive')

    try:
        while True:
            try:
                run(tor, config, archive)
                time.sleep(300)  # 5 minutes
            except (
                    prawcore.exceptions.RequestException,
                    prawcore.exceptions.ServerError,
                    prawcore.exceptions.Forbidden
            ) as e:
                logging.warning(
                    '{} - Issue communicating with Reddit. Sleeping for 60s!'
                    ''.format(e)
                )
                time.sleep(60)

    except KeyboardInterrupt:
        logging.info('Received keyboard interrupt! Shutting down!')
        sys.exit(0)

    except Exception as e:
        explode_gracefully('ToR_archivist', e, tor)
github vihanggodbole / Search-for-reddit-saved-posts / search_saved_posts.py View on Github external
def login():
    '''logs in the user using OAuth 2.0 and returns a redditor object for use'''
    username = input('Username: ')
    user_agent = 'reddit_saved_posts_search: v1.0 (for /u/{})'.format(
        username)
    r = praw.Reddit('mysettings', user_agent=user_agent)
    try:
        return r.user.me()
    except prawcore.exceptions.Forbidden:
        print('\nIt seems your credentials are invalid. Please check whether your praw.ini file is properly setup.')
        return None
github mcandocia / TreeGrabForReddit / praw_object_data.py View on Github external
'comments_platinum': comment_gildings.get('gid_3', 0),
                }
                
            except Exception as e:
                print(e)
                gilding_data = {'gilded_visible': False}

            data.update(gilding_data)
        #get comment history
        comment_history = user.comments.new(limit=opts.user_comment_limit)
        comments = {}
        try:
            for i, comment in enumerate(comment_history):
                comments.update(get_comment_data(comment, opts, mode='minimal',
                                                 author_id = data['id']))
        except Forbidden:
            print(user.name)
            print('forbidden comment history for some reason')

            

        #get submission history
        post_history = user.submissions.new(limit=opts.user_thread_limit)
        threads = {}

        
        try:
            for i, post in enumerate(post_history):
                threads.update(get_thread_data(post, opts, mode='minimal'))
        except Forbidden:
            print(user.name)
            print('forbidden post history for some reason')
github trambelus / UserSim / usim.py View on Github external
started.append(com.name)
				try:
					if com.subreddit == None:
						continue
					if com.subreddit.display_name in banned:
						log("%s: (%d) Ignored request from banned subreddit %s" % (com.name,index+1,com.subreddit.display_name))
						continue
				except prawcore.exceptions.Forbidden:
					continue
				res = re.search(req_pat, com.body.lower())
				if res == None:
					continue # We were mentioned but it's not a proper request, move on
				try:
					if USER.lower() in [rep.author.name.lower() for rep in com.replies.list() if rep.author != None]:
						continue # We've already hit this one, move on
				except prawcore.exceptions.Forbidden:
					continue

				warnings.simplefilter("ignore")
				try:
					log("%s: processing" % (com.name), console_only=True)
					#mp.Process(target=process, args=(q, com, res.group(0), index+1)).start()
					process(q, com, res.group(0), index+1, r)
				except Exception as ex:
					log("%s: exception: %s" % (com.name, ex))
					continue
				while q.qsize() > 0:
					item = q.get()
					if item == 'clear':
						log("Clearing list of started tasks")
						started = []
					elif item == 'quit':
github SreenivasVRao / RiskyClickerBot / RedditBot.py View on Github external
elif new_comment.is_root:
            submission = self.bot.submission(new_parent)
            botreply = self.parse_comment(submission, root=True)

        try:
            if botreply is not None:
                if not test:
                    new_comment.reply(botreply)
                    id = new_parent.id
                    print ('I made a new comment: reddit.com'+ new_comment.permalink)
                else:
                    print (botreply)

        except APIException as a:
            print(a.message, a.error_type)
        except prawcore.exceptions.Forbidden as e:
            print (e.message) # If bot is banned from the sub.

        return id
github aquelemiguel / vreddit-mirror-bot / vreddit_mirror_bot.py View on Github external
reply = f"""Issues with **v.redd.it**? Try this **Streamable** mirror!{s}^^[Why?](https://github.com/aquelemiguel/vreddit-mirror-bot/wiki/FAQ){s}{s}\n
* [**MP4** ({mp4_size} MB)]({mp4_url})  \n\n***
^^vredditmirrorbot{s}|{s}[Creator](https://github.com/aquelemiguel){s}|{s}[Keep{s}this{s}bot{s}alive{s}♥️](https://github.com/aquelemiguel/vreddit-mirror-bot/wiki/Donations)
"""

    while True:
        try:
            root.reply(reply)
            print("Upload complete!\n")
            log_url(submission.url, 4)
            break
        except praw.exceptions.APIException as e: # Hit Reddit's submission limit.
            print("Hit rate limit: " + e.message)
            time.sleep(30)
        except prawcore.exceptions.Forbidden as e: # Probably got banned while converting the video.
            print("Wasn't able to comment on: " + submission.url)
            break
    return