Skip to content

Commit

Permalink
Add support to create SoftwareManual from PDF
Browse files Browse the repository at this point in the history
Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
  • Loading branch information
sahlberg committed Oct 8, 2023
1 parent 419ae5d commit 661a8f3
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 44 deletions.
1 change: 1 addition & 0 deletions .github/workflows/py.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ jobs:
run: |
pip install pillow
pip install pycryptodome
pip install PyPDF2
pip install requests
pip install pycdlib
pip install ecdsa
Expand Down
2 changes: 2 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ git clone https://github.com/NRGDEAD/Cue2cu2.git
git clone https://github.com/putnam/binmerge.git
git clone https://github.com/dcherednik/atracdenc.git
pip3 install git+https://github.com/nficano/pytube
pip3 install PyPDF2
pip3 install requests
pip3 install pycdlib (or pip3 install pycdio)
pip3 install ecdsa
Expand Down Expand Up @@ -637,6 +638,7 @@ pacman -S mingw-w64-x86_64-python-pip
pacman -S mingw-w64-x86_64-python-pillow
pip3 uninstall pillow
pip3 install pillow
pip3 install PyPDF2
pip3 install requests
pip3 install pycdlib
pip3 install pycryptodome
Expand Down
1 change: 1 addition & 0 deletions gamedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2347,6 +2347,7 @@
'pic0': 'https://images.launchbox-app.com/27c885e5-b210-41d4-b3f9-4c3bc0c7b282.png',
'pic1': 'https://images.launchbox-app.com/8181cf02-1af0-4a99-8f75-7f5e44acf1f4.jpg',
'snd0': 'https://www.youtube.com/watch?v=STJm1RoLYLo&list=PL4385054D3D733B93&index=1',
'manual': 'https://www.gamesdatabase.org/Media/SYSTEM/Sony_Playstation//Manual/formated/Air_Combat_-_1995_-_Namco_Limited.pdf',
},
'SLUS01467': {
'url': "games/U/A/SLUS-01467.html",
Expand Down
118 changes: 74 additions & 44 deletions pop-fe.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
import rarfile
except:
print('rarfile is not installed.\nYou should install requests by running:\npip3 install rarfile')
try:
import PyPDF2
except:
print('PyPDF2 is not installed.\nYou should install requests by running:\npip3 install PyPDF2')
try:
import requests
except:
Expand Down Expand Up @@ -1546,7 +1550,14 @@ def create_ps2(dest, disc_ids, game_title, icon0, pic1, cue_files, cu2_files, im
def get_disc_ids(cue_files, subdir='./'):
disc_ids = []
for idx in range(len(cue_files)):
print('Convert CUE to a normal style ISO', cue_files[idx]) if verbose else None
try:
with open(create_path(cue_files[idx], 'GAME_ID'), 'r') as d:
disc_ids.append(d.read())
print('Read disc id from', create_path(cue_files[idx], 'GAME_ID'))
continue
except:
True
print('Convert CUE to a normal style ISO to read the disc id', cue_files[idx]) if verbose else None
bc = bchunk()
bc.verbose = args.v
bc.open(cue_files[idx])
Expand Down Expand Up @@ -1606,6 +1617,13 @@ def apply_ppf(img, disc_id, magic_word, auto_libcrypt):

def install_deps():
print(os.name)
# PyPDF2
try:
import PyPDF2
print('PyPDF2 is already installed')
except:
print('Installing python PyPDF2')
subprocess.call(['pip', 'install', 'PyPDF2'])
# requests
try:
import requests
Expand Down Expand Up @@ -1857,7 +1875,7 @@ def create_manual(source, gameid, subdir='./pop-fe-work/'):

print('Create manual', source)
files = []

if source[:8] != 'https://':
with open(source, 'rb') as f:
buf = f.read(4)
Expand All @@ -1873,45 +1891,66 @@ def create_manual(source, gameid, subdir='./pop-fe-work/'):

print('Create DOCUMENT.DAT from', source)
if source[:8] == 'https://':
print('Download manual from', source)
try:
tmpfile = subdir + '/DOCUMENT-' + source.split('/')[-1]
temp_files.append(tmpfile)
subprocess.run(['wget', source, '-O', tmpfile], timeout=240, check=True)
print('Downloaded manual as', tmpfile)
source = tmpfile
except:
print('Failed to download manual from', source)
return None
print('Download manual from', source)
try:
tmpfile = subdir + '/DOCUMENT-' + source.split('/')[-1]
temp_files.append(tmpfile)
subprocess.run(['wget', source, '-O', tmpfile], timeout=240, check=True)
print('Downloaded manual as', tmpfile)
source = tmpfile
except:
print('Failed to download manual from', source)
return None
if source[-4:] == '.zip':
print('Unzip manual', source, 'from ZIP')
subdir = subdir + '/DOCUMENT-tmp'
os.mkdir(subdir)
temp_files.append(subdir)
print('Unzip manual', source, 'from ZIP')
subdir = subdir + '/DOCUMENT-tmp'
os.mkdir(subdir)
temp_files.append(subdir)

z = zipfile.ZipFile(source)
for f in z.namelist():
f = z.extract(f, path=subdir)
z = zipfile.ZipFile(source)
for f in z.namelist():
f = z.extract(f, path=subdir)
temp_files.append(f)
files.append(f)
source = subdir
if source[-4:] == '.cbr':
print('Unzip manual', source, 'from CBR')
subdir = subdir + '/DOCUMENT-tmp'
os.mkdir(subdir)
temp_files.append(subdir)

try:
r = rarfile.RarFile(source)
for f in r.namelist():
f = r.extract(f, path=subdir)
temp_files.append(f)
files.append(f)
source = subdir
if source[-4:] == '.cbr':
print('Unzip manual', source, 'from CBR')
subdir = subdir + '/DOCUMENT-tmp'
os.mkdir(subdir)
temp_files.append(subdir)
except:
print('Failed to create SOFTWARE MANUAL. Could not extract images from CBR file. Make sure that UNRAR is installed.')
return None

try:
r = rarfile.RarFile(source)
for f in r.namelist():
f = r.extract(f, path=subdir)
if source[-4:] == '.pdf':
print('Extract manual', source, 'from PDF')
subdir = subdir + '/DOCUMENT-tmp'
os.mkdir(subdir)
temp_files.append(subdir)
try:
idx = 0
r = PyPDF2.PdfReader(source)
for p in r.pages:
for i in p.images:
f = subdir + '/' + f"{idx:04d}" + '.img'
with open(f, "wb") as fp:
fp.write(i.data)
idx = idx + 1
temp_files.append(f)
files.append(f)
source = subdir
except:
print('Failed to create SOFTWARE MANUAL. Could not extract images from CBR file. Make sure that UNRAR is installed.')
return None
source = subdir
except:
print('Failed to parse PDF.')
return None

if not os.path.isdir(source):
print('Can not create manual.', source, 'is not a directory')
return None
Expand Down Expand Up @@ -2377,19 +2416,10 @@ def force_ntsc_config(ps3config):
raise Exception('Must specify --game_id when using --psp-install-memory-card')
install_psp_mc(args.psp_dir, args.game_id[0], mem_cards)
quit()

_gids = None

if args.game_id:
_gids = args.game_id
if not _gids:
try:
with open(create_path(args.files[0], 'GAME_ID'), 'r') as d:
_gids = re.sub(r'[^A-Z0-9\,]+', '', d.read()).split(',')
except:
True

if _gids:
# override the disc_ids with the content of 'GAME_ID' or --game_id
# override the disc_ids with the content of --game_id
for idx in range(len(_gids)):
if idx < len(disc_ids):
disc_ids[idx] = _gids[idx]
Expand Down

0 comments on commit 661a8f3

Please sign in to comment.