-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter_books.py
60 lines (50 loc) · 1.86 KB
/
filter_books.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import os
import json
import math
BOOKS_FILE = "books_raw.jl"
def load_books():
books = []
if os.path.exists(BOOKS_FILE):
with open(BOOKS_FILE, "r", encoding="utf-8") as file:
for line in file:
books.append(json.loads(line))
return books
def filter_books(filters):
selected_genres = filters.get("selected_genres", [])
min_ratings = filters.get("min_ratings", 0)
current_page = filters.get("current_page", 1)
books = load_books()
total_books_count = len(books)
all_genres = sorted({genre for book in books for genre in book["Genres"]})
genre_counts = {}
for genre in all_genres:
genre_counts[genre] = 0
filtered_books = []
for book in books:
for genre in book["Genres"]:
genre_counts[genre] += 1
num_ratings = int(book["Num Ratings"].replace(",", ""))
if selected_genres and not all(genre in book["Genres"] for genre in selected_genres):
continue
if num_ratings < min_ratings:
continue
filtered_books.append(book)
filtered_books.sort(key=lambda b: float(b["Avg Rating"]), reverse=True)
filtered_books_count = len(filtered_books)
results_per_page = 20
start = (current_page - 1) * results_per_page
end = min(start + results_per_page, filtered_books_count)
paginated_books = filtered_books[start:end]
total_pages = math.ceil(filtered_books_count/results_per_page)
all_genres = sorted(genre for genre in genre_counts)
return {
"books": paginated_books,
"total_books_count": total_books_count,
"filtered_books_count": filtered_books_count,
"all_genres": all_genres,
"genre_counts": genre_counts,
"selected_genres": selected_genres,
"min_ratings": min_ratings,
"current_page": current_page,
"total_pages": total_pages,
}