Exemplo n.º 1
0
 def __init__(self):
     warning_filter.ignore()
     self.logger = LogProvider.setup_logging(log_level="DEBUG")
     self.read_config()
     self.multi_thread = MultiThreader()
     self.database = DatabaseProvider()
     try:
         self.load_responders()
         self.praw_handler = MultiprocessHandler()
         self.submission_poller = praw.Reddit(user_agent='Submission-Poller for several logins by /u/DarkMio',
                                              handler=self.praw_handler)
         self.comment_poller = praw.Reddit(user_agent='Comment-Poller for several logins by /u/DarkMio',
                                           handler=self.praw_handler)
     except Exception as e:  # I am sorry linux, but ConnectionRefused Error can't be imported..
         self.logger.error("PRAW Multiprocess server does not seem to be running. "
                           "Please make sure that the server is running and responding. "
                           "Bot is shutting down now.")
         self.logger.error(e)
         self.logger.error(traceback.print_exc())
         exit(-1)
     self.delete_after = self.config.get('REDDIT', 'delete_after')
     self.submission_stream()
     self.comment_stream()
     self.lock = self.multi_thread.get_lock()
     self.multi_thread.go([self.comment_thread], [self.submission_thread], [self.update_thread])
     self.multi_thread.join_threads()
Exemplo n.º 2
0
    def __init__(self, user, multiprocess=False):
        """Initialise a RedditWordCounter object.

        :param user: your Reddit username
        :param multiprocess: if True, will handle requests from multiple RedditWordCounter objects (False by default)
        :return:
        """
        super(RedditWordCounter,
              self).__init__()  # Initialise the WordCounter class
        handler = MultiprocessHandler() if multiprocess else None
        self.user_agent = 'redditvocab/0.1 bot by {0}'.format(user)
        self.reddit = praw.Reddit(user_agent=self.user_agent, handler=handler)
Exemplo n.º 3
0
def main():
    global r
    handler = MultiprocessHandler()
    r = praw.Reddit(user_agent=AccountDetails.USERAGENT, handler=handler)
    r.login(AccountDetails.REDDIT_USERNAME, AccountDetails.REDDIT_PASSWORD)
    sub = AccountDetails.SUBREDDIT
    mods = r.get_moderators(sub)

    for msg in r.get_unread(limit=None):
        if msg.author in mods:
            if msg.subject == AccountDetails.ADD_COMMAND_1 or msg.subject == AccountDetails.ADD_COMMAND_2:
                setFlair(sub, msg.body)
            elif msg.subject == AccountDetails.REMOVE_COMMAND:
                removeFlair(sub, msg.body)
        msg.mark_as_read()
Exemplo n.º 4
0
    def connect(self, controller="Main"):
        r = None  #Praw Connection Object
        handler = MultiprocessHandler()

        # Check the Database for a Stored Praw Key
        db_praw_id = PrawKey.get_praw_id(self.env, controller)
        pk = self.db_session.query(PrawKey).filter_by(id=db_praw_id).first()
        r = praw.Reddit(
            user_agent="Test version of CivilServant by u/natematias",
            handler=handler)

        access_information = {}

        if (pk is None):
            with open(
                    os.path.join(
                        self.base_dir, "config",
                        "access_information_{environment}.pickle".format(
                            environment=self.env)), "rb") as fp:
                access_information = pickle.load(fp)
        else:
            access_information['access_token'] = pk.access_token
            access_information['refresh_token'] = pk.refresh_token
            access_information['scope'] = json.loads(pk.scope)

        r.set_access_credentials(**access_information)

        new_access_information = {}
        update_praw_key = False

        # SAVE OR UPDATE THE ACCESS TOKEN IF NECESSARY
        if (pk is None):
            pk = PrawKey(id=db_praw_id,
                         access_token=r.access_token,
                         refresh_token=r.refresh_token,
                         scope=json.dumps(list(r._authentication)),
                         authorized_username=r.user.json_dict['name'],
                         authorized_user_id=r.user.json_dict['id'])
            self.db_session.add(pk)

        elif (access_information['access_token'] != r.access_token
              or access_information['refresh_token'] != r.refresh_token):
            pk.access_token = r.access_token
            pk.refresh_token = r.refresh_token
            pk.scope = json.dumps(list(r._authentication))

        self.db_session.commit()
        return r
Exemplo n.º 5
0
def getComments(username):
    """
	Return the subreddits a user has commented in.
	"""
    try:
        unique_subs = []
        client = MongoClient()
        reddit = praw.Reddit(user_agent="kNN Subreddit Recommendation Engine",
                             handler=MultiprocessHandler())
        user = reddit.get_redditor(username)
        subs = []
        for comment in user.get_comments(limit=250):
            if comment.subreddit.display_name not in subs:
                subs.append(comment.subreddit.display_name)
            insertSub(comment.subreddit.display_name, client)
        return insertUser(username, subs, client)
    except requests.exceptions.HTTPError as e:
        print e
        pass
Exemplo n.º 6
0
def getSubredditUsers(subreddit):
    """
	Get the commentors in a subreddit. 
	"""
    client = MongoClient()
    reddit = praw.Reddit(user_agent="kNN Subreddit Recommendation Engine",
                         handler=MultiprocessHandler())
    subreddit = reddit.get_subreddit(subreddit)
    comments = subreddit.get_comments(limit=250)
    currentUsers = allUsers(client)
    if currentUsers:
        found = [user['username'] for user in currentUsers]
    else:
        found = []
    users = []
    for comment in comments:
        if comment.author.name not in found:
            users.append({'user': comment.author.name})
    return tempBulkInsert(users, client)
Exemplo n.º 7
0
def main():
    logging.info("Starting bot: " + strftime("%Y-%m-%d %H:%M:%S", gmtime()))
    gc = gspread.login(AccountDetails.GSPREAD_USERNAME, AccountDetails.GSPREAD_PASSWORD)
    doc = gc.open_by_key(AccountDetails.GSPREAD_SHEET).sheet1
    usernames = doc.col_values(2)
    usernames.pop(0) # remove header that just contains the question

    handler = MultiprocessHandler()
    r = praw.Reddit(user_agent='Subot 1.0', handler=handler)
    r.login(AccountDetails.REDDIT_USERNAME_I, AccountDetails.REDDIT_PASSWORD_I)
    
    flairName = 'registered' # choose which flair to apply
    subreddit = AccountDetails.SUBREDDIT
    sub = r.get_subreddit(subreddit) # and which subreddit to run in

    already_added = loadJSON()

    for user in usernames:
        if user not in already_added:
            sub.set_flair(user, '', flairName)
            already_added.append(user)
            r.send_message(user, "Registered!", "Your registration has been received. Check /r/%s now to view your flair." % subreddit)
            logging.info("Registering: %s at %s" %(user, strftime("%Y-%m-%d %H:%M:%S", gmtime())))
            saveJSON(already_added)
Exemplo n.º 8
0
def main():
    # parse the command-line options and arguments
    user, target, options = parse_cmd_line()

    # Check for package updates
    update_check(__name__, __version__)

    # open connection to Reddit
    handler = None

    if options.multiprocess:
        from praw.handlers import MultiprocessHandler
        handler = MultiprocessHandler()

    reddit = praw.Reddit(
        user_agent="/u/{0} reddit analyzer".format(user), handler=handler)

    reddit.config.decode_html_entities = True

    # run analysis
    sys.stderr.write("Analyzing {0}\n".format(target))
    sys.stderr.flush()

    target = target[3:]

    if options.is_subreddit:
        process_subreddit(subreddit=reddit.get_subreddit(target),
                          period=options.period, limit=options.limit,
                          count_word_freqs=options.count_word_freqs,
                          max_threshold=options.max_threshold)
    else:
        process_redditor(redditor=reddit.get_redditor(target), limit=options.limit,
                         count_word_freqs=options.count_word_freqs,
                         max_threshold=options.max_threshold)

    # build a string containing all the words for the word cloud software
    output = ""

    # open output file to store the output string
    out_file_name = "{0}.csv".format(target)

    if options.is_subreddit:
        out_file_name = "subreddit-{0}".format(out_file_name)
    else:
        out_file_name = "user-{0}".format(out_file_name)

    out_file = open(out_file_name, "w")

    # combine singular and plural forms of words into single count
    for word in list(popular_words.keys()):
        count = popular_words[word]

        # e.g.: "picture" and "pictures"
        if word.endswith("s"):
            # if the singular form of the word was used
            singular = word[:-1]
            if popular_words[singular] > 0:

                # combine the count into the most-used form of the word
                if popular_words[singular] > count:
                    popular_words[singular] += popular_words[word]
                    del popular_words[word]
                else:
                    popular_words[word] += popular_words[singular]
                    del popular_words[singular]

        # e.g.: "furry" and "furries"
        if word.endswith("ies"):
            # if the singular form of the word was used
            singular = "{0}y".format(word[:-3])
            if popular_words[singular] > 0:
                # combine the count into the most-used form of the word
                if popular_words[singular] > count:
                    popular_words[singular] += popular_words[word]
                    del popular_words[word]
                else:
                    popular_words[word] += popular_words[singular]
                    del popular_words[singular]

    for word in sorted(popular_words, key=popular_words.get, reverse=True):
        # tweak this number depending on the subreddit
        # some subreddits end up having TONS of words and it seems to overflow
        # the Python string buffer
        if popular_words[word] > 5:
            pri = True

            # don't print the word if it's just a number
            if word.isdigit():
                pri = False

            # add as many copies of the word as it was mentioned in the
            # subreddit
            if pri:
                out_text = str("{0}:{1}\n".format(word, popular_words[word]))
                output += out_text
                out_file.write(out_text)

    out_file.close()

    # print the series of words for the word cloud software
    # place this text into wordle.net
    if options.verbose:
        print(output)

    # save the raw word counts to a file
    if not options.no_raw_data:
        out_file = open("raw-{0}".format(out_file_name), "w")
        for word in sorted(all_words, key=all_words.get, reverse=True):
            out_text = str("{0}:{1}\n".format(word, all_words[word]))
            out_file.write(out_text)
        out_file.close()
        'https://github.com/thepaperpilot/RedditSavedLinksExporter '
        'for source',
        handler=handler)

    reddit.set_oauth_app_info(client_id=settings['client_id'],
                              client_secret=settings['client_secret'],
                              redirect_uri=settings['redirect_uri'])

    return reddit


logging.basicConfig()

os.system("praw-multiprocess & disown")
settings = read_settings()
handler = MultiprocessHandler()
scope = ['identity', 'history', 'save']
r = get_reddit_object()

application = web.Application(
    [
        (r"/", MainHandler),
        (r"/auth", AuthHandler),
        (r"/linksocket", LinkSocketHandler),
    ],
    cookie_secret=settings['cookie_secret'],
    template_path=os.path.join(os.path.dirname(__file__), "templates"),
    static_path=os.path.join(os.path.dirname(__file__), "static"),
    login_url="/auth",
)
Exemplo n.º 10
0
-now not saving, only load
-now
'''

import sys
from ThisWordHasNLetters import NLettersBot
from bot_comment import ReplyBot
from bot_vote_sub import SubVoteBot
from praw.handlers import MultiprocessHandler
from bot_class import Bot_Instance
from Reply_to_submission import IT_HelpdeskBot

SUB_VISIT_TXT = "sub_visit_time.txt"

#for multiple instances-----------------------------//
handler = MultiprocessHandler('127.0.0.1', 65010)
'''==================================================================//
main function
//=================================================================='''


def Main_Func():

    bot_list = []

    #fansOfHahahahut3
    #FreeKarma
    #create multiple instances-------------//
    ua = '/u/DaMaTaYaDaLa testing only'

    Bot_Instance.Init_Static(
Exemplo n.º 11
0
def main():
    def conditions():
        if comment.id in completed:
            return False
        if not hasattr(comment.author, 'name'):
            return False
        if 'confirm' not in comment.body.lower():
            return False
        if comment.author.name == username:
            return False
        if comment.is_root == True:
            return False
        if comment.banned_by:
            return False
        return True

    def check_self_reply():
        if comment.author.name == parent.author.name:
            item.reply(equal_warning)
            item.report()
            parent.report()
            save()
            return False
        return True

    def verify(item):
        karma = item.author.link_karma + item.author.comment_karma
        age = (datetime.utcnow() -
               datetime.utcfromtimestamp(item.author.created_utc)).days

        if item.author_flair_css_class < 1:
            if age < age_check:
                item.report()
                item.reply(age_warning)
                save()
                return False
            if karma < karma_check:
                item.report()
                item.reply(karma_warning)
                save()
                return False
        return True

    def values(item):
        if not item.author_flair_css_class or item.author_flair_css_class == 'i-none':
            item.author_flair_css_class = 'i-1'
        elif (item.author_flair_css_class
              and ('i-mod' in item.author_flair_css_class
                   or 'i-vendor' in item.author_flair_css_class)):
            pass
        else:
            item.author_flair_css_class = ('i-%d' % (int(''.join(
                [c
                 for c in item.author_flair_css_class if c in '0123456789'])) +
                                                     1))
        if not item.author_flair_text:
            item.author_flair_text = ''

    def flair(item):
        if item.author_flair_css_class != 'i-mod':
            item.subreddit.set_flair(item.author, item.author_flair_text,
                                     item.author_flair_css_class)
            logger.info('Set ' + item.author.name + '\'s flair to ' +
                        item.author_flair_css_class)

        for com in flat_comments:
            if hasattr(com.author, 'name'):
                if com.author.name == item.author.name:
                    com.author_flair_css_class = item.author_flair_css_class

    def save():
        with open(link_id + ".log", 'a') as myfile:
            myfile.write('%s\n' % comment.id)

    try:
        # Load old comments
        with open(link_id + ".log", 'a+') as myfile:
            completed = myfile.read()

        # Log in
        logger.info('Logging in as /u/' + username)
        if multiprocess == 'true':
            handler = MultiprocessHandler()
            r = praw.Reddit(user_agent=username, handler=handler)
        else:
            r = praw.Reddit(user_agent=username)
        r.login(username, password)

        # Get the submission and the comments
        submission = r.get_submission(submission_id=link_id)
        submission.replace_more_comments(limit=None, threshold=0)
        flat_comments = list(praw.helpers.flatten_tree(submission.comments))

        for comment in flat_comments:
            logger.debug("Processing comment: " + comment.id)
            if not hasattr(comment, 'author'):
                continue
            if not conditions():
                continue
            parent = [
                com for com in flat_comments
                if com.fullname == comment.parent_id
            ][0]
            if not hasattr(parent.author, 'link_karma'):
                continue
            if not check_self_reply():
                continue

            if not comment.author.name.lower() in parent.body.lower():
                continue

            # Check Account Age and Karma
            if not verify(comment):
                continue
            if not verify(parent):
                continue

            # Get Future Values to Flair
            values(comment)
            values(parent)

            # Flairs up in here
            flair(comment)
            flair(parent)
            comment.reply(added_msg)
            save()

    except Exception as e:
        logger.error(e)
Exemplo n.º 12
0
 def login(self, username, password, user_agent):
     handler = MultiprocessHandler()
     self.client = praw.Reddit(user_agent=user_agent, handler=handler)
     self.client.login(username, password, disable_warning=True)
Exemplo n.º 13
0
def _login():
    reddit = praw.Reddit(user_agent='Reddcoin Comment Stream Tipbot', handler=MultiprocessHandler())
    reddit.login('ReddcoinRewards', 'phd51blognewstartreddr')
    return reddit