forked from crscillitoe/RoboBanana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_request_structure.py
75 lines (56 loc) · 1.85 KB
/
convert_request_structure.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
import json
from os import listdir
from urllib.parse import urlparse
TEXT_FIELDS = set([
"headerLeft",
"headerRight",
"sideBannerTextOne",
"sideBannerTextTwo",
"sideBannerTextThree",
])
MEDIA_FIELDS = set(["backgroundVideo", "preRollVideo", "headerIcon", "sideBannerIcon"])
def convert_to_text(value: str):
return {"type": "text", "value": value}
def convert_to_media(source: str):
return {"type": "media", "source": source}
def convert_timer(duration: int | None):
if duration is None:
return None
return {"duration": duration}
def convert_title(value: str):
try:
result = urlparse(value)
if all([result.scheme, result.netloc]):
return convert_to_media(value)
return convert_to_text(value)
except:
return convert_to_text(value)
def stay_same(value: any):
return value
CONVERSION_MAPPING = {
"title": convert_title,
"timer": convert_timer,
"display": stay_same,
"scrollingText": stay_same,
"headerLeft": convert_to_text,
"headerRight": convert_to_text,
"sideBannerTextOne": convert_to_text,
"sideBannerTextTwo": convert_to_text,
"sideBannerTextThree": convert_to_text,
"backgroundVideo": convert_to_media,
"preRollVideo": convert_to_media,
"headerIcon": convert_to_media,
"sideBannerIcon": convert_to_media,
}
def main():
new_contents = {}
input_files = listdir("./old_requests")
for input_file in input_files:
with open(f"./old_requests/{input_file}", "r") as f:
original_contents: dict = json.loads(f.read())
for key, value in original_contents.items():
new_contents[key] = CONVERSION_MAPPING[key](value)
with open(f"./new_requests/{input_file}", "w") as f:
f.write(json.dumps(new_contents, indent=2))
if __name__ == "__main__":
main()