4040from threading import _MainThread , Thread , Lock
4141from ...handlers .lol .timer import timer_update
4242from ...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
5244download_threads = []
5345bg_threads = []
@@ -78,6 +70,10 @@ def load_local_TOC(context, asset_type):
7870def 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
713708def 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