keys2db.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/env python3
  2. # -*- coding: utf8 -*-
  3. # libray - Libre Blu-Ray PS3 ISO Tool
  4. # Copyright © 2018 - 2021 Nichlas Severinsen
  5. #
  6. # This file is part of libray.
  7. #
  8. # libray is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # libray is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with libray. If not, see <https://www.gnu.org/licenses/>.
  20. # This script transforms Datfile.dat and keys/*.key keyfiles into a sqlite3 keys.db
  21. # Keys.db is then moved to libray/data/keys.db and packaged with libray in setup.py.
  22. # Libray checks if this file is bundled with it and checks if it has a key for the .iso using a crc32 of it.
  23. # TODO: In theory we could add the game-serials (BLUS-0000) and check that first.
  24. import bs4
  25. import sys
  26. import shutil
  27. import sqlite3
  28. import pathlib
  29. if __name__ == '__main__':
  30. db_path = pathlib.Path('keys.db')
  31. if db_path.exists():
  32. db_path.unlink()
  33. db = sqlite3.connect(db_path)
  34. c = db.cursor()
  35. c.execute('CREATE TABLE games (name TEXT, size TEXT, crc32 TEXT, md5 TEXT, sha1 TEXT, key BLOB)')
  36. db.commit()
  37. cwd = pathlib.Path(__file__).resolve().parent
  38. keys_path = cwd / 'keys'
  39. if not keys_path.exists():
  40. print('Error: No keys/ folder. Place the .key files in a tools/keys/ folder')
  41. sys.exit()
  42. any_dats = [x for x in cwd.glob('*.dat')]
  43. if not any_dats:
  44. print('Error: No .dat file. Place the .dat file in the tools/ folder')
  45. sys.exit()
  46. datfile = any_dats[0]
  47. warnings = 0
  48. with open(datfile, 'r') as infile:
  49. soup = bs4.BeautifulSoup(infile.read(), features='html5lib')
  50. for game in soup.find_all('game'):
  51. name = game.find('description').text.strip()
  52. attrs = game.find('rom').attrs
  53. entry = [name, attrs['size'], attrs['crc'], attrs['md5'], attrs['sha1']]
  54. try:
  55. with open(cwd / ('keys/' + name + '.key'), 'rb') as keyfile:
  56. entry.append(keyfile.read())
  57. except FileNotFoundError:
  58. warnings += 1
  59. c.execute('INSERT INTO games (name, size, crc32, md5, sha1) VALUES (?, ?, ?, ?, ?)', entry)
  60. continue
  61. c.execute('INSERT INTO games VALUES (?, ?, ?, ?, ?, ?)', entry)
  62. db.commit()
  63. db.close()
  64. data_path = (cwd.parent / 'libray') / 'data/'
  65. if not data_path.exists():
  66. data_path.mkdir()
  67. shutil.copyfile(db_path, ((cwd.parent / 'libray') / 'data/') / db_path.name)
  68. print('Warning: no keyfiles for %s titles' % str(warnings))