-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
95 lines (76 loc) · 2.21 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import traceback
import logging
import argparse
import yaml
import pandas as pd
from datetime import date
from database.db import save_to_database
logging.getLogger().setLevel(logging.INFO)
def csv_to_array(csv_url):
try:
df = pd.read_csv(csv_url)
except Exception:
return logging.error(traceback.format_exc())
return df
def get_csv_url(ad_network, date):
if len(ad_network) > 0:
for url in ad_network:
if (
str(date.year) in url
and str(date.month) in url
and str(date.day) in url
):
return url
def pars_yaml():
with open("advertising_network.yaml", "r") as stream:
try:
ad_network = yaml.safe_load(stream)
except Exception:
return logging.error(traceback.format_exc())
return ad_network
def parse_cli():
parser = argparse.ArgumentParser(
description="The application takes adNetwork and date as \
input parameters. It then retrieves each report for \
these input parameters from the URLs provided in the \
advertising_network.YAML file and stores it in a database."
)
parser.add_argument(
"-a",
"--adNetwork",
action="store",
required=True,
type=str,
choices=["SuperNetwork", "AdUmbrella"],
help='Choose between "SuperNetwork" or "AdUmbrella"',
)
parser.add_argument(
"-d",
"--date",
action="store",
required=True,
type=date.fromisoformat,
help='Date in format "YYYY-MM-DD", Example: 2014-01-28',
)
return vars(parser.parse_args())
def main():
"""
Entry point for the program
"""
# Parse arguments
cli_config = parse_cli()
ad_network = cli_config["adNetwork"]
date = cli_config["date"]
# Pars yaml
data = pars_yaml()
data_url = data["reports"][ad_network]
# Get CSV from URL
csv_url = get_csv_url(data_url, date)
csv_array = csv_to_array(csv_url)
logging.info('Got CSV')
# Save to database
save_to_database(csv_array)
logging.info('Saved to DB')
if __name__ == "__main__":
main()
logging.info('DONE')