1
0
mirror of https://github.com/obskyr/khinsider.git synced 2025-09-02 17:42:33 +02:00

Adapted searching to KHInsider's new HTML.

Also fixed a li'l bug (I changed a lazy-loaded property name without changing a place where I set its underlying property; whoops) which made searching absurdly slow.
This commit is contained in:
obskyr
2022-05-09 02:15:04 +02:00
parent 34c630a74c
commit b1683fbf28

View File

@@ -11,6 +11,7 @@ import os
import re import re
import sys import sys
from functools import wraps from functools import wraps
from itertools import chain
try: try:
from urllib.parse import unquote, urljoin, urlsplit from urllib.parse import unquote, urljoin, urlsplit
@@ -461,33 +462,54 @@ class SearchError(KhinsiderError):
def search(term): def search(term):
"""Return a list of Soundtrack objects for the search term `term`.""" """Return a tuple of two lists of Soundtrack objects for the search term
`term`. The first tuple contains album name results, and the second song
name results.
"""
r = requests.get(urljoin(BASE_URL, 'search'), params={'search': term}) r = requests.get(urljoin(BASE_URL, 'search'), params={'search': term})
path = urlsplit(r.url).path path = urlsplit(r.url).path
if path.split('/', 2)[1] == 'game-soundtracks': if path.split('/', 2)[1] == 'game-soundtracks':
return [Soundtrack(path.rsplit('/', 1)[-1])] return [Soundtrack(path.rsplit('/', 1)[-1])]
soup = toSoup(r) soup = toSoup(r)
try:
anchors = soup('p')[1]('a') tables = soup('table', class_='albumList')
except IndexError: if not tables:
raise SearchError(soup.find('p').get_text(strip=True)) raise SearchError(soup.find('p').get_text(strip=True))
soundtracks = [soundtracksInSearchTable(table) for table in tables]
if len(soundtracks) == 1:
if "song" in soup.find(id='pageContent').find('p').get_text():
soundtracks.insert(0, [])
else:
soundtracks.append([])
return soundtracks
def soundtracksInSearchTable(table):
anchors = (tr('td')[1].find('a') for tr in table('tr')[1:])
soundtrackParams = [(a['href'].split('/')[-1], a.get_text(strip=True)) for a in anchors] soundtrackParams = [(a['href'].split('/')[-1], a.get_text(strip=True)) for a in anchors]
soundtracks = [] soundtracks = []
for id, title in soundtrackParams: for id, name in soundtrackParams:
curSoundtrack = Soundtrack(id) curSoundtrack = Soundtrack(id)
curSoundtrack._lazy_title = title curSoundtrack._lazy_name = name
soundtracks.append(curSoundtrack) soundtracks.append(curSoundtrack)
return soundtracks return soundtracks
def printSearchResults(searchResults, file=sys.stdout): def printSearchResults(searchResults, file=sys.stdout):
padLen = max(len(x.id) for x in searchResults) padLen = max(len(x.id) for x in chain(*searchResults))
s = "" s = ""
for soundtrack in searchResults: hasPreviousList = False
s += "{} {}. {}\n".format(soundtrack.id, '.' * (padLen - len(soundtrack.id)), soundtrack.name) for heading, soundtracks in zip(("Album title results:", "Song name results:"), searchResults):
if soundtracks:
if hasPreviousList:
s += "\n"
s += heading + "\n"
for soundtrack in soundtracks:
s += "{} {}. {}\n".format(soundtrack.id, '.' * (padLen - len(soundtrack.id)), soundtrack.name)
hasPreviousList = True
unicodePrint(s, end="", file=file) unicodePrint(s, end="", file=file)
# --- And now for the execution. --- # --- And now for the execution. ---
@@ -597,7 +619,7 @@ if __name__ == '__main__':
else: else:
if searchResults: if searchResults:
print("Soundtracks found (to download, " print("Soundtracks found (to download, "
"run \"{} soundtrack-name\"):".format(SCRIPT_NAME)) "run \"{} soundtrack-name\")!\n".format(SCRIPT_NAME))
printSearchResults(searchResults) printSearchResults(searchResults)
else: else:
print("No soundtracks found.") print("No soundtracks found.")