-
Notifications
You must be signed in to change notification settings - Fork 1
/
glslsandbox.py
117 lines (96 loc) · 3.46 KB
/
glslsandbox.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
import urllib.request
import json
import requests
def save_from_list():
shader_list_file = open('shader.list')
shader_list = []
for line in shader_list_file:
if line == '':
continue
if line.startswith('#'):
continue
item = line.split(' ')
item_id, title = item[0], item[-1].rstrip()
shader_list.append((item_id, title))
for s in shader_list:
shader_id = s[0]
shader_title = s[1]
try:
with open(f'.\\shader\\glslsandbox\\{shader_title}.glsl') as handler:
s_id = shader_id.split('.')[0]
print('writing', s_id, '.glsl')
new_file = open(f'.\\shader\\glslsandbox_shaders\\{s_id}.glsl', "w")
new_file.write(handler.read())
new_file.close()
except FileNotFoundError as e:
print('failed to open', e)
'''
# download images
img_id = shader_id.split('.')[0]
img_data = requests.get(f"http://glslsandbox.com/thumbs/{img_id}.png").content
with open(f'.\\shader\\thumbs\\{img_id}.png', 'wb') as handler:
print("saving ...", img_id, )
handler.write(img_data)
print("done")
'''
'''
# shader source json
fp = urllib.request.urlopen(f"http://glslsandbox.com/item/{shader_id}")
mybytes = fp.read()
mystr = mybytes.decode("utf8")
fp.close()
# download shader source
fn = f".\\shader\\glslsandbox\\{shader_title}.glsl"
f = open(fn, "w")
shader_json = json.loads(mystr)
shader_json['id'] = shader_id
f.write(json.dumps(shader_json))
print("writing", fn)
f.close()
'''
print(shader_list)
def _dump_from_glslsandbox():
dump_dir = '.\\glslsandbox_dump\\'
# last run end 47520-400
start_id = 45000 - 50
for i in range(50):
shader_id = start_id - i
# download images
url = f"http://glslsandbox.com/thumbs/{shader_id}.png"
print("saving...", url, )
img_data = requests.get(url).content
with open(f'{dump_dir}thumbs\\{shader_id}.png', 'wb') as handler:
handler.write(img_data)
print("done")
# shader source json
fp = urllib.request.urlopen(f"http://glslsandbox.com/item/{shader_id}")
mybytes = fp.read()
mystr = mybytes.decode("utf8")
fp.close()
# download shader source
fn = f"{dump_dir}source\\{shader_id}.glsl"
print("saving", fn, )
with open(fn, 'w') as f:
shader_json = json.loads(mystr)
shader_json['id'] = shader_id
f.write(json.dumps(shader_json))
print("done")
def _rename_images():
shader_list_file = open('shader.list')
for line in shader_list_file:
if line == '':
continue
if line.startswith('#'):
continue
item = line.split(' ')
shader_id, shader_title = item[0], item[-1].rstrip()
shader_id = shader_id.split('.')[0]
with open(f'.\\shader\\dump\\thumbs\\{shader_id}.png', 'rb') as orig:
with open(f'.\\shader\\dump\\thumbs\\{shader_title}.png', 'wb') as handler:
handler.write(orig.read())
print(f"renaming {shader_id} to {shader_title}.png", )
print("done")
if __name__ == '__main__':
# save_from_list(shader_list)
#_dump_from_glslsandbox()
_rename_images()