#!/usr/bin/python3

import sys
import os
import requests
import re
import time
import datetime
import random

collection = 'funds'
interval = 3600
funds_behr = {
    'robeco_one_defensief': 'rg.one.def',
    'robeco_one_neutraal' : 'rg.one.neut',
    'robeco_one_offensief': 'rg.one.offe',
    # 'robeco_plus_gprec'   : 'rg.bp.gprec',
    # 'robeco_plus_glconsc' : 'rg.gl.consc',
    # 'robeco_plus_uspreme' : 'rg.us.preme',
    # 'robeco_plus_uslcfe'  : 'rg.us.lc.fe'
}
funds_morningstar = {
    'robeco_one_defensief': 'F00000OZ3S',
    'robeco_one_neutraal' : 'F00000OZ3T',
    'robeco_one_offensief': 'F00000OZ3U',
    # 'robeco_plus_gprec'   : 'F00000QDBK', # BP Global Premium Equities C
    # 'robeco_plus_glconsc' : 'F00000PNRA', # Global Conservative Equities C EUR
    # 'robeco_plus_uspreme' : 'F00000OWHQ', # BP US Premium Equities F
    # 'robeco_plus_uslcfe'  : 'F00000QDBI'  # BP US Large Cap Equities F EUR
}
# log = open('/host/var/log/funds.log', 'a')

def behr_backlog(funds):
    for fund,code in funds.items():
        dir = '/var/lib/collectd/rrd/{}/exec-fund-{}'.format(collection, fund)
        if not os.path.isdir(dir):
            url = 'http://www.behr.nl/Beurs/Slotkoersen/slotkoersen.php?fd={}'.format(code)
            # url = 'http://haggis.no-ip.org/funds/{}.html'.format(code)
            response = requests.get(url)
            matches = re.findall('(\d+): ([\d\.]+)', response.text)
            for match in matches:
                for i in range(23):
                    timestamp = int(time.mktime(datetime.datetime.strptime(match[0], '%y%m%d').replace(hour = i + 1).timetuple()))
                    put_value(fund, match[1], timestamp)
        sys.stdout.flush()

def morningstar_ticker(funds):
    for fund,code in funds.items():
        url = 'http://www.morningstar.nl/nl/funds/snapshot/snapshot.aspx?id={}'.format(code)
        response = requests.get(url)
        matches = re.findall('>EUR[\s]*[^\d]*([\d,]+)', response.text)
        quote = matches[0].replace(',', '.')
        # quote = 100 + 50 * (random.random() - 1)
        put_value(fund, quote)

def put_value(fund, value, timestamp = 'N'):
    print('PUTVAL {}/exec-fund-{}/gauge-ticker interval={} {}:{}'.format(collection, fund, interval, timestamp, value))
    sys.stdout.flush()
    if timestamp == 'N':
        timestamp = int(time.time())
    # log.write('{},{},{}\n'.format(fund, timestamp, int(value)))

behr_backlog(funds_behr)
while True:
    seconds = os.times()[4]
    morningstar_ticker(funds_morningstar)
    elapsed = os.times()[4] - seconds
    time.sleep(interval - elapsed)
