Verified Commit 4047e190 authored by Todd Weaver's avatar Todd Weaver
Browse files

Adding pagination to show more search results upon page reaching the end of scroll

Adjusted spinner to top headerbar (to show subsequent page loading)
Deduping videos from subsequent page loads
parent a3fd9357
......@@ -27,41 +27,47 @@ class Search:
# for internal plugins only
self.app_window = kwargs.get('app_window', None)
self.spinner = kwargs.get('spinner', None)
self.scroller = kwargs.get('scroller', None)
self.si_index = 0
self.this_instance = self.app_window.strong_instances[self.si_index]
self.search_video_ids = []
# limited access
self.add_result_meta = kwargs.get('add_result_meta', None)
def do_search(self, query):
esc_query = GLib.uri_escape_string(query, None, None)
uri = f"{self.this_instance}/api/v1/search?q={esc_query};fields=title,videoId,author,lengthSeconds,videoThumbnails"
def do_search(self, query, page):
self.spinner.set_visible(True)
self.query = query
esc_query = GLib.uri_escape_string(self.query, None, None)
uri = f"{self.this_instance}/api/v1/search?q={esc_query};page={page};fields=title,videoId,author,lengthSeconds,videoThumbnails"
#print(uri)
self.session = Soup.Session.new()
self.session.set_property("timeout", 5)
message = Soup.Message.new("GET", uri)
self.session.queue_message(message, self.show_results, message)
def show_results(self, session, result, message):
self.spinner.set_visible(False)
self.session.queue_message(message, self.show_results, page)
if message.status_code != 200:
self.app_window.show_error_box("Service Failure",
"There is no response from the streaming servers.")
def show_results(self, session, results, page):
if results.status_code != 200:
if page == 1:
self.app_window.show_error_box("Service Failure",
"There is no response from the streaming servers.")
return False
try:
self.search_json = json.loads(message.response_body.data)
self.search_json = json.loads(results.response_body.data)
except:
self.app_window.show_error_box("Service Failure",
"The streaming server response failed to parse results.")
if page == 1:
self.app_window.show_error_box("Service Failure",
"The streaming server response failed to parse results.")
return False
for video_meta in self.search_json:
self.get_poster_url(video_meta)
# loop to find a playable video instance
self.get_video_details(video_meta)
if video_meta['videoId'] not in self.search_video_ids:
self.get_poster_url(video_meta)
self.get_video_details(video_meta)
def get_poster_url(self, video_meta):
for poster in video_meta['videoThumbnails']:
......@@ -119,7 +125,6 @@ class Search:
#print(video_meta['title'])
video_meta.pop('video_uri')
self.si_index += 1
if len(self.app_window.strong_instances) > self.si_index:
self.this_instance = self.app_window.strong_instances[self.si_index]
#print(self.this_instance)
......@@ -129,8 +134,16 @@ class Search:
return False
if 'video_uri' in video_meta:
# add the result to the video meta
# which will trigger the video to display to the user
self.add_result_meta(video_meta)
# appending known playable videos to filter duplicates
self.search_video_ids.append(video_meta['videoId'])
self.spinner.set_visible(False)
self.scroller.set_visible(True)
def get_download_uris(self, video_meta):
# get download link urls based on (future) user-config
# video quality: ["480p", "720p", "1080p"] # default 720p
......
......@@ -75,6 +75,19 @@ audio-volume-medium-symbolic</property>
</style>
</object>
</child>
<child>
<object class="GtkSpinner" id="spinner">
<property name="visible">False</property>
<property name="can-focus">True</property>
<property name="halign">center</property>
<property name="valign">center</property>
<property name="active">True</property>
</object>
<packing>
<property name="pack-type">end</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkMenuButton" id="menu_button">
<property name="visible">True</property>
......@@ -151,21 +164,9 @@ audio-volume-medium-symbolic</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkSpinner" id="spinner">
<property name="visible">False</property>
<property name="can-focus">False</property>
<property name="halign">center</property>
<property name="valign">center</property>
<property name="active">True</property>
</object>
<packing>
<property name="position">3</property>
</packing>
</child>
<child>
<object class="GtkScrolledWindow" id="scroller">
<property name="visible">True</property>
<property name="visible">False</property>
<property name="can-focus">True</property>
<property name="hexpand">False</property>
<property name="vexpand">False</property>
......@@ -188,7 +189,7 @@ audio-volume-medium-symbolic</property>
</child>
</object>
<packing>
<property name="position">4</property>
<property name="position">3</property>
</packing>
</child>
<child>
......@@ -250,7 +251,7 @@ audio-volume-medium-symbolic</property>
</child>
</object>
</child>
</object>
</object>
</child>
</object>
</child>
......
......@@ -90,20 +90,22 @@ class StreamWindow(Handy.ApplicationWindow):
self.status_page.set_visible(False)
self.hide_error_box()
self.spinner.set_visible(True)
self.scroller.set_visible(False)
search_query = search_box.get_text()
self.search_query = search_box.get_text()
self.clear_results()
self.page_results = 1
if not self.strong_instances:
self.show_error_box("Service Failure",
"No strong video server instances found yet. Try again shortly.")
else:
search = DefaultSearch(app_window = self,
self.search = DefaultSearch(app_window = self,
spinner = self.spinner,
scroller = self.scroller,
add_result_meta = self.add_result_meta)
search.do_search(query = search_query)
self.search.do_search(query = self.search_query, page = self.page_results)
def add_result_meta(self, video_meta):
self.video_results_meta.append(video_meta)
......@@ -124,6 +126,11 @@ class StreamWindow(Handy.ApplicationWindow):
upper = vadj.get_upper()
page_size = vadj.get_page_size()
self.page_upper = upper
if position + page_size >= upper:
self.page_results += 1
self.search.do_search(query = self.search_query, page = self.page_results)
def fullscreen_toggle(self, focus_child):
if self.is_fullscreen:
focus_child.get_child().unfullscreen_button(None)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment