You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.6 KiB

  1. from io import BytesIO
  2. from pathlib import Path
  3. from zipfile import ZipFile
  4. FS_ROOT = '/srv/www/atr0phy.net/acks/quests'
  5. URL_ROOT = 'https://atr0phy.net/acks/quests'
  6. ASSET_EXTENSIONS = ('png', 'jpg', 'jpeg', 'dungeondraft_map', 'wonderdraft_map', 'wxx')
  7. def load_quests():
  8. quest_list = {}
  9. fs = Path(FS_ROOT)
  10. for e in fs.iterdir():
  11. if not e.is_dir():
  12. continue
  13. # Each directory correlates to a level group
  14. quest_level_url_name = e.name.replace('Level', '').replace('-', '').replace(' ', '')
  15. # Gather quests under each level
  16. quest_list[quest_level_url_name] = load_quest_level_directory(e)
  17. return quest_list
  18. def load_quest_level_directory(qdir):
  19. this_level = []
  20. for e in qdir.iterdir():
  21. if not e.is_dir():
  22. continue
  23. # Each directory is a quest for this level
  24. this_level.append(e.name)
  25. return this_level
  26. def get_quest_details(level, quest_name):
  27. level_range = str(level)
  28. folder_name = "Level {0} - {1}".format(level_range[0], level_range[1])
  29. quest_path = "{}/{}/{}".format(FS_ROOT, folder_name, quest_name)
  30. fs = Path(quest_path)
  31. if not fs.exists() or not fs.is_dir():
  32. print('EXITING {}'.format(fs))
  33. return None
  34. quest = {}
  35. quest['name'] = fs.name
  36. quest['download'] = (level, quest_name)
  37. quest['assets'] = []
  38. def urlify(path):
  39. return {'display': path.name, 'url': path.as_posix().replace(FS_ROOT, URL_ROOT)}
  40. def read_coordinates(path):
  41. return path.read_text()
  42. for e in fs.iterdir():
  43. if e.name.endswith(ASSET_EXTENSIONS):
  44. quest['assets'].append(urlify(e))
  45. elif e.name == 'info.html':
  46. quest['info'] = urlify(e)
  47. elif e.name == 'tsv.txt':
  48. quest['tsv'] = urlify(e)
  49. elif e.name == 'coordinates.txt':
  50. quest['coords'] = read_coordinates(e)
  51. print("Quest: {}".format(quest))
  52. return quest
  53. def get_quest_archive(level, quest_name):
  54. level_range = str(level)
  55. folder_name = "Level {0} - {1}".format(level_range[0], level_range[1])
  56. quest_path = "{}/{}/{}".format(FS_ROOT, folder_name, quest_name)
  57. fs = Path(quest_path)
  58. if not fs.exists() or not fs.is_dir():
  59. print('EXITING {}'.format(fs))
  60. return None
  61. file_path_list = []
  62. for e in fs.iterdir():
  63. file_path_list.append((e.as_posix(), e.name))
  64. archive_mem = BytesIO()
  65. with ZipFile(archive_mem, 'w') as archive:
  66. for f in file_path_list:
  67. archive.write(f[0], f[1])
  68. archive_mem.seek(0)
  69. return archive_mem