fork download
  1. def non_recursive(scene_file):
  2. with open(scene_file, 'r', encoding='utf-8') as file:
  3. data = json.load(file)
  4.  
  5. new_scene_data = {}
  6. for data_k,data_v in data.items():
  7. if data_k == 'atoms':
  8. new_atoms = []
  9. atoms = data_v
  10. for atom in atoms:
  11. temp_dict = {}
  12. for atom_k, atom_v in atom.items():
  13. if atom_k == 'storables':
  14. storables = atom_v
  15. new_storables = []
  16. for storable in storables:
  17. subtemp_dict = {}
  18. for storable_k, storable_v in storable.items():
  19. if isinstance(storable_v, str):
  20. if ':/' in storable_v:
  21. new_value = seeker_and_downloader(storable_v)
  22. subtemp_dict[storable_k] = new_value
  23. else:
  24. subtemp_dict[storable_k] = storable_v
  25. elif isinstance(storable_v, list):
  26. sublist = []
  27. for item in storable_v:
  28.  
  29. try:
  30. item_id = item['id']
  31. except KeyError:
  32. try:
  33. item_id = item['uid']
  34. except KeyError:
  35. item_id = item['url']
  36. new_plugin = {}
  37. for plugin_k, plugin_v in item.items():
  38. if ':/' in plugin_v:
  39. new_value = seeker_and_downloader(item_id)
  40. new_plugin[plugin_k] = plugin_v
  41. else:
  42. new_plugin[plugin_k] = plugin_v
  43.  
  44. sublist.append(new_plugin)
  45. subtemp_dict[storable_k] = sublist
  46. elif isinstance(storable_v, dict):
  47. subdict = {}
  48. for sub_k,sub_v in storable_v.items():
  49.  
  50. if isinstance(sub_v, dict):
  51. subsubdict = {}
  52. for k,v in sub_v.items():
  53. if ':/' in v:
  54. new_v = seeker_and_downloader(v)
  55. subsubdict[k] = new_v
  56. else:
  57. subsubdict[k] = v
  58. subdict[sub_k] = subsubdict
  59.  
  60. elif isinstance(sub_v, str):
  61. if ':/' in sub_v:
  62. new_value = seeker_and_downloader(sub_v)
  63. subdict[sub_k] = new_value
  64. else:
  65. subdict[sub_k] = sub_v
  66. elif isinstance(sub_v, list):
  67. # Тут обычно ничего интересного нет, но мало ли...
  68. subdict[sub_k] = sub_v
  69. else:
  70. subdict[sub_k] = sub_v
  71. subtemp_dict[storable_k] = subdict
  72. else:
  73. subtemp_dict[storable_k] = storable_v
  74. new_storables.append(subtemp_dict)
  75. temp_dict[atom_k] = new_storables
  76. else:
  77. temp_dict[atom_k] = atom_v
  78. new_atoms.append(temp_dict)
  79. new_scene_data[data_k] = new_atoms
  80. else:
  81. new_scene_data[data_k] = data_v
  82.  
  83.  
  84. #создаем исправленный под локальный путь апперанс-файл
  85.  
  86. new_scene_file = f'new_{scene_file}'
  87. with open(new_scene_file, 'w', encoding='utf-8') as file:
  88. json.dump(new_scene_data, file, indent=4)
  89.  
  90. #создаем картинку к новому апперансу
  91. scene_image_file = scene_file.replace('.json', '.jpg')
  92. scene_image_file_name = scene_image_file.split('.')[0]
  93. scene_image_file_extension = scene_image_file.split('.')[-1]
  94. new_scene_image_file = f'new_{scene_image_file_name}.{scene_image_file_extension}'
  95. shutil.copy(scene_image_file, new_scene_image_file)
Success #stdin #stdout 0.03s 9612KB
stdin
Standard input is empty
stdout
Standard output is empty