#!/usr/bin/env python
'''Print an alphabetized list of coauthors for a Scopus author id starting in a
year.'''

import argparse
from scopus.scopus_api import ScopusAbstract
from scopus.scopus_search import ScopusSearch
from scopus.scopus_author import ScopusAuthor

parser = argparse.ArgumentParser()
parser.add_argument("auid", help='The Scopus author id')
parser.add_argument("year", help='Starting year')
parser.add_argument("--exclude-affiliation", help="affiliations to exclude")
args = parser.parse_args()

s = ScopusSearch('AU-ID({args.auid}) AND PUBYEAR > {args.year}'.format(args=args))

coauthors = {}
for eid in s.EIDS:
    ab = ScopusAbstract(eid)
    for au in ab.authors:
        if au.auid not in coauthors and au.auid != args.auid:
            coauthors[au.auid] = au.indexed_name

data = sorted([[auid, name] for auid,name in coauthors.items()], key=lambda x:x[1])

coauthors = [ScopusAuthor(auid) for auid, name in data]

print(', '.join(['{0} ({1})'.format(au.name, au.current_affiliation.split(',')[0])
                 for au in coauthors
                 if au.current_affiliation.split(',')[0] != args.exclude_affiliation]))
