-
Notifications
You must be signed in to change notification settings - Fork 5
/
aggregator.py
executable file
·85 lines (73 loc) · 3.19 KB
/
aggregator.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
import datetime
import json
import os
import jdatetime
# currencies = ['usd', 'eur', 'gbp', 'chf', 'cad', 'aud', 'sek', 'nok', 'rub', 'thb', 'sgd', 'hkd', 'azn', 'amd', 'dkk',
# 'aed', 'jpy', 'try', 'cny', 'sar', 'inr', 'myr', 'afn', 'kwd', 'iqd', 'bhd', 'omr', 'qar', 'emami1',
# 'azadi1g', 'azadi1', 'azadi1_2', 'azadi1_4']
def aggregator(files: iter) -> dict:
date_json_map = dict()
for file_path in files:
date_str = '/'.join(file_path.split('/')[1:])
with open(file_path, encoding="utf8") as f:
data = json.load(f)
date_json_map[date_str] = data
return date_json_map
def walker(base_directory: str, dt):
year_directories = sorted(
[
d for d in os.listdir(os.path.join(base_directory))
if os.path.isdir(os.path.join(base_directory, d)) and d.isdigit()
],
)
for year in year_directories:
month_directories = sorted(
[
d for d in os.listdir(os.path.join(base_directory, year))
if os.path.isdir(os.path.join(base_directory, year, d)) and d.isdigit()
],
)
# monthly
for month in month_directories:
path = os.path.join(base_directory, year, month)
# if os.path.exists(os.path.join(path, 'full')):
# continue
# date = dt.datetime.strptime(f'{year}/{month}', '%Y/%m')
# last_date_in_month = date.replace(
# year=date.year + 1 if date.month == 12 else date.year,
# month=1 if date.month == 12 else date.month + 1,
# day=1
# ) - dt.timedelta(days=1)
# if os.path.exists(os.path.join(path, last_date_in_month.strftime('%d'))):
aggregated_json = aggregator(
sorted(
[
os.path.join(path, f) for f in os.listdir(path)
if os.path.isfile(os.path.join(path, f)) and f.isdigit()
],
)
)
with open(os.path.join(path, 'full'), "w", encoding="utf8") as f:
json.dump(aggregated_json, f, ensure_ascii=False)
# yearly
path = os.path.join(base_directory, year)
# if os.path.exists(os.path.join(path, 'full')):
# continue
# date = dt.datetime.strptime(f'{year}', '%Y')
# last_date_in_year = date.replace(year=date.year + 1, month=1, day=1) - dt.timedelta(days=1)
file_list = []
# if os.path.exists(os.path.join(path, last_date_in_year.strftime('%m'), last_date_in_year.strftime('%d'))):
for month in month_directories:
file_list.extend(
sorted(
[
os.path.join(path, month, f) for f in os.listdir(os.path.join(path, month))
if os.path.isfile(os.path.join(path, month, f)) and f.isdigit()
],
)
)
aggregated_json = aggregator(file_list)
with open(os.path.join(path, 'full'), "w", encoding="utf8") as f:
json.dump(aggregated_json, f, ensure_ascii=False)
walker('gregorian', datetime)
walker('jalali', jdatetime)