-
Notifications
You must be signed in to change notification settings - Fork 2
/
convert_to_json.py
191 lines (160 loc) · 6.21 KB
/
convert_to_json.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"""
This script converts an XML metadata from extract_heic.py script to a json file for WDD and makes a .dww file
"""
import os
import shutil
import sys
from zipfile import ZipFile
from bs4 import BeautifulSoup as Soup
inp_dir = sys.argv[1]
def solar_convert(input_data, json):
sunrise_img = []
day_img = []
sunset_img = []
night_img = []
input_data.seek(0, 0)
for string in input_data:
if string == 'a\n':
altitude = float(input_data.readline())
input_data.readline()
index = int(input_data.readline())
input_data.readline()
azimuth = float(input_data.readline())
if (azimuth <= 180) and (altitude <= 0):
sunrise_img.append([azimuth, altitude, index])
elif (azimuth <= 180) and (altitude > 0):
day_img.append([azimuth, altitude, index])
elif (azimuth > 180) and (altitude > 0):
sunset_img.append([azimuth, altitude, index])
elif (azimuth > 180) and (altitude <= 0):
night_img.append([azimuth, altitude, index])
i = 0
json.write('\t"sunriseImageList": [\n')
if sunrise_img:
if len(sunrise_img) > 1:
for i in range(len(sunrise_img) - 1):
json.write('\t\t' + str(sunrise_img[i][2] + 1) + ',\n')
json.write('\t\t' + str(sunrise_img[i + 1][2] + 1) + '\n')
else:
json.write('\t\t' + str(sunrise_img[0][2] + 1) + '\n')
else:
json.write('\t\t' + str(day_img[0][2] + 1) + ',\n')
json.write('\t],\n')
json.write('\t"dayImageList": [\n')
if len(day_img) > 1:
for i in range(len(day_img) - 1):
json.write('\t\t' + str(day_img[i][2] + 1) + ',\n')
json.write('\t\t' + str(day_img[i + 1][2] + 1) + '\n')
else:
json.write('\t\t' + str(day_img[0][2] + 1) + '\n')
json.write('\t],\n')
json.write('\t"sunsetImageList": [\n')
if sunset_img:
if len(sunset_img) > 1:
for i in range(len(sunset_img) - 1):
json.write('\t\t' + str(sunset_img[i][2] + 1) + ',\n')
json.write('\t\t' + str(sunset_img[i + 1][2] + 1) + '\n')
else:
json.write('\t\t' + str(sunset_img[0][2] + 1) + '\n')
else:
json.write('\t\t' + str(night_img[0][2] + 1) + ',\n')
json.write('\t],\n')
json.write('\t"nightImageList": [\n')
if len(night_img) > 1:
for i in range(len(night_img) - 1):
json.write('\t\t' + str(night_img[i][2] + 1) + ',\n')
json.write('\t\t' + str(night_img[i + 1][2] + 1) + '\n')
else:
json.write('\t\t' + str(night_img[0][2] + 1) + '\n')
json.write('\t]\n')
return json
def h24_convert(input_data, json):
sunrise_img = []
day_img = []
sunset_img = []
night_img = []
input_data.seek(0, 0)
for string in input_data:
if string == 'i\n':
index = int(input_data.readline())
input_data.readline()
time = float(input_data.readline()) * 24
if (time >= 6) and (time <= 8):
sunrise_img.append([time, index])
elif (time > 8) and (time < 19):
day_img.append([time, index])
elif (time >= 19) and (time <= 21):
sunset_img.append([time, index])
else:
night_img.append([time, index])
i = 0
json.write('\t"sunriseImageList": [\n')
if sunrise_img:
if len(sunrise_img) > 1:
for i in range(len(sunrise_img) - 1):
json.write('\t\t' + str(sunrise_img[i][1] + 1) + ',\n')
json.write('\t\t' + str(sunrise_img[i + 1][1] + 1) + '\n')
else:
json.write('\t\t' + str(sunrise_img[0][1] + 1) + '\n')
else:
json.write('\t\t' + str(day_img[0][1] + 1) + ',\n')
json.write('\t],\n')
json.write('\t"dayImageList": [\n')
if len(day_img) > 1:
for i in range(len(day_img) - 1):
json.write('\t\t' + str(day_img[i][1] + 1) + ',\n')
json.write('\t\t' + str(day_img[i + 1][1] + 1) + '\n')
else:
json.write('\t\t' + str(day_img[0][1] + 1) + '\n')
json.write('\t],\n')
json.write('\t"sunsetImageList": [\n')
if sunset_img:
if len(sunset_img) > 1:
for i in range(len(sunset_img) - 1):
json.write('\t\t' + str(sunset_img[i][1] + 1) + ',\n')
json.write('\t\t' + str(sunset_img[i + 1][1] + 1) + '\n')
else:
json.write('\t\t' + str(sunset_img[0][1] + 1) + '\n')
else:
json.write('\t\t' + str(night_img[0][1] + 1) + ',\n')
json.write('\t],\n')
json.write('\t"nightImageList": [\n')
if len(night_img) > 1:
for i in range(len(night_img) - 1):
json.write('\t\t' + str(night_img[i][1] + 1) + ',\n')
json.write('\t\t' + str(night_img[i + 1][1] + 1) + '\n')
else:
json.write('\t\t' + str(night_img[0][1] + 1) + '\n')
json.write('\t]\n')
return json
if __name__ == "__main__":
if not os.path.isdir(inp_dir + '_wdd'):
shutil.copytree(inp_dir, inp_dir + '_wdd')
inp = open(inp_dir + '_wdd/metadata.xml', mode='r')
meta = Soup(inp.read(), features="lxml")
out = open(inp_dir + '_wdd/' + inp_dir + '_wdd.json', mode='w')
temp = open(inp_dir + '_wdd/temp.txt', mode='w+')
temp.write(meta.get_text())
temp.seek(0, 0)
out.write('{\n'
'\t"imagesZipUri": "' + inp_dir + '.ddw",\n'
'\t"imageFilename": "' + os.path.basename(inp_dir).replace(' ', '_') + '_*.jpg",\n'
'\t"imageCredits": "convert_to_json.py by kolaqsq",\n'
'\t"displayName": "' + inp_dir + '",\n')
for line in temp:
if line.find('a\n') != -1:
solar_convert(temp, out)
break
if line.find('t\n') != -1:
h24_convert(temp, out)
break
out.write('}\n')
inp.close()
out.close()
temp.close()
os.remove(inp_dir + '_wdd/metadata.xml')
os.remove(inp_dir + '_wdd/temp.txt')
with ZipFile(inp_dir + '.ddw', 'w') as theme_file:
for folderName, subfolders, filenames in os.walk(inp_dir + '_wdd'):
for filename in filenames:
theme_file.write(inp_dir + '_wdd/' + filename, filename)