Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pagination #49

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions freetar/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from flask import Flask, render_template, request
from flask_minify import Minify

from freetar.ug import ug_search, ug_tab
from freetar.ug import Search, ug_tab
from freetar.utils import get_version, FreetarError


Expand All @@ -28,14 +28,20 @@ def index():
@app.route("/search")
def search():
search_term = request.args.get("search_term")
page = request.args.get("page") or 1
if search_term:
search_results = ug_search(search_term)
search = Search(search_term, page)
search_results = search.results
total_pages = search.total_pages
current_page = search.current_page
else:
search_results = []
return render_template("index.html",
search_term=search_term,
title=f"Freetar - Search: {search_term}",
search_results=search_results,)
search_results=search_results,
total_pages=total_pages,
current_page=current_page)


@app.route("/tab/<artist>/<song>")
Expand Down
17 changes: 17 additions & 0 deletions freetar/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@
</table>
</div>

<div>
page:
<table>
<tbody>
<tr>
{% for page in range(total_pages) %}
<td><a
{% if page + 1 != current_page %}
href="/search?search_term={{ search_term }}&page={{ page + 1 }}"
{% endif %}
>{{ page + 1 }}</a></td>
{% endfor %}
</tr>
</tbody>
</table>
</div>

{% if favs %}
<details><summary>Advanced</summary>
<strong class="d-block">Export favorties</strong>
Expand Down
43 changes: 25 additions & 18 deletions freetar/ug.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __repr__(self):


@dataclass
class SongDetail():
class SongDetail:
tab: str
artist_name: str
song_name: str
Expand Down Expand Up @@ -105,31 +105,38 @@ def parse_chord(self, chord):
bass = '/<span class="chord-bass">%s</span>' % chord.group('bass')[1:]
return '<span class="chord fw-bold">%s</span>' % (root + quality + bass)


def ug_search(value: str):
try:
resp = requests.get(f"https://www.ultimate-guitar.com/search.php?search_type=title&value={quote(value)}",
headers={'User-Agent': USER_AGENT},
proxies=PROXIES)
resp.raise_for_status()
bs = BeautifulSoup(resp.text, 'html.parser')
# data can be None
data = bs.find("div", {"class": "js-store"})
# KeyError
data = data.attrs['data-content']
data = json.loads(data)
@dataclass
class Search:
results: dict
total_pages: int
current_page: int

def __init__(self, value: str, page: int):
try:
resp = requests.get(f"https://www.ultimate-guitar.com/search.php?page={page}&search_type=title&value={quote(value)}",
headers={'User-Agent': USER_AGENT},
proxies=PROXIES)
resp.raise_for_status()
bs = BeautifulSoup(resp.text, 'html.parser') # data can be None
data = bs.find("div", {"class": "js-store"}) # KeyError
data = json.loads(data.attrs['data-content'])
self.results = self.get_results(data)
self.total_pages = data['store']['page']['data']['pagination']['total']
self.current_page = data['store']['page']['data']['pagination']['current']
#print(json.dumps(data, indent=4))

except (KeyError, ValueError, AttributeError, requests.exceptions.RequestException) as e:
raise FreetarError(f"Could not search for chords: {e}") from e

def get_results(self, data: object):
results = data['store']['page']['data']['results']
ug_results = []
for result in results:
_type = result.get("type")
if _type and _type != "Pro":
s = SearchResult(result)
ug_results.append(s)
#print(s)
#print(json.dumps(data, indent=4))
return ug_results
except (KeyError, ValueError, AttributeError, requests.exceptions.RequestException) as e:
raise FreetarError(f"Could not search for chords: {e}") from e


def get_chords(s: SongDetail) -> SongDetail:
Expand Down