Coverage for mindsdb / integrations / handlers / google_books_handler / google_books_tables.py: 0%
91 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-21 00:36 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-21 00:36 +0000
1import pandas as pd
2from mindsdb_sql_parser import ast
3from pandas import DataFrame
5from mindsdb.integrations.libs.api_handler import APITable
6from mindsdb.integrations.utilities.sql_utils import extract_comparison_conditions
9class VolumesTable(APITable):
10 """
11 A class for handling the volumes table.
12 """
14 def select(self, query: ast.Select) -> DataFrame:
15 """
16 Gets all info about the wanted contents of a bookshelf.
18 Args:
19 query (ast.Select): SQL query to parse.
21 Returns:
22 Response: Response object containing the results.
23 """
25 # Parse the query to get the conditions.
26 conditions = extract_comparison_conditions(query.where)
27 # Get the parameters for the request.
28 params = {}
29 for op, arg1, arg2 in conditions:
30 if op != '=':
31 raise NotImplementedError
32 if arg1 == 'q' or arg1 == 'download' or arg1 == 'langRestrict' \
33 or arg1 == 'printType'\
34 or arg1 == 'source' or arg1 == 'partner' \
35 or arg1 == 'showPreorders' or arg1 == 'startIndex':
36 params[arg1] = arg2
37 elif arg1 == 'filter':
38 if arg2 not in ['ebooks', 'free-ebooks', 'full', 'paid-ebooks', 'partial']:
39 raise NotImplementedError
40 params[arg1] = arg2
41 elif arg1 == 'libraryRestrict':
42 if arg2 not in ['my-library', 'no-restrictions']:
43 raise NotImplementedError
44 params[arg1] = arg2
45 elif arg1 == 'printType':
46 if arg2 not in ['all', 'books', 'magazines']:
47 raise NotImplementedError
48 params[arg1] = arg2
49 elif arg1 == 'projection':
50 if arg2 not in ['lite', 'full']:
51 raise NotImplementedError
52 params[arg1] = arg2
53 else:
54 raise NotImplementedError
56 # Get the order by from the query.
57 if query.order_by is not None:
58 if query.order_by[0].value == 'newest':
59 params['orderBy'] = 'newest'
60 elif query.order_by[0].value == 'relevance':
61 params['orderBy'] = 'relevance'
62 else:
63 raise NotImplementedError
65 if query.limit is not None:
66 params['maxResults'] = query.limit.value
68 # Get the volumes from the Google Books API.
69 bookshelves = self.handler.\
70 call_application_api(method_name='get_volumes', params=params)
72 selected_columns = []
73 for target in query.targets:
74 if isinstance(target, ast.Star):
75 selected_columns = self.get_columns()
76 break
77 elif isinstance(target, ast.Identifier):
78 selected_columns.append(target.parts[-1])
79 else:
80 raise ValueError(f"Unknown query target {type(target)}")
82 if len(bookshelves) == 0:
83 bookshelves = pd.DataFrame([], columns=selected_columns)
84 else:
85 bookshelves.columns = self.get_columns()
86 for col in set(bookshelves.columns).difference(set(selected_columns)):
87 bookshelves = bookshelves.drop(col, axis=1)
88 return bookshelves
90 def get_columns(self) -> list:
91 """
92 Gets the columns of the table.
94 Returns:
95 list: List of column names.
96 """
97 return [
98 'kind',
99 'id',
100 'etag',
101 'selfLink',
102 'volumeInfo',
103 'userInfo'
104 'saleInfo',
105 'accessInfo',
106 'searchInfo',
107 ]
110class BookshelvesTable(APITable):
111 """
112 A class for handling the bookshelves table.
113 """
115 def select(self, query: ast.Select) -> DataFrame:
116 """
117 Gets all info about the wanted bookshelves.
119 Args:
120 query (ast.Select): SQL query to parse.
122 Returns:
123 Response: Response object containing the results.
124 """
126 # Parse the query to get the conditions.
127 conditions = extract_comparison_conditions(query.where)
128 # Get the parameters for the request.
129 params = {}
130 for op, arg1, arg2 in conditions:
131 if arg1 == 'userId' or arg1 == 'source' or arg1 == 'fields':
132 if op != '=':
133 raise NotImplementedError
134 params[arg1] = arg2
135 elif arg1 == 'shelf':
136 if op == '=':
137 params[arg1] = arg2
138 elif op == '>':
139 params['minShelf'] = arg2
140 elif op == '<':
141 params['maxShelf'] = arg2
142 else:
143 raise NotImplementedError
144 else:
145 raise NotImplementedError
147 # Get the bookshelves from the Google Books API.
148 bookshelves = self.handler.\
149 call_application_api(method_name='get_bookshelves', params=params)
151 selected_columns = []
152 for target in query.targets:
153 if isinstance(target, ast.Star):
154 selected_columns = self.get_columns()
155 break
156 elif isinstance(target, ast.Identifier):
157 selected_columns.append(target.parts[-1])
158 else:
159 raise ValueError(f"Unknown query target {type(target)}")
161 if len(bookshelves) == 0:
162 bookshelves = pd.DataFrame([], columns=selected_columns)
163 else:
164 bookshelves.columns = self.get_columns()
165 for col in set(bookshelves.columns).difference(set(selected_columns)):
166 bookshelves = bookshelves.drop(col, axis=1)
167 return bookshelves
169 def get_columns(self) -> list:
170 """Gets all columns to be returned in pandas DataFrame responses"""
171 return [
172 'kind',
173 'id',
174 'selfLink',
175 'title',
176 'description',
177 'access',
178 'updated',
179 'created',
180 'volumeCount',
181 'volumesLastUpdated'
182 ]