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

Partial paginated search implementation #688

Open
wants to merge 3 commits into
base: master
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
6 changes: 3 additions & 3 deletions trackma/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,12 @@ def get(self):
"""Get list from memory"""
return self.showlist

def search(self, criteria, method):
def search(self, criteria, method, page):
# Tell API to search
results = self.api.search(criteria, method)
results = self.api.search(criteria, method, page or 1)
self.api.logout()
if results:
return results
return results if page else results[0]

raise utils.DataError('No results.')

Expand Down
4 changes: 2 additions & 2 deletions trackma/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ def tracker_status(self):

return None

def search(self, criteria, method=utils.SearchMethod.KW):
def search(self, criteria, method=utils.SearchMethod.KW, page=None):
"""
Request a remote list of shows matching the criteria
and returns it as a list of show dictionaries.
Expand All @@ -491,7 +491,7 @@ def search(self, criteria, method=utils.SearchMethod.KW):
raise utils.EngineError(
'Search method not supported by API or mediatype.')

return self.data_handler.search(criteria, method)
return self.data_handler.search(criteria, method, page)

def add_show(self, show, status=None):
"""
Expand Down
2 changes: 1 addition & 1 deletion trackma/lib/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def delete_show(self, item):
"""
raise NotImplementedError

def search(self, criteria, method):
def search(self, criteria, method, page):
"""
Called when the data handler needs a detailed list of shows from the remote server.
It should return a list of show dictionaries with the additional 'extra' key (which is a list of tuples)
Expand Down
4 changes: 2 additions & 2 deletions trackma/lib/libanilist.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ def delete_show(self, item):
variables = {'id': item['my_id']}
self._request(query, variables)

def search(self, criteria, method):
def search(self, criteria, method, page):
self.check_credentials()
self.msg.info("Searching for {}...".format(criteria))

Expand Down Expand Up @@ -415,7 +415,7 @@ def search(self, criteria, method):
infolist.append(self._parse_info(media))

self._emit_signal('show_info_changed', infolist)
return infolist
return (infolist, len(infolist), 1, 1)

def request_info(self, itemlist):
self.check_credentials()
Expand Down
10 changes: 7 additions & 3 deletions trackma/lib/libkitsu.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,18 +452,22 @@ def delete_show(self, item):
except urllib.error.URLError as e:
raise utils.APIError('Error deleting: ' + str(e.reason))

def search(self, query, method):
def search(self, query, method, page):
self.msg.info("Searching for %s..." % query)
item_per_page = 20

values = {
"filter[text]": query,
"page[limit]": 20,
"page[limit]": item_per_page,
"page[offset]": (page - 1) * item_per_page
}

try:
data = self._request('GET', self.prefix +
"/" + self.mediatype, get=values)
shows = json.loads(data)
pages = int(shows['meta']['count'] / item_per_page) + \
1 if shows['meta']['count'] % item_per_page != 0 else 0

infolist = []
for media in shows['data']:
Expand All @@ -475,7 +479,7 @@ def search(self, query, method):
if not infolist:
raise utils.APIError('No results.')

return infolist
return (infolist, shows['meta']['count'], page, pages,)
except urllib.error.HTTPError as e:
raise utils.APIError('Error searching: ' + str(e.code))
except urllib.error.URLError as e:
Expand Down
4 changes: 2 additions & 2 deletions trackma/lib/libmal.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ def delete_show(self, item):
self.msg.info("Deleting item %s..." % item['title'])
data = self._request('DELETE', self.query_url + '/%s/%d/my_list_status' % (self.mediatype, item['id']), auth=True)

def search(self, criteria, method):
def search(self, criteria, method, page):
self.check_credentials()
self.msg.info("Searching for {}...".format(criteria))

Expand All @@ -333,7 +333,7 @@ def search(self, criteria, method):
results.append(self._parse_info(item['node']))

self._emit_signal('show_info_changed', results)
return results
return (results, len(results), 1, 1)

def request_info(self, itemlist):
self.check_credentials()
Expand Down
6 changes: 3 additions & 3 deletions trackma/lib/libshikimori.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ def delete_show(self, item):
data = self._request(
"DELETE", self.api_url + "/user_rates/{}".format(item['my_id']), auth=True)

def search(self, criteria, method):
def search(self, criteria, method, page):
self.check_credentials()

self.msg.info("Searching for {}...".format(criteria))
Expand All @@ -293,7 +293,7 @@ def search(self, criteria, method):
except ValueError:
# An empty document, without any JSON, is returned
# when there are no results.
return []
return ([], 0, 0, 0)

showlist = []

Expand All @@ -314,7 +314,7 @@ def search(self, criteria, method):

showlist.append(show)

return showlist
return (showlist, len(showlist), 1, 1)

def request_info(self, itemlist):
self.check_credentials()
Expand Down
6 changes: 3 additions & 3 deletions trackma/lib/libvndb.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,14 +321,14 @@ def delete_show(self, item):
if name != 'ok':
raise utils.APIError("Invalid response (%s)" % name)

def search(self, criteria, method):
def search(self, criteria, method, page):
self.check_credentials()

results = list()
self.msg.info('Searching for %s...' % criteria)

(name, data) = self._sendcmd('get vn basic,details (search ~ "%s")' % criteria,
{'page': 1,
{'page': page,
'results': self.pagesize_details,
})

Expand All @@ -345,7 +345,7 @@ def search(self, criteria, method):
if not results:
raise utils.APIError('No results.')

return results
return (results, len(results), 1, 1)

def logout(self):
self.msg.info('Disconnecting...')
Expand Down
Loading