#!/usr/bin/python3

from __future__ import unicode_literals, print_function
import moesearch
import argparse
import sys

parser = argparse.ArgumentParser("index")
parser.add_argument("board", help="the board you want to read")
parser.add_argument("page",  help="the page you want to read")

def list_index(res):
  for thread_num, index_res in sorted(res.items(), reverse=True):
    print(thread_num, index_res.op.comment)
    try:
      print("it has", len(list(index_res.posts)), "posts!")
    except TypeError:
      print("it has no posts!")
    if(index_res.op.media):
      print(index_res.op.media.media_link)
    print()

if __name__ == "__main__":
  args = parser.parse_args()
  try:
    res = moesearch.index(args.board, args.page)
  except moesearch.exceptions.ArchiveException as e:
    print (">every year >breaking moesearch: ", e)
    sys.exit(1)
  list_index(res)
