def fetch_posts(s, newsgroup, last, cid):
  """Fetch newsgroups posts, using the given nntp object s."""
  NUM_POSTS = 10  # we will fetch the last 10 posts (at most)
  try:
    start = str(int(last) - NUM_POSTS)
    _, items = s.xover(start, last)
    posts = []
    for (article_id, subject, author, _, _, _,
         _, _) in items:
      _, _, _, text = s.article(article_id)
      article = []
      for l in text:
        try:
          article.append(l.decode('utf-8', errors='ignore'))
        except __HOLE__:
          logging.exception('error on: %s', l)

      post_info = [article_id, author.decode('utf-8', errors='ignore'),
                   subject.decode('utf-8', errors='ignore'), len(text), article]
      posts.append(post_info)
    if posts:
      pd = PostData(posts=posts)
      pd.put()
      pid = pd.key.id()
      logging.debug('pid: %s, cid: %s', pid, cid)
      channel.send_message(cid, json.dumps(
          {'pid': pid, 'newsgroup': newsgroup}))
  except:
    logging.exception('Something went wrong...')
dataset/ETHPy150Open GoogleCloudPlatform/appengine-sockets-python-java-go/python_socket_demo/main.py/fetch_posts