コード例 #1
0
    def check_post_table(self, thread_id):
        """
        Checks the table for a post
        :param thread_id: thread id (e.g. 'cghs2')
        :return: db ID or None
        """

        # print("Searching db for " + thread_id)

        a = check_post_table(self.Session, thread_id)
コード例 #2
0
    def grab_data(self, thread_id):
        """
        Gets posts, inserts/updates the database, inserts history entry
        :return: nothing
        """

        # threads = ['42e77i']

        # init skip variable, should be false by default
        skip = False

        # empty tblHistory dict
        history = dict()

        # get created datetime for tblHistory
        history['created'] = datetime.utcnow()

        # make sure oauth tokens are good, since grabbing threads can take a while
        self.o.refresh()

        # get post data from reddit
        retdata = get_post_data(self.r, thread_id)

        # get post data from database
        # dbdata = get_post_data_from_db(self.Session, thread)

        # package the data for skip_logic
        # package = dict()
        # package['reddit'] = dict(thread_id=retdata['id'], comments=retdata['comments'], archived=retdata['archived'])
        # package['database'] = dbdata

        # if the database doesn't contain a record for the post, it will return false
        # if that happens then we don't want to run it through the skip logic

        # get comments for post from reddit
        data = get_comments(self.r, thread_id)

        # if data['status'] == 'C' then the retrieval was successful, so proceed
        if data['status'] == 'C':

            # query the database to see if the post already exists, will either get ID or None
            post_id = check_post_table(self.Session, thread_id)

            # if the post does not exist (no id returned) then insert the post data into db
            # you can do bulk_comment_insert on everything because this is all new data
            if not post_id:
                post_id = insert_post_data(self.Session, retdata)
                bulk_comment_insert(self.Session, data['comments'], post_id)
            else:
                # go through all comments and insert into database
                for comment in data['comments']:
                    insert_comment_data(self.Session, comment, post_id)

            # get finished time for tblHistory
            history['finished'] = datetime.utcnow()

            # build tblhistory entry
            history['message'] = 'Fetched post ID {0} with {1} comments'.format(retdata['id'], len(data['comments']))
            print(history['message'])

            # set status for now, until I'm able to implement error handling
            history['status'] = 'C'

        elif data['status'] == 'F':
            # data['status'] == 'F' so we build the message and send to insert_history
            history = dict(
                status=data['status'],
                finished=datetime.utcnow(),
                message=data['thread'] + ' failed due to ' + data['errormsg']
            )

        # insert message
        insert_history(self.Session, history)

        print("\n")