Skip to content

Commit

Permalink
Put back sort by oldest logic since YouTube added it back
Browse files Browse the repository at this point in the history
Previous commit replaced it with shorts-filtering, use sort code
number 4 for that instead. Sort by oldest is still broken
pending reverse engineering of new ctoken format, however.
  • Loading branch information
user234683 committed Oct 15, 2023
1 parent d3b5ab1 commit 877b88a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
31 changes: 20 additions & 11 deletions youtube/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,18 +372,23 @@ def get_channel_first_page(base_url=None, tab='videos', channel_id=None):
def get_channel_page_general_url(base_url, tab, request, channel_id=None):

page_number = int(request.args.get('page', 1))
# sort 1: views
# sort 2: oldest
# sort 3: newest
# sort 4: newest - no shorts (Just a kludge on our end, not internal to yt)
sort = request.args.get('sort', '3')
view = request.args.get('view', '1')
query = request.args.get('query', '')
ctoken = request.args.get('ctoken', '')
include_shorts = (sort != '2')
default_params = (page_number == 1 and sort in ('2', '3') and view == '1')
include_shorts = (sort != '4')
default_params = (page_number == 1 and sort in ('3', '4') and view == '1')
continuation = bool(ctoken) # whether or not we're using a continuation
page_size = 30
try_channel_api = True
polymer_json = None

# Use the special UU playlist which contains all the channel's uploads
playlist_method_failed = False
if tab == 'videos':
if tab == 'videos' and sort in ('3', '4'):
if not channel_id:
channel_id = get_channel_id(base_url)
if page_number == 1 and include_shorts:
Expand Down Expand Up @@ -418,25 +423,29 @@ def get_channel_page_general_url(base_url, tab, request, channel_id=None):
pl_json = tasks[0].value
pl_info = yt_data_extract.extract_playlist_info(pl_json)
number_of_videos = tasks[2].value
print(number_of_videos)
info = pl_info
info['channel_id'] = channel_id
info['current_tab'] = 'videos'
if info['items']:
if info['items']: # Success
page_size = 100
else:
playlist_method_failed = True # Try the first-page method next
try_channel_api = False
else: # Try the first-page method next
try_channel_api = True

# Use the regular channel API
if tab in ('shorts','streams') or tab=='videos' and playlist_method_failed:
if tab in ('shorts', 'streams') or (tab=='videos' and try_channel_api):
if channel_id:
num_videos_call = (get_number_of_videos_channel, channel_id)
else:
num_videos_call = (get_number_of_videos_general, base_url)

# Use ctoken method, which YouTube changes all the time
if channel_id and not default_params:
page_call = (get_channel_tab, channel_id, page_number, sort,
if sort == 4:
_sort = 3
else:
_sort = sort
page_call = (get_channel_tab, channel_id, page_number, _sort,
tab, view, ctoken)
# Use the first-page method, which won't break
else:
Expand Down Expand Up @@ -468,7 +477,7 @@ def get_channel_page_general_url(base_url, tab, request, channel_id=None):
else:
flask.abort(404, 'Unknown channel tab: ' + tab)

if tab != 'videos' or playlist_method_failed:
if polymer_json is not None:
info = yt_data_extract.extract_channel_info(
json.loads(polymer_json), tab, continuation=continuation
)
Expand Down
4 changes: 2 additions & 2 deletions youtube/templates/channel.html
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ <h3>Description</h3>
<div class="content {{ current_tab + '-content'}}">
<div id="links-metadata">
{% if current_tab in ('videos', 'shorts', 'streams') %}
{% set sorts = [('1', 'views'), ('2', 'newest - no shorts'), ('3', 'newest')] %}
{% set sorts = [('1', 'views'), ('2', 'oldest'), ('3', 'newest'), ('4', 'newest - no shorts'),] %}
<div id="number-of-results">{{ number_of_videos }} videos</div>
{% elif current_tab == 'playlists' %}
{% set sorts = [('2', 'oldest'), ('3', 'newest'), ('4', 'last video added')] %}
Expand Down Expand Up @@ -196,7 +196,7 @@ <h2 class="page-number">No results</h2>

{% if current_tab in ('videos', 'shorts', 'streams') %}
<nav class="page-button-row">
{{ common_elements.page_buttons(number_of_pages, channel_url + '/' + current_tab, parameters_dictionary, include_ends=(current_sort.__str__() in '23')) }}
{{ common_elements.page_buttons(number_of_pages, channel_url + '/' + current_tab, parameters_dictionary, include_ends=(current_sort.__str__() in '34')) }}
</nav>
{% elif current_tab == 'playlists' or current_tab == 'search' %}
<nav class="next-previous-button-row">
Expand Down

0 comments on commit 877b88a

Please sign in to comment.