def getPosts(programMode): """Call PRAW regarding to arguments and pass it to extractDetails. Return what extractDetails has returned. """ reddit = Reddit(GLOBAL.config["credentials"]["reddit"]).begin() if programMode["sort"] == "best": raise NoPrawSupport("PRAW does not support that") if "subreddit" in programMode: if "search" in programMode: if programMode["subreddit"] == "frontpage": programMode["subreddit"] = "all" if "user" in programMode: if programMode["user"] == "me": programMode["user"] = str(reddit.user.me()) if not "search" in programMode: if programMode["sort"] == "top" or programMode[ "sort"] == "controversial": keyword_params = { "time_filter": programMode["time"], "limit": programMode["limit"] } # OTHER SORT TYPES DON'T TAKE TIME_FILTER else: keyword_params = {"limit": programMode["limit"]} else: keyword_params = { "time_filter": programMode["time"], "limit": programMode["limit"] } if "search" in programMode: if programMode["sort"] in ["hot", "rising", "controversial"]: raise InvalidSortingType("Invalid sorting type has given") if "subreddit" in programMode: print ( "search for \"{search}\" in\n" \ "subreddit: {subreddit}\nsort: {sort}\n" \ "time: {time}\nlimit: {limit}\n".format( search=programMode["search"], limit=programMode["limit"], sort=programMode["sort"], subreddit=programMode["subreddit"], time=programMode["time"] ).upper(),noPrint=True ) return extractDetails( reddit.subreddit(programMode["subreddit"]).search( programMode["search"], limit=programMode["limit"], sort=programMode["sort"], time_filter=programMode["time"])) elif "multireddit" in programMode: raise NoPrawSupport("PRAW does not support that") elif "user" in programMode: raise NoPrawSupport("PRAW does not support that") elif "saved" in programMode: raise ("Reddit does not support that") if programMode["sort"] == "relevance": raise InvalidSortingType("Invalid sorting type has given") if "saved" in programMode: print("saved posts\nuser:{username}\nlimit={limit}\n".format( username=reddit.user.me(), limit=programMode["limit"]).upper(), noPrint=True) return extractDetails( reddit.user.me().saved(limit=programMode["limit"])) if "subreddit" in programMode: if programMode["subreddit"] == "frontpage": print ( "subreddit: {subreddit}\nsort: {sort}\n" \ "time: {time}\nlimit: {limit}\n".format( limit=programMode["limit"], sort=programMode["sort"], subreddit=programMode["subreddit"], time=programMode["time"] ).upper(),noPrint=True ) return extractDetails( getattr(reddit.front, programMode["sort"])(**keyword_params)) else: print ( "subreddit: {subreddit}\nsort: {sort}\n" \ "time: {time}\nlimit: {limit}\n".format( limit=programMode["limit"], sort=programMode["sort"], subreddit=programMode["subreddit"], time=programMode["time"] ).upper(),noPrint=True ) return extractDetails( getattr(reddit.subreddit(programMode["subreddit"]), programMode["sort"])(**keyword_params)) elif "multireddit" in programMode: print ( "user: {user}\n" \ "multireddit: {multireddit}\nsort: {sort}\n" \ "time: {time}\nlimit: {limit}\n".format( user=programMode["user"], limit=programMode["limit"], sort=programMode["sort"], multireddit=programMode["multireddit"], time=programMode["time"] ).upper(),noPrint=True ) try: return extractDetails( getattr( reddit.multireddit(programMode["user"], programMode["multireddit"]), programMode["sort"])(**keyword_params)) except NotFound: raise MultiredditNotFound("Multireddit not found") elif "submitted" in programMode: print ( "submitted posts of {user}\nsort: {sort}\n" \ "time: {time}\nlimit: {limit}\n".format( limit=programMode["limit"], sort=programMode["sort"], user=programMode["user"], time=programMode["time"] ).upper(),noPrint=True ) return extractDetails( getattr( reddit.redditor(programMode["user"]).submissions, programMode["sort"])(**keyword_params)) elif "upvoted" in programMode: print("upvoted posts of {user}\nlimit: {limit}\n".format( user=programMode["user"], limit=programMode["limit"]).upper(), noPrint=True) try: return extractDetails( reddit.redditor( programMode["user"]).upvoted(limit=programMode["limit"])) except Forbidden: raise InsufficientPermission( "You do not have permission to do that") elif "post" in programMode: print("post: {post}\n".format(post=programMode["post"]).upper(), noPrint=True) return extractDetails(reddit.submission(url=programMode["post"]), SINGLE_POST=True)
def getPosts(args): """Call PRAW regarding to arguments and pass it to redditSearcher. Return what redditSearcher has returned. """ config = GLOBAL.config reddit = beginPraw(config) if args["sort"] == "best": raise NoPrawSupport("PRAW does not support that") if "subreddit" in args: if "search" in args: if args["subreddit"] == "frontpage": args["subreddit"] = "all" if "user" in args: if args["user"] == "me": args["user"] = str(reddit.user.me()) if not "search" in args: if args["sort"] == "top" or args["sort"] == "controversial": keyword_params = { "time_filter": args["time"], "limit": args["limit"] } # OTHER SORT TYPES DON'T TAKE TIME_FILTER else: keyword_params = {"limit": args["limit"]} else: keyword_params = {"time_filter": args["time"], "limit": args["limit"]} if "search" in args: if GLOBAL.arguments.sort in ["hot", "rising", "controversial"]: raise InvalidSortingType("Invalid sorting type has given") if "subreddit" in args: print ( "search for \"{search}\" in\n" \ "subreddit: {subreddit}\nsort: {sort}\n" \ "time: {time}\nlimit: {limit}\n".format( search=args["search"], limit=args["limit"], sort=args["sort"], subreddit=args["subreddit"], time=args["time"] ).upper(),noPrint=True ) return redditSearcher( reddit.subreddit(args["subreddit"]).search( args["search"], limit=args["limit"], sort=args["sort"], time_filter=args["time"])) elif "multireddit" in args: raise NoPrawSupport("PRAW does not support that") elif "user" in args: raise NoPrawSupport("PRAW does not support that") elif "saved" in args: raise ("Reddit does not support that") if args["sort"] == "relevance": raise InvalidSortingType("Invalid sorting type has given") if "saved" in args: print("saved posts\nuser:{username}\nlimit={limit}\n".format( username=reddit.user.me(), limit=args["limit"]).upper(), noPrint=True) return redditSearcher(reddit.user.me().saved(limit=args["limit"])) if "subreddit" in args: if args["subreddit"] == "frontpage": print ( "subreddit: {subreddit}\nsort: {sort}\n" \ "time: {time}\nlimit: {limit}\n".format( limit=args["limit"], sort=args["sort"], subreddit=args["subreddit"], time=args["time"] ).upper(),noPrint=True ) return redditSearcher( getattr(reddit.front, args["sort"])(**keyword_params)) else: print ( "subreddit: {subreddit}\nsort: {sort}\n" \ "time: {time}\nlimit: {limit}\n".format( limit=args["limit"], sort=args["sort"], subreddit=args["subreddit"], time=args["time"] ).upper(),noPrint=True ) return redditSearcher( getattr(reddit.subreddit(args["subreddit"]), args["sort"])(**keyword_params)) elif "multireddit" in args: print ( "user: {user}\n" \ "multireddit: {multireddit}\nsort: {sort}\n" \ "time: {time}\nlimit: {limit}\n".format( user=args["user"], limit=args["limit"], sort=args["sort"], multireddit=args["multireddit"], time=args["time"] ).upper(),noPrint=True ) try: return redditSearcher( getattr(reddit.multireddit(args["user"], args["multireddit"]), args["sort"])(**keyword_params)) except NotFound: raise MultiredditNotFound("Multireddit not found") elif "submitted" in args: print ( "submitted posts of {user}\nsort: {sort}\n" \ "time: {time}\nlimit: {limit}\n".format( limit=args["limit"], sort=args["sort"], user=args["user"], time=args["time"] ).upper(),noPrint=True ) return redditSearcher( getattr(reddit.redditor(args["user"]).submissions, args["sort"])(**keyword_params)) elif "upvoted" in args: print("upvoted posts of {user}\nlimit: {limit}\n".format( user=args["user"], limit=args["limit"]).upper(), noPrint=True) try: return redditSearcher( reddit.redditor(args["user"]).upvoted(limit=args["limit"])) except Forbidden: raise InsufficientPermission( "You do not have permission to do that") elif "commented" in args: print("commented posts of {user}\nlimit: {limit}\n".format( user=args["user"], limit=args["limit"]).upper(), noPrint=True) try: comments = reddit.redditor( args["user"]).comments.new(limit=args["limit"]) return redditSearcher((comment.submission for comment in comments)) except Forbidden: raise InsufficientPermission( "You do not have permission to do that") elif "post" in args: return redditSearcher(reddit.submission(url=args["post"]), SINGLE_POST=True)