123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- import bs4
- import sys
- import shutil
- import sqlite3
- import pathlib
- if __name__ == '__main__':
- db_path = pathlib.Path('keys.db')
- if db_path.exists():
- db_path.unlink()
- db = sqlite3.connect(db_path)
- c = db.cursor()
- c.execute('CREATE TABLE games (name TEXT, size TEXT, crc32 TEXT, md5 TEXT, sha1 TEXT, key BLOB)')
- db.commit()
- cwd = pathlib.Path(__file__).resolve().parent
- keys_path = cwd / 'keys'
- if not keys_path.exists():
- print('Error: No keys/ folder. Place the .key files in a tools/keys/ folder')
- sys.exit()
- any_dats = [x for x in cwd.glob('*.dat')]
- if not any_dats:
- print('Error: No .dat file. Place the .dat file in the tools/ folder')
- sys.exit()
- datfile = any_dats[0]
- warnings = 0
- with open(datfile, 'r') as infile:
- soup = bs4.BeautifulSoup(infile.read(), features='html5lib')
- for game in soup.find_all('game'):
- name = game.find('description').text.strip()
- attrs = game.find('rom').attrs
- entry = [name, attrs['size'], attrs['crc'], attrs['md5'], attrs['sha1']]
- try:
- with open(cwd / ('keys/' + name + '.key'), 'rb') as keyfile:
- entry.append(keyfile.read())
- except FileNotFoundError:
- warnings += 1
- c.execute('INSERT INTO games (name, size, crc32, md5, sha1) VALUES (?, ?, ?, ?, ?)', entry)
- continue
- c.execute('INSERT INTO games VALUES (?, ?, ?, ?, ?, ?)', entry)
- db.commit()
- db.close()
- data_path = (cwd.parent / 'libray') / 'data/'
- if not data_path.exists():
- data_path.mkdir()
- shutil.copyfile(db_path, ((cwd.parent / 'libray') / 'data/') / db_path.name)
- print('Warning: no keyfiles for %s titles' % str(warnings))
|