Projekt

Obecné

Profil

Stáhnout (1.82 KB) Statistiky
| Větev: | Revize:
1
from Utilities.CSV import csv_data_line
2
from Utilities import date_formating
3
import logging
4
from datetime import date
5
import time
6
import datetime
7

    
8
from shared_types import DateDict
9

    
10
logging.basicConfig(filename='../../CrawlerLogs' + 'Crawlerlog-' +
11
                    date.today().strftime("%b-%Y") + '.log',
12
                    level=logging.INFO,
13
                    format='%(asctime)s %(message)s')
14

    
15

    
16
def process_file(filename: str) -> DateDict:
17
    """
18
    Method that take path to crawled file and outputs date dictionary:
19
    Date dictionary is a dictionary where keys are dates in format YYYY-mm-dd-hh (2018-04-08-15)
20
    and value is dictionary where keys are devices (specified in configuration file)
21
    and value is CSVDataLine.csv_data_line with device,date and occurrence
22

    
23
    Args:
24
    filename: name of processed file
25

    
26
    Returns:
27
    None if not implemented
28
    date_dict when implemented
29
    """
30
    date_dict = {}
31

    
32
    with open(filename, "r") as file:
33

    
34
        YEAR_START = 1
35
        YEAR_END = 11
36
        for line in file:
37

    
38
            array = line.split(";")
39

    
40
            #pick later time
41
            time_ = max(
42
                array[2][1:-1],
43
                array[3][1:-1],
44
                key=lambda x: time.mktime(
45
                    datetime.datetime.strptime(x, "%H:%M").timetuple()))
46

    
47
            date = date_formating.date_time_formatter(
48
                array[14][YEAR_START:YEAR_END] + " " + time_)
49

    
50
            name = array[10][1:-1]
51
            if name == "":
52
                continue
53

    
54
            if date not in date_dict:
55
                date_dict[date] = {}
56

    
57
            if name in date_dict[date]:
58
                date_dict[date][name].occurrence = int(array[12])
59
            else:
60
                date_dict[date][name] = csv_data_line.CSVDataLine(
61
                    name, date, int(array[12]))
62

    
63
    return date_dict
(3-3/4)