Exemplo n.º 1
0
def pull(variable):
    driver = Driver()
    driver.connect(mode='heroku')
    query = 'select row_to_json({0}) from {0}'.format(variable)
    result = driver.pull(query)
    result = [x for t in result for x in t]
    output_dct = {'content': result}
    return jsonify(output_dct)
Exemplo n.º 2
0
 def update(self):
     self.reddit = self.auth()
     driver = Driver()
     subs = driver.pull('select * from {0}'.format(self.subreddits_table))
     self.subreddits_names = [x for t in subs for x in t]
     self.subreddits_names = [
         x for x in self.subreddits_names if isinstance(x, str)
     ]
Exemplo n.º 3
0
class Manager(object):
    def __init__(self):
        self.account = ''
        self.driver = Driver()
        self.streamer = Streamer()
        self.stream_table = 'stream'
        self.accounts_table = 'accounts'
        self.limit = self.driver.limit

    def auth(self, idx=1):
        query = 'SELECT * FROM accounts WHERE id = {0};'
        count_query = 'SELECT count(*) FROM {0};'
        query = query.format(idx)
        count_query = count_query.format(self.accounts_table)
        count = self.driver.pull(count_query)[0][0]
        if (idx > count):
            self.auth(1)
            return
        try:
            account = self.driver.pull(query)[0]
            self.account = {
                'id': account[0],
                'CLIENT_ID': account[1],
                'CLIENT_SECRET': account[2],
                'PASSWORD': account[3],
                'USER_AGENT': account[4],
                'USERNAME': account[5]
            }
        except:
            self.auth(idx + 1)
            return

    def build(self):
        self.streamer.account = self.account
        self.streamer()

    def get_type(self, post):
        types = ['comment', 'submission']
        post_type = str(type(post)).lower()
        for t in types:
            if t in post_type:
                return t

    def run(self):
        self.driver.check(self.stream_table)
        select_query = 'select * from {0} where reddit_id = %s'
        insert_query = 'insert into {0} (reddit_id,class) values (%s,%s)'
        idx = 0
        try:
            for post in self.streamer:
                idx += 1
                post_type = self.get_type(post)
                copies = self.driver.pull_var(
                    select_query.format(self.stream_table), (post.id, ))
                copies = [x for t in copies for x in t]
                if not any(copies):
                    self.driver.push_var(
                        insert_query.format(self.stream_table),
                        (post.id, post_type))
                if idx >= self.limit:
                    self.driver.check(self.stream_table)
                    idx = 1
        except:
            self.auth()
            self.build()
            self.run()

    def __call__(self):
        self.auth()
        self.build()
        self.run()
Exemplo n.º 4
0
class Streamer(object):
    def __init__(self,*args,**kwargs):
        self.reddit = ''
        self.account = ''
        self.driver = Driver()
        self.bot = ArchiveBot()
        self.error = "There was an error processing your request."

    def auth(self):
        return  praw.Reddit(client_id=self.account['CLIENT_ID'],
                           client_secret=self.account['CLIENT_SECRET'],
                           password=self.account['PASSWORD'],
                           user_agent=self.account['USER_AGENT'],
                           username=self.account['USERNAME'])

    def connect(self,mode='local'):
        self.driver.connect(mode='local')
        query = 'SELECT * FROM accounts'
        account = self.driver.pull(query)
        account = account[0]
        self.account = {
                'id' : account[0],
                'CLIENT_ID' : account[1],
                'CLIENT_SECRET' : account[2],
                'PASSWORD' : account[3],
                'USER_AGENT' : account[4],
                'USERNAME' : account[5]
            }
        self.reddit = self.auth()

    def compile(self,**kwargs):
        self.results = []
        mentions = self.reddit.inbox.unread(**kwargs)
        self.results.extend(mentions)
        self.results.sort(key=lambda post: post.created_utc, reverse=True)
        return self.results

    def get_body(self,post):
        try:
            post_type = post.parent().__class__.__name__.lower()
        except:
            return self.error
        if post_type == 'submission':
            url = post.parent().url
            title = post.parent().title
            if 'reddit.com' in url:
                try:
                    selftext = post.parent().selftext
                    body =  " ".join([title, selftext])
                except:
                    return self.error
            else:
                return " ".join([title, url])
        elif post_type == 'comment' or post_type == 'message':
            return post.parent().body
        else:
            return self.error

    def get_url(self, body, length):


    def __iter__(self):
        username = self.account['USERNAME']
        stream = praw.models.util.stream_generator(lambda **kwargs: self.compile(**kwargs))
        for idx,post in enumerate(stream):
            self.reddit.inbox.mark_read([post])
            body = ''

            if post.parent().author == username:
                chain = []
                while True:
                    try:
                        parent = post.parent()
                        chain.append(parent)
                    except:
                        break
                chain_authors = [ x.author for x in chain ])
                for idx,item in chain_authors[::-1]:
                    if item == username:
                        original_post = chain[idx-1]
                        body = self.get_body(original_post)
                        break
                else:
                        body = self.get_body(post.parent())
            chain_length = len([x for x in chain if x == username ])
            post.reply(self.get_url(body, chain_length))

if __name__ == '__main__':
    stream = Streamer()
    stream.connect()
    for post in stream:
        print(post)