mirror of
https://github.com/ihabunek/toot.git
synced 2024-09-29 04:35:54 -04:00
Fetch next statuses when going below last
This commit is contained in:
parent
d789d21171
commit
923ac57811
19
toot/api.py
19
toot/api.py
@ -88,21 +88,20 @@ def timeline_home(app, user):
|
|||||||
return http.get(app, user, '/api/v1/timelines/home').json()
|
return http.get(app, user, '/api/v1/timelines/home').json()
|
||||||
|
|
||||||
|
|
||||||
def _get_next_path(headers):
|
def timeline_generator(app, user, limit=20):
|
||||||
|
path = '/api/v1/timelines/home?limit={}'.format(limit)
|
||||||
|
|
||||||
|
def get_next_path(headers):
|
||||||
links = headers.get('Link', '')
|
links = headers.get('Link', '')
|
||||||
matches = re.match('<([^>]+)>; rel="next"', links)
|
matches = re.match('<([^>]+)>; rel="next"', links)
|
||||||
if matches:
|
if matches:
|
||||||
url = matches.group(1)
|
parsed = urlparse(matches.group(1))
|
||||||
return urlparse(url).path
|
return "?".join([parsed.path, parsed.query])
|
||||||
|
|
||||||
|
while path:
|
||||||
def timeline_generator(app, user):
|
response = http.get(app, user, path)
|
||||||
next_path = '/api/v1/timelines/home'
|
|
||||||
|
|
||||||
while next_path:
|
|
||||||
response = http.get(app, user, next_path)
|
|
||||||
yield response.json()
|
yield response.json()
|
||||||
next_path = _get_next_path(response.headers)
|
path = get_next_path(response.headers)
|
||||||
|
|
||||||
|
|
||||||
def upload_media(app, user, file):
|
def upload_media(app, user, file):
|
||||||
|
34
toot/app.py
34
toot/app.py
@ -63,7 +63,7 @@ class TimelineApp:
|
|||||||
self.right_width = screen_width - self.left_width
|
self.right_width = screen_width - self.left_width
|
||||||
|
|
||||||
self.top = curses.newwin(2, screen_width, 0, 0)
|
self.top = curses.newwin(2, screen_width, 0, 0)
|
||||||
self.left = curses.newpad(screen_height * 2, self.left_width)
|
self.left = curses.newpad(screen_height - 4, self.left_width)
|
||||||
self.right = curses.newwin(screen_height - 4, self.right_width, 2, self.left_width)
|
self.right = curses.newwin(screen_height - 4, self.right_width, 2, self.left_width)
|
||||||
self.bottom = curses.newwin(2, screen_width, screen_height - 2, 0)
|
self.bottom = curses.newwin(2, screen_width, screen_height - 2, 0)
|
||||||
|
|
||||||
@ -108,8 +108,9 @@ class TimelineApp:
|
|||||||
self.clear_bottom_message()
|
self.clear_bottom_message()
|
||||||
|
|
||||||
if self.selected + 1 >= len(self.statuses):
|
if self.selected + 1 >= len(self.statuses):
|
||||||
self.draw_bottom_message("Cannot move beyond last toot.", Color.GREEN)
|
self.fetch_next()
|
||||||
return
|
self.draw_statuses(self.left, self.selected + 1)
|
||||||
|
self.draw_bottom_status()
|
||||||
|
|
||||||
old_index = self.selected
|
old_index = self.selected
|
||||||
new_index = self.selected + 1
|
new_index = self.selected + 1
|
||||||
@ -125,10 +126,11 @@ class TimelineApp:
|
|||||||
self.draw_status_row(self.left, old_status, 3 * old_index - 1, False)
|
self.draw_status_row(self.left, old_status, 3 * old_index - 1, False)
|
||||||
self.draw_status_row(self.left, new_status, 3 * new_index - 1, True)
|
self.draw_status_row(self.left, new_status, 3 * new_index - 1, True)
|
||||||
self.draw_status_details(self.right, new_status)
|
self.draw_status_details(self.right, new_status)
|
||||||
self.draw_bottom_status(self.bottom, new_index, len(self.statuses))
|
self.draw_bottom_status()
|
||||||
|
|
||||||
def fetch_next(self):
|
def fetch_next(self):
|
||||||
try:
|
try:
|
||||||
|
self.draw_bottom_message("Loading toots...", Color.BLUE)
|
||||||
statuses = next(self.status_generator)
|
statuses = next(self.status_generator)
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
return None
|
return None
|
||||||
@ -136,6 +138,8 @@ class TimelineApp:
|
|||||||
for status in statuses:
|
for status in statuses:
|
||||||
self.statuses.append(parse_status(status))
|
self.statuses.append(parse_status(status))
|
||||||
|
|
||||||
|
self.draw_bottom_message("Loaded {} toots".format(len(statuses)), Color.GREEN)
|
||||||
|
|
||||||
return len(statuses)
|
return len(statuses)
|
||||||
|
|
||||||
def full_redraw(self):
|
def full_redraw(self):
|
||||||
@ -154,7 +158,7 @@ class TimelineApp:
|
|||||||
self.draw_statuses(self.left)
|
self.draw_statuses(self.left)
|
||||||
self.draw_status_details(self.right, self.get_selected_status())
|
self.draw_status_details(self.right, self.get_selected_status())
|
||||||
self.draw_usage(self.bottom)
|
self.draw_usage(self.bottom)
|
||||||
self.draw_bottom_status(self.bottom, self.selected, len(self.statuses))
|
self.draw_bottom_status()
|
||||||
|
|
||||||
screen_height, screen_width = self.stdscr.getmaxyx()
|
screen_height, screen_width = self.stdscr.getmaxyx()
|
||||||
self.left.refresh(0, 0, 2, 0, screen_height - 3, self.left_width)
|
self.left.refresh(0, 0, 2, 0, screen_height - 3, self.left_width)
|
||||||
@ -200,8 +204,16 @@ class TimelineApp:
|
|||||||
screen_height, screen_width = self.stdscr.getmaxyx()
|
screen_height, screen_width = self.stdscr.getmaxyx()
|
||||||
window.refresh(0, 0, 2, 0, screen_height - 4, self.left_width)
|
window.refresh(0, 0, 2, 0, screen_height - 4, self.left_width)
|
||||||
|
|
||||||
def draw_statuses(self, window):
|
def draw_statuses(self, window, starting=0):
|
||||||
|
# Resize window to accomodate statuses if required
|
||||||
|
height, width = window.getmaxyx()
|
||||||
|
new_height = len(self.statuses) * 3 + 1
|
||||||
|
if new_height > height:
|
||||||
|
window.resize(new_height, width)
|
||||||
|
window.box()
|
||||||
|
|
||||||
for index, status in enumerate(self.statuses):
|
for index, status in enumerate(self.statuses):
|
||||||
|
if index >= starting:
|
||||||
offset = 3 * index - 1
|
offset = 3 * index - 1
|
||||||
highlight = self.selected == index
|
highlight = self.selected == index
|
||||||
self.draw_status_row(window, status, offset, highlight)
|
self.draw_status_row(window, status, offset, highlight)
|
||||||
@ -256,16 +268,16 @@ class TimelineApp:
|
|||||||
|
|
||||||
def draw_bottom_message(self, text, color=0):
|
def draw_bottom_message(self, text, color=0):
|
||||||
_, width = self.bottom.getmaxyx()
|
_, width = self.bottom.getmaxyx()
|
||||||
text = trunc(text, width - 1)
|
text = trunc(text, width - 1).ljust(width - 1)
|
||||||
self.bottom.addstr(1, 0, text, color)
|
self.bottom.addstr(1, 0, text, color)
|
||||||
self.bottom.refresh()
|
self.bottom.refresh()
|
||||||
|
|
||||||
def draw_bottom_status(self, window, index, count):
|
def draw_bottom_status(self):
|
||||||
_, width = self.bottom.getmaxyx()
|
_, width = self.bottom.getmaxyx()
|
||||||
text = "Showing toot {} of {}".format(index + 1, count)
|
text = "Showing toot {} of {}".format(self.selected + 1, len(self.statuses))
|
||||||
text = trunc(text, width - 1).ljust(width - 1)
|
text = trunc(text, width - 1).ljust(width - 1)
|
||||||
window.addstr(0, 0, text, Color.WHITE_ON_BLUE | curses.A_BOLD)
|
self.bottom.addstr(0, 0, text, Color.WHITE_ON_BLUE | curses.A_BOLD)
|
||||||
window.refresh()
|
self.bottom.refresh()
|
||||||
|
|
||||||
|
|
||||||
def parse_status(status):
|
def parse_status(status):
|
||||||
|
Loading…
Reference in New Issue
Block a user