keys2db.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. any_dats = [x for x in cwd.glob('*.dat')]
  39. if not any_dats:
  40. print('Error: No .dat file. Place the .dat file in the tools/ folder')
  41. sys.exit()
  42. datfile = any_dats[0]
  43. warnings = 0
  44. with open(datfile, 'r') as infile:
  45. soup = bs4.BeautifulSoup(infile.read(), features='html5lib')
  46. for game in soup.find_all('game'):
  47. name = game.find('description').text.strip()
  48. attrs = game.find('rom').attrs
  49. entry = [name, attrs['size'], attrs['crc'], attrs['md5'], attrs['sha1']]
  50. try:
  51. with open(cwd / ('keys/' + name + '.key'), 'rb') as keyfile:
  52. entry.append(keyfile.read())
  53. except FileNotFoundError:
  54. warnings += 1
  55. c.execute('INSERT INTO games (name, size, crc32, md5, sha1) VALUES (?, ?, ?, ?, ?)', entry)
  56. continue
  57. c.execute('INSERT INTO games VALUES (?, ?, ?, ?, ?, ?)', entry)
  58. db.commit()
  59. db.close()
  60. shutil.copyfile(db_path, ((cwd.parent / 'libray') / 'data/') / db_path.name)
  61. print('Warning: no keyfiles for %s titles' % str(warnings))