from log import Log, Line
from typing import List
import re
from datetime import datetime

class Ftp(Log):
    def __init__(self, paths: List[str]):
        super().__init__(
            'FTP',
            re.compile(r'((?:Mon|Tue|Wed|Thu|Fri|Sat|Sun) (?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[ ]+\d+ \d+:\d+:\d+ \d+) \d+ (\d+.\d+.\d+.\d+) (\d+) ([^ ]+) .*'),
            paths
        )

    def _readLine(self, line, parts, maxmind):
        # http://www.castaglia.org/proftpd/doc/xferlog.html
        [time, ip, size, request] = parts
        
        if not self.isValidRequest(request):
            return [None, 1]

        time = datetime.strptime(time, '%c').date()
        size = float(size) / 1073741824.0 # byte to gib

        try:
            location = maxmind.city(ip)
        except:
            location = None

        return [Line(ip, time, request, size, location, line[:-1]), 0]
