#!/usr/bin/python3

import glob
import os
import sys

import filer

mail_dir = '/home/ernad/var/repsum/mail'
ernad_dir = '/home/ernad/var/repsum/ernad'

def auid_from_hp(hp):
    if 'http://econpapers.repec.org/RAS/' in hp:
        auid = hp[32:]
        auid = auid[:-4]
        return auid
    if 'https://econpapers.repec.org/RAS/' in hp:
        auid = hp[33:]
        auid = auid[:-4]
        return auid
    if 'http://ideas.repec.org/' in hp:
        auid = hp[25:]
        auid = auid[:-5]
        return auid
    if 'http://econpapers.repec.org/p' in hp:
        auid = hp[28:]
        auid = auid[:-4]
        return auid
    if hp == 'https://www.udmercy.edu/index.php':
        return None
    print(hp, file=sys.stderr)
    quit()

def gather_from_fufi(out, fufi):
    repcode = os.path.basename(fufi)[0:7]
    if repcode not in out:
        out[repcode] = {}
    data = filer.load(fufi)
    for ishid in data:
        issuedate = ishid[-10:]
        if 'hp' in data[ishid]:
            hp = data[ishid]['hp']
            if hp is None:
                continue
            auid = auid_from_hp(hp)
            if auid is None:
                return
            if auid == '':
                print(hp)
                quit()
            if auid not in out[repcode]:
                out[repcode][auid] = {}
                out[repcode][auid]['names'] = []
                if 'name' in data[ishid] and data[ishid] is not None:
                    name = data[ishid]['name']
                    if name not in out[repcode][auid]['names']:
                        out[repcode][auid]['names'].append(name)
                out[repcode][auid]['from'] = issuedate
                out[repcode][auid]['until'] = issuedate
            else:
                if issuedate < out[repcode][auid]['from']:
                    out[repcode][auid]['from'] = issuedate
                if issuedate > out[repcode][auid]['until']:
                    out[repcode][auid]['until'] = issuedate


out = {}

for fufi in glob.glob(mail_dir + '/*.json'):
    gather_from_fufi(out, fufi)
for fufi in glob.glob(ernad_dir + '/*.json'):
    gather_from_fufi(out, fufi)

print(out)
#for repcode in out:
#    if len(out[repcode]) == 0:
#        continue
#    print(repcode + ' ' + str(out[repcode]))
