Skip to content

Commit

Permalink
Merge pull request #98 from zurdi15/feature/scan_new_platform
Browse files Browse the repository at this point in the history
smar scan [platforms+games]
  • Loading branch information
zurdi15 authored Mar 31, 2023
2 parents b541134 + 9685415 commit 11c1c7e
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 38 deletions.
1 change: 1 addition & 0 deletions backend/src/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

# PATHS
LIBRARY_BASE_PATH: str = f"{pathlib.Path(__file__).parent.parent.parent.parent.resolve()}/library"
HIGH_PRIO_STRUCTURE_PATH: str = f"{LIBRARY_BASE_PATH}/roms"
ROMM_USER_CONFIG_PATH: str = f"{pathlib.Path(__file__).parent.parent.parent.parent.resolve()}/romm/config.yml"

# ROMM RESERVED FOLDERS
Expand Down
2 changes: 1 addition & 1 deletion backend/src/handler/igdb_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def get_rom_details(self, file_name: str, p_igdb_id: int, r_igdb_id_search: str)
except KeyError:
pass

else:
else: #TODO: improve API calls to make only one
search_term: str = unidecode.unidecode(file_name_no_tags)
if p_igdb_id:
try:
Expand Down
6 changes: 3 additions & 3 deletions backend/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async def platforms() -> dict:


@app.put("/scan")
async def scan(req: Request, overwrite: bool=False) -> dict:
async def scan(req: Request, full_scan: bool=False, overwrite: bool=False) -> dict:
"""Scan platforms and roms and write them in database."""

log.info("complete scaning...")
Expand All @@ -84,10 +84,10 @@ async def scan(req: Request, overwrite: bool=False) -> dict:
platforms: list[str] = data['platforms'] if data['platforms'] else fs.get_platforms()
for p_slug in platforms:
platform: Platform = fastapi.scan_platform(p_slug)
roms: list[dict] = fs.get_roms(p_slug)
roms: list[dict] = fs.get_roms(p_slug, full_scan)
for rom in roms:
fastapi.scan_rom(platform, rom)
dbh.purge_roms(p_slug, roms)
dbh.purge_roms(p_slug, fs.get_roms(p_slug, True))
dbh.purge_platforms(fs.get_platforms())
return {'msg': 'success'}

Expand Down
2 changes: 1 addition & 1 deletion backend/src/utils/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def scan_platform(p_slug: str) -> Platform:
platform_attrs: dict = igdbh.get_platform_details(p_slug)
platform_attrs['slug'] = p_slug
platform_attrs['logo_path'] = ''
platform_attrs['n_roms'] = fs.get_roms(p_slug, only_amount=True)
platform_attrs['n_roms'] = fs.get_roms(p_slug, True, only_amount=True)
log.info(f"Platform n_roms: {platform_attrs['n_roms']}")
platform = Platform(**platform_attrs)
dbh.add_platform(platform)
Expand Down
49 changes: 25 additions & 24 deletions backend/src/utils/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import requests
from fastapi import HTTPException

from config import user_config, LIBRARY_BASE_PATH, RESERVED_FOLDERS, DEFAULT_URL_COVER_L, DEFAULT_PATH_COVER_L, DEFAULT_URL_COVER_S, DEFAULT_PATH_COVER_S
from config import user_config, LIBRARY_BASE_PATH, HIGH_PRIO_STRUCTURE_PATH, RESERVED_FOLDERS, DEFAULT_URL_COVER_L, DEFAULT_PATH_COVER_L, DEFAULT_URL_COVER_S, DEFAULT_PATH_COVER_S
from models.platform import Platform
from models.rom import Rom
from handler import dbh
from logger.logger import log


Expand Down Expand Up @@ -52,12 +55,11 @@ def get_platforms() -> list[str]:

# ========= Roms utils =========
def _check_folder_structure(p_slug) -> tuple:
if os.path.exists(f"{LIBRARY_BASE_PATH}/roms"):
roms_path: str = f"{LIBRARY_BASE_PATH}/roms/{p_slug}"
roms_files = list(os.walk(f"{LIBRARY_BASE_PATH}/roms/{p_slug}"))[0][2]
else:
roms_path: str = f"{LIBRARY_BASE_PATH}/{p_slug}/roms"
roms_files = list(os.walk(f"{LIBRARY_BASE_PATH}/{p_slug}/roms"))[0][2]
roms_path: str = f"{HIGH_PRIO_STRUCTURE_PATH}/{p_slug}" if os.path.exists(HIGH_PRIO_STRUCTURE_PATH) else f"{LIBRARY_BASE_PATH}/{p_slug}/roms"
try:
roms_files = list(os.walk(roms_path))[0][2]
except IndexError:
roms_files = []
return roms_path, roms_files


Expand Down Expand Up @@ -91,30 +93,29 @@ def parse_tags(file_name: str) -> tuple:
return reg, rev, other_tags


def get_roms(p_slug: str, only_amount: bool = False) -> list[dict]:
def get_roms(p_slug: str, full_scan: bool, only_amount: bool = False) -> list[dict]:
"""Gets all filesystem roms for a platform
Args:
p_slug: short name of the platform
only_amount: flag to return only amount of roms instead of all info
Returns: list with all the filesystem roms for a platform found in the LIBRARY_BASE_PATH. Just the amount of them if only_amount=True
"""
try:
roms: list[dict] = []
roms_path, roms_files = _check_folder_structure(p_slug)
roms_files = _exclude_files(roms_files)

if only_amount: return len(roms_files)

for rom in roms_files:
file_size: str = str(round(os.stat(f"{roms_path}/{rom}").st_size / (1024 * 1024), 2))
file_extension: str = rom.split('.')[-1] if '.' in rom else ""
reg, rev, other_tags = parse_tags(rom)
roms.append({'file_name': rom, 'file_path': roms_path, 'file_size': file_size, 'file_extension': file_extension,
'region': reg, 'revision': rev, 'tags': other_tags})
log.info(f"Roms found for {p_slug}: {roms}")
except IndexError:
log.warning(f"Roms not found for {p_slug}")
roms: list[dict] = []
roms_path, roms_files = _check_folder_structure(p_slug)
roms_files = _exclude_files(roms_files)

if only_amount: return len(roms_files)

excluded_roms: list[str] = [rom.file_name for rom in dbh.get_roms(p_slug)]
for rom in roms_files:
if rom in excluded_roms and not full_scan: continue
file_size: str = str(round(os.stat(f"{roms_path}/{rom}").st_size / (1024 * 1024), 2))
file_extension: str = rom.split('.')[-1] if '.' in rom else ""
reg, rev, other_tags = parse_tags(rom)
roms.append({'file_name': rom, 'file_path': roms_path, 'file_size': file_size, 'file_extension': file_extension,
'region': reg, 'revision': rev, 'tags': other_tags})
log.info(f"Roms found for {p_slug}: {roms}")
if only_amount: return 0
return roms

Expand Down
21 changes: 14 additions & 7 deletions frontend/src/components/Navigation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const currentPlatform = ref(JSON.parse(localStorage.getItem('currentPlatform'))
const platformsToScan = ref([])
const scanning = ref(false)
const scanOverwrite = ref(false)
const fullScan = ref(false)
const gettingRomsFlag = ref(false)
const filter = ref('')
const drawer = ref(null)
Expand All @@ -33,7 +34,7 @@ async function scan() {
toRaw(platformsToScan)._rawValue.forEach(p => {platforms.push(toRaw(p.slug))})
console.log(platforms)
await axios.put('/api/scan?overwrite='+scanOverwrite.value,{
await axios.put('/api/scan?overwrite='+scanOverwrite.value+'&full_scan='+fullScan.value,{
platforms: platforms
}).then((response) => {
console.log("scan completed")
Expand Down Expand Up @@ -103,12 +104,18 @@ getPlatforms()
<v-list>
<!-- Settings drawer - scan button -->
<v-select label="Platforms" item-title="name" v-model="platformsToScan" :items="platforms" class="pl-5 pr-5 mt-2" density="comfortable" variant="outlined" multiple return-object clearable/>
<v-list-item class="d-flex align-center justify-center mb-2 pt-0">
<v-btn title="scan" @click="scan()" :disabled="scanning" prepend-icon="mdi-magnify-scan" color="secondary" rounded="0" inset>
<p v-if="!scanning">Scan</p>
<p v-if="scanning">Scanning</p>
<v-progress-circular v-show="scanning" class="ml-2" :width="2" :size="20" indeterminate/>
</v-btn>
<v-list-item class="pa-0">
<v-row class="align-center">
<v-col class="d-flex justify-center">
<v-btn title="scan" @click="scan()" :disabled="scanning" prepend-icon="mdi-magnify-scan" class="ml-7" color="secondary" rounded="0" inset>
<p v-if="!scanning">Scan</p>
<v-progress-circular v-show="scanning" class="ml-2" :width="2" :size="20" indeterminate/>
</v-btn>
</v-col>
<v-col class="mr-4">
<v-checkbox v-model="fullScan" label="Full scan" hide-details="true"/>
</v-col>
</v-row>
</v-list-item>
</v-list>
<!-- Settings drawer - theme toggle -->
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/RomDetails.vue
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ async function deleteRom() {
</v-row>
</v-card-text>
<v-card-actions v-show="!searching">
<v-checkbox v-model="renameAsIGDB" label="Rename file" class="pl-3" hide-details="true"></v-checkbox>
<v-checkbox v-model="renameAsIGDB" label="Rename file" class="pl-3" hide-details="true"/>
</v-card-actions>
</v-card>
</v-dialog>
Expand Down Expand Up @@ -230,7 +230,7 @@ async function deleteRom() {
<v-btn @click="dialogDeleteRom=false" variant="tonal">Cancel</v-btn>
</v-card-actions>
<div class="pl-8">
<v-checkbox v-model="deleteFromFs" label="Delete from filesystem" hide-details="true"></v-checkbox>
<v-checkbox v-model="deleteFromFs" label="Delete from filesystem" hide-details="true"/>
</div>
</v-card>
</v-dialog>
Expand Down

0 comments on commit 11c1c7e

Please sign in to comment.