def get_opener(handlers=[], headers={}, proxies={}): """Get HTTP URL opener and call its `open()` method to open an URL. Arguments: - `handlers`: list, handlers support cookie, authority, and other advanced HTTP features. - `headers`: dictionary, be treated as if add_header() was called with each key and value as arguments, often used to "spoof" the `User-Agent` header or `Referer` header, etc. - `proxies`: dictionary, URL of the proxy, e.g. {'http': 'http://<host>:<port>'}, if your proxy requires authentication: {'http': 'http://<user>:<password>@<host>:<port>'} """ _handlers = [] _handlers.extend(handlers) # proxy handler http_proxy = proxies or \ settings.get('allow_proxy') and settings.get('proxies', None) if http_proxy: try: _handlers.append(urllib2.ProxyHandler(http_proxy)) except Exception, e: print "==> Waring: proxy invalid, please check." print e
def nudgeUsers(): ''' Searches through the comments of the most recent submissions for keyword !Nudge and nudges users through private messaging ''' for submission in subreddit.new(limit=36): for comment in submission.comments: if hasattr(comment, "body"): flag = False if ("!Nudge" in comment.body): sentenceList = comment.body.split() #Edge check if last word of the comment is !Nudge which results in an invalid user if (sentenceList[-1] == "!Nudge"): continue author = comment.author #Avoids infinite response to keyword if (author == settings.get("username")): continue #Avoids duplicate responses to same comment containing keyword for response in comment.replies: if hasattr(response, "body"): if (response.author == settings.get("username")): flag = True break if (not flag): print(comment.body) usernameIndex = sentenceList.index("!Nudge") receivingUser = sentenceList[usernameIndex + 1] #Corrects Reddit name formatting for underscores receivingUser = receivingUser.replace("\\_", "_") try: comment.reply( "You nudged /u/{}".format(receivingUser)) reddit.redditor(receivingUser).message( "{} nudged you".format(author), "{}".format(comment.permalink)) print("Nudge successful") except RedditAPIException as exception: print(exception)
import praw import time import threading from configs import settings from praw.exceptions import RedditAPIException reddit = praw.Reddit(client_id=settings.get("client_id"), client_secret=settings.get("client_secret"), user_agent=settings.get("user_agent"), username=settings.get("username"), password=settings.get("password")) subreddit = reddit.subreddit(settings.get("subreddit")) def nudgeUsers(): ''' Searches through the comments of the most recent submissions for keyword !Nudge and nudges users through private messaging ''' for submission in subreddit.new(limit=36): for comment in submission.comments: if hasattr(comment, "body"): flag = False if ("!Nudge" in comment.body): sentenceList = comment.body.split() #Edge check if last word of the comment is !Nudge which results in an invalid user if (sentenceList[-1] == "!Nudge"): continue author = comment.author #Avoids infinite response to keyword if (author == settings.get("username")):
""" _handlers = [] _handlers.extend(handlers) # proxy handler http_proxy = proxies or \ settings.get('allow_proxy') and settings.get('proxies', None) if http_proxy: try: _handlers.append(urllib2.ProxyHandler(http_proxy)) except Exception, e: print "==> Waring: proxy invalid, please check." print e # gzip/deflate/bzip2 compression handler if settings.get('accept_gzip'): encoding_handler = utils.ContentEncodingProcessor() _handlers.append(encoding_handler) # redirect handler _handlers.append(utils.HTTPRedirectHandler) opener = urllib2.build_opener(*_handlers) ## Add HTTP Request Headers # default HTTP Headers in configures _headers = settings.get('default_headers') # dictionary of HTTP Headers to attach _headers.update(headers)