2015-11-22 2 views
1

Я использую Python Reddit API Wrapper (PRAW) для сбора конкретных комментариев от Reddit, и одна из функций, которые я обычно использую, - replace_more_comments(), чтобы собрать все комментарии для потока.PRAW: индикатор выполнения для replace_more_comments()?

Некоторые из этих потоков довольно большие - например, 10 000 комментариев - и найдите время, чтобы собрать все комментарии. Есть ли способ показать индикатор выполнения для replace_more_comments()?

Вот минимальный рабочий пример кода:

import praw 
r = praw.Reddit('MSU vs Nebraska game') 
submission = r.get_submission(submission_id='3rxx3y') 
flat_comments = praw.helpers.flatten_tree(submission.comments) 
submission.replace_more_comments(limit=None, threshold=0) 
all_comments = submission.comments 
flat_comments = praw.helpers.flatten_tree(submission.comments) 

ответ

0

Встроенный в реализации replace_more_comments не поддерживает это, но вы можете написать свою собственную версию. Для справки, here's the original implementation.

Я не знаю, как нарисовать реальный индикатор выполнения; вам нужно будет написать update_progress_bar. Я также не тестировал этот код, и он может вообще не работать.

def replace_more_comments(self, post): 
    """Update the comment tree by replacing instances of MoreComments.""" 
    if post._replaced_more: 
     return 

    more_comments = post._extract_more_comments(comment.comments) 

    # Estimate the total number of comments 
    count = 0 
    for item in more_comments: 
     count += item.count 

    update_progress_bar(0, count) 

    num_loaded = 0 

    while more_comments: 
     item = heappop(more_comments) 

     # Fetch new comments and decrease remaining if a request was made 
     new_comments = item.comments(update=False) 
     elif new_comments is None: 
      continue 

     # Re-add new MoreComment objects to the heap of more_comments 
     for more in self._extract_more_comments(new_comments): 
      more._update_submission(post) # pylint: disable=W0212 
      heappush(more_comments, more) 
     # Increase progress bar 
     num_loaded += len(new_comments) 
     update_progress_bar(num_loaded, count) 
     # Insert the new comments into the tree 
     for comment in new_comments: 
      post._insert_comment(comment) 

    post._replaced_more = True 
Смежные вопросы