Exemplo n.º 1
0
    def process_answers(self, dbquestion_ids):
        """ Get all answers for the list of question ids """
        has_more = True
        page = 1
        base_url = self.url + '/2.2/questions/' + str(
            dbquestion_ids) + '/answers?'
        base_url += 'order=desc&sort=activity&site=stackoverflow&key=' + self.api_key
        base_url += '&' + 'pagesize=' + str(self.pagesize)
        logging.debug("Getting answers for dbquestion ids" +
                      str(dbquestion_ids))
        dbanswers_ids = []

        while has_more:
            url = base_url + "&page=" + str(page)
            if not self.debug:
                data = self._get_api_data(url)
            else:
                has_more = False
                data = StackSampleData.answers

            parser = JSONParser(unicode(data))
            parser.parse()
            # [u'has_more', u'items', u'quota_max', u'quota_remaining']
            has_more = parser.data['has_more']
            page += 1
            data = parser.data['items']

            for answer in data:
                dbanswer = Answers()
                dbanswer.identifier = answer['answer_id']
                dbanswers_ids.append(dbanswer.identifier)
                # dbanswer.body = text
                if 'user_id' in answer['owner']:
                    dbanswer.user_identifier = answer['owner']['user_id']
                    if dbanswer.user_identifier not in self.user_ids_answers:
                        self.user_ids_answers.append(dbanswer.user_identifier)
                dbanswer.question_identifier = answer['question_id']
                create_date = datetime.datetime.fromtimestamp(
                    int(answer['creation_date']))
                dbanswer.submitted_on = create_date.strftime(
                    '%Y-%m-%d %H:%M:%S')
                dbanswer.votes = answer['score']

                self.session.add(dbanswer)
                self.total_answers += 1
                self.user_ids_answers.append(dbanswer.user_identifier)
            self.session.commit()
        # Time to get comments for all answers
        while len(dbanswers_ids) > 0:
            ids = []
            for i in range(self.pagesize):
                if len(dbanswers_ids) > 0:
                    val = dbanswers_ids.pop()
                    if val is not None: ids.append(val)
                    else: logging.info("Found None Answer")
            ids = ";".join([str(x) for x in ids])
            self.process_comments(ids, 'answer')
Exemplo n.º 2
0
        def process_answer(answer):
            dbanswer = Answers()
            dbanswer.identifier = answer['id']
            # dbanswer.body = text
            dbanswer.user_identifier = answer['user_id']
            if answer['username'] not in self.user_ids_answers:
                self.user_ids_answers.append(answer['username'])
            dbanswer.question_identifier = question_id
            dbanswer.submitted_on = answer['updated_at']
            dbanswer.votes = answer['score']
            dbanswer.body = answer['cooked']

            self.session.add(dbanswer)
            self.total_answers += 1