Skip to content

Commit 0c35ce8

Browse files
committed
LOL: Change urllib handling; Add settings to Addon preferences
1 parent e51f239 commit 0c35ce8

3 files changed

Lines changed: 87 additions & 62 deletions

File tree

operators/lol/update_ToC.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@
2727

2828
from bpy.types import Operator
2929
from ...utils.lol import utils as utils
30-
from ... import bl_info
31-
32-
blc_ver = '.'.join([str(_) for _ in bl_info["version"]])
33-
useragent = f"BlendLuxCore/{blc_ver}" # user agent for urllib request to LOL
34-
35-
3630

3731
class LOLUpdateTOC(Operator):
3832
bl_idname = 'scene.luxcore_ol_update_toc'
@@ -52,39 +46,44 @@ def execute(self, context):
5246

5347
name = 'bl_ext.user_default.' + basename(dirname(dirname(dirname(__file__))))
5448
user_preferences = context.preferences.addons[name].preferences
49+
LOL_HOST_URL = user_preferences.lol_host
50+
LOL_VERSION = user_preferences.lol_version
51+
LOL_HTTP_HOST = user_preferences.lol_http_host
52+
LOL_USERAGENT = user_preferences.lol_useragent
53+
5554

5655
filepath = join(user_preferences.global_dir, 'assets_model_blendermarket.json')
57-
urlstr = utils.LOL_HOST_URL + "/" + utils.LOL_VERSION + "/assets_model_blendermarket.json"
56+
urlstr = LOL_HOST_URL + "/" + LOL_VERSION + "/assets_model_blendermarket.json"
5857

5958
req = urllib.request.Request(
60-
urlstr, data=None, headers={'User-Agent': useragent}
59+
urlstr, headers={'User-Agent': LOL_USERAGENT, 'Host': LOL_HTTP_HOST}
6160
)
62-
with urllib.request.urlopen(req, timeout=60) as request:
63-
assets = json.load(request)
61+
with urllib.request.urlopen(req, timeout=60) as response:
62+
assets = json.load(response)
6463

6564
with open(filepath, 'w') as file:
6665
file.write(json.dumps(assets, indent=2))
6766

6867
filepath = join(user_preferences.global_dir, 'assets_model.json')
69-
urlstr = utils.LOL_HOST_URL + "/" + utils.LOL_VERSION + "/assets_model.json"
68+
urlstr = LOL_HOST_URL + "/" + LOL_VERSION + "/assets_model.json"
7069

7170
req = urllib.request.Request(
72-
urlstr, data=None, headers={'User-Agent': useragent}
71+
urlstr, headers={'User-Agent': LOL_USERAGENT, 'Host': LOL_HTTP_HOST}
7372
)
74-
with urllib.request.urlopen(req, timeout=60) as request:
75-
assets = json.load(request)
73+
with urllib.request.urlopen(req, timeout=60) as response:
74+
assets = json.load(response)
7675

7776
with open(filepath, 'w') as file:
7877
file.write(json.dumps(assets, indent=2))
7978

8079
filepath = join(user_preferences.global_dir, 'assets_material.json')
81-
urlstr = utils.LOL_HOST_URL + "/" + utils.LOL_VERSION + "/assets_material.json"
80+
urlstr = LOL_HOST_URL + "/" + LOL_VERSION + "/assets_material.json"
8281

8382
req = urllib.request.Request(
84-
urlstr, data=None, headers={'User-Agent': useragent}
83+
urlstr, headers={'User-Agent': LOL_USERAGENT, 'Host': LOL_HTTP_HOST}
8584
)
86-
with urllib.request.urlopen(req, timeout=60) as request:
87-
assets = json.load(request)
85+
with urllib.request.urlopen(req, timeout=60) as response:
86+
assets = json.load(response)
8887

8988
with open(filepath, 'w') as file:
9089
file.write(json.dumps(assets, indent=2))

ui/addon_preferences.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import bpy
2-
from os.path import basename, dirname
32
from bpy.types import AddonPreferences
43
from bpy.props import IntProperty, StringProperty, EnumProperty, BoolProperty
4+
55
from ..ui import icons
66
from .. import utils
77
from ..utils.lol import utils as lol_utils
88
from importlib.metadata import version
9+
from .. import bl_info
910

11+
blc_ver = '.'.join([str(_) for _ in bl_info["version"]])
1012

1113
film_device_items = []
1214

13-
1415
class LuxCoreAddonPreferences(AddonPreferences):
1516
# id name for 4.2
1617
bl_idname = "bl_ext.user_default.BlendLuxCore"
@@ -59,6 +60,26 @@ def film_device_items_callback(self, context):
5960
description="Global storage for your assets, will use subdirectories for the contents",
6061
subtype='DIR_PATH', default=lol_utils.get_default_directory()
6162
)
63+
lol_host: StringProperty(
64+
name="Host URL",
65+
description="Address of the LuxCore Online Library server",
66+
default = "https://luxcorerender.org/lol",
67+
)
68+
lol_http_host: StringProperty(
69+
name="HTTP Host",
70+
description=" FOR DEVELOPERS ONLY! DO NOT EDIT! Hostname transferred with HTTP(S) request. Needed for technical reasons.",
71+
default = "www.luxcorerender.org",
72+
)
73+
lol_version: StringProperty(
74+
name="Library Version",
75+
description="Version of the LuxCore Online Library.",
76+
default = "v2.5",
77+
)
78+
lol_useragent: StringProperty(
79+
name="HTTP User-Agent",
80+
description="User Agent transmitted with requests",
81+
default = f"BlendLuxCore/{blc_ver}",
82+
)
6283

6384
max_assetbar_rows: IntProperty(name="Max Assetbar Rows", description="max rows of assetbar in the 3d view",
6485
default=1, min=0, max=20)
@@ -125,6 +146,10 @@ def draw(self, context):
125146
col.prop(self, "use_library")
126147
if self.use_library:
127148
col.prop(self, "global_dir")
149+
col.prop(self, "lol_host")
150+
col.prop(self, "lol_http_host")
151+
# col.prop(self, "lol_version") # unlikely to need change so not even needed for developers
152+
# col.prop(self, "lol_useragent") # unlikely to need change so not even needed for developers
128153
col.prop(self, "thumb_size")
129154

130155
# pyluxcore version

utils/lol/utils.py

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,6 @@
4040
from threading import _MainThread, Thread, Lock
4141
from ...handlers.lol.timer import timer_update
4242
from ...utils import get_addon_preferences, compatibility
43-
from ... import bl_info
44-
45-
46-
LOL_HOST_URL = "https://luxcorerender.org/lol"
47-
# LOL_HOST_URL = "https://www.sciencehooligans.de/lol"
48-
LOL_VERSION = "v2.5"
49-
blc_ver = '.'.join([str(_) for _ in bl_info["version"]])
50-
useragent = f"BlendLuxCore/{blc_ver}" # user agent for urllib request to LOL
5143

5244
download_threads = []
5345
bg_threads = []
@@ -78,6 +70,10 @@ def load_local_TOC(context, asset_type):
7870
def load_patreon_assets(context):
7971
name = 'bl_ext.user_default.' + basename(dirname(dirname(dirname(__file__))))
8072
user_preferences = context.preferences.addons[name].preferences
73+
LOL_HOST_URL = user_preferences.lol_host
74+
LOL_VERSION = user_preferences.lol_version
75+
LOL_HTTP_HOST = user_preferences.lol_http_host
76+
LOL_USERAGENT = user_preferences.lol_useragent
8177

8278
#check if local file is available
8379
filepath = join(user_preferences.global_dir, 'assets_model_blendermarket.json')
@@ -99,11 +95,11 @@ def load_patreon_assets(context):
9995
import urllib.request
10096
urlstr = LOL_HOST_URL + "/" + LOL_VERSION + "/assets_model_blendermarket.json"
10197
req = urllib.request.Request(
102-
urlstr, data=None, headers={'User-Agent': useragent}
98+
urlstr, headers={'User-Agent': LOL_USERAGENT, 'Host': LOL_HTTP_HOST}
10399
)
104-
with urllib.request.urlopen(req, timeout=60) as request:
100+
with urllib.request.urlopen(req, timeout=60) as response:
105101
import json
106-
assets = json.load(request)
102+
assets = json.load(response)
107103

108104
#cache file for future offline work
109105
with open(filepath, 'w') as file:
@@ -129,6 +125,10 @@ def download_table_of_contents(context):
129125
ui_props = context.scene.luxcoreOL.ui
130126
name = 'bl_ext.user_default.' + basename(dirname(dirname(dirname(__file__))))
131127
user_preferences = context.preferences.addons[name].preferences
128+
LOL_HOST_URL = user_preferences.lol_host
129+
LOL_VERSION = user_preferences.lol_version
130+
LOL_HTTP_HOST = user_preferences.lol_http_host
131+
LOL_USERAGENT = user_preferences.lol_useragent
132132

133133
try:
134134
for threaddata in bg_threads:
@@ -154,11 +154,11 @@ def download_table_of_contents(context):
154154
import urllib.request
155155
urlstr = LOL_HOST_URL + "/" + LOL_VERSION + "/assets_model.json"
156156
req = urllib.request.Request(
157-
urlstr, data=None, headers={'User-Agent': useragent}
157+
urlstr, headers={'User-Agent': LOL_USERAGENT, 'Host': LOL_HTTP_HOST}
158158
)
159-
with urllib.request.urlopen(req, timeout=60) as request:
159+
with urllib.request.urlopen(req, timeout=60) as response:
160160
import json
161-
assets = json.load(request)
161+
assets = json.load(response)
162162
# cache file for future offline work
163163
with open(filepath, 'w') as file:
164164
file.write(json.dumps(assets, indent=2))
@@ -174,16 +174,6 @@ def download_table_of_contents(context):
174174
assets.extend(load_patreon_assets(context))
175175
scene.luxcoreOL.model['assets'] = assets
176176

177-
# with urllib.request.urlopen(LOL_HOST_URL + "/assets_scene.json", timeout=60) as request:
178-
# import json
179-
# assets = json.loads(request.read())
180-
# for asset in scene.luxcoreOL.scene['assets']:
181-
# asset['downloaded'] = 0.0
182-
# asset['local'] = False
183-
#
184-
# assets.extend(load_local_TOC(context, 'scene'))
185-
# scene.luxcoreOL.scene['assets'] = assets
186-
187177
# check if local file is available
188178
filepath = join(user_preferences.global_dir, 'assets_material.json')
189179
if os.path.exists(filepath):
@@ -199,11 +189,11 @@ def download_table_of_contents(context):
199189
import urllib.request
200190
urlstr = LOL_HOST_URL + "/" + LOL_VERSION + "/assets_material.json"
201191
req = urllib.request.Request(
202-
urlstr, data=None, headers={'User-Agent': useragent}
192+
urlstr, headers={'User-Agent': LOL_USERAGENT, 'Host': LOL_HTTP_HOST}
203193
)
204-
with urllib.request.urlopen(req, timeout=60) as request:
194+
with urllib.request.urlopen(req, timeout=60) as response:
205195
import json
206-
assets = json.load(request)
196+
assets = json.load(response)
207197
# cache file for future offline work
208198
with open(filepath, 'w') as file:
209199
file.write(json.dumps(assets, indent=2))
@@ -235,7 +225,7 @@ def download_table_of_contents(context):
235225

236226
finally:
237227
try:
238-
request.close()
228+
response.close()
239229
except NameError:
240230
pass
241231

@@ -369,6 +359,10 @@ def stopped(self):
369359
def run(self):
370360
import urllib.request
371361
user_preferences = get_addon_preferences(bpy.context)
362+
LOL_HOST_URL = user_preferences.lol_host
363+
LOL_VERSION = user_preferences.lol_version
364+
LOL_HTTP_HOST = user_preferences.lol_http_host
365+
LOL_USERAGENT = user_preferences.lol_useragent
372366
tcom = self.tcom
373367

374368
filename = self.asset["url"]
@@ -379,21 +373,21 @@ def run(self):
379373
try:
380374
print("[LOL] Downloading:", urlstr)
381375
req = urllib.request.Request(
382-
urlstr, data=None, headers={'User-Agent': useragent}
376+
urlstr, headers={'User-Agent': LOL_USERAGENT, 'Host': LOL_HTTP_HOST}
383377
)
384-
with urllib.request.urlopen(req, timeout=60) as url_handle, \
378+
with urllib.request.urlopen(req, timeout=60) as response, \
385379
open(temp_zip_path, "wb") as file_handle:
386-
if 'Content-Length' in url_handle.headers:
387-
total_length = url_handle.headers.get('Content-Length')
380+
if 'Content-Length' in response.headers:
381+
total_length = response.headers.get('Content-Length')
388382
tcom.file_size = int(total_length)
389383
else:
390384
tcom.file_size = None
391385

392386
dl = 0
393-
data = url_handle.read(8192)
387+
data = response.read(8192)
394388
file_handle.write(data)
395389
while len(data) == 8192:
396-
data = url_handle.read(8192)
390+
data = response.read(8192)
397391
if tcom.file_size is not None:
398392
dl += len(data)
399393
tcom.downloaded = dl
@@ -407,7 +401,7 @@ def run(self):
407401

408402
file_handle.write(data)
409403
if self.stopped():
410-
url_handle.close()
404+
response.close()
411405
return
412406
print("[LOL] Download finished")
413407
import zipfile
@@ -420,6 +414,7 @@ def run(self):
420414
print(error)
421415
except urllib.error.URLError as error:
422416
print("[LOL] Could not download: " + filename)
417+
print(error)
423418

424419
finally:
425420
tcom.finished = True
@@ -711,11 +706,14 @@ def bg_load_previews(context, asset_type):
711706

712707

713708
def bg_download_thumbnails(context, download_queue):
714-
from requests import Session
715-
session = Session()
709+
import urllib.request
716710

717711
name = 'bl_ext.user_default.' + basename(dirname(dirname(dirname(__file__))))
718712
user_preferences = context.preferences.addons[name].preferences
713+
LOL_HOST_URL = user_preferences.lol_host
714+
LOL_VERSION = user_preferences.lol_version
715+
LOL_HTTP_HOST = user_preferences.lol_http_host
716+
LOL_USERAGENT = user_preferences.lol_useragent
719717

720718
while not download_queue.empty():
721719
# Stop download if Blender is closed
@@ -730,10 +728,13 @@ def bg_download_thumbnails(context, download_queue):
730728
tpath = join(user_preferences.global_dir, asset_type.lower(), 'preview', imagename)
731729

732730
print("[LOL] Downloading ", imagename)
733-
url = LOL_HOST_URL + "/"+ asset_type.lower() +"/preview/" + imagename
734-
with session.get(url) as resp, open(tpath, "wb") as file_handle:
735-
if resp.status_code == 200:
736-
file_handle.write(resp.content)
731+
urlstr = LOL_HOST_URL + "/"+ asset_type.lower() +"/preview/" + imagename
732+
req = urllib.request.Request(
733+
urlstr, headers={'User-Agent': LOL_USERAGENT, 'Host': LOL_HTTP_HOST}
734+
)
735+
with urllib.request.urlopen(req, timeout=60) as response, open(tpath, "wb") as file_handle:
736+
if response.getcode() == 200:
737+
file_handle.write(response.read())
737738

738739
from shutil import copyfile
739740
copyfile(tpath, tpath_full)
@@ -750,7 +751,7 @@ def bg_download_thumbnails(context, download_queue):
750751
asset['thumbnail'] = bpy.data.images.load(tpath)
751752
asset['thumbnail'].name = '.LOL_preview'
752753
else:
753-
print("[LOL] Download error ",resp.status_code, ": ", url)
754+
print("[LOL] Download error ", response.status_code, ": ", urlstr)
754755

755756
for threaddata in bg_threads:
756757
tag, bg_task = threaddata

0 commit comments

Comments
 (0)