Ordering suggested channel names by video count.

This commit is contained in:
Roland 2024-04-16 10:04:06 -06:00
parent 969a065a6c
commit 7669cfe361
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
3 changed files with 40 additions and 25 deletions

View file

@ -1,7 +1,7 @@
from pathlib import Path
from typing import List, Dict, Union
from sqlalchemy import asc, or_
from sqlalchemy import or_, func, desc, asc
from sqlalchemy.orm import Session
from sqlalchemy.orm.exc import NoResultFound
@ -14,7 +14,7 @@ from .. import schema
from ..common import check_for_channel_conflicts
from ..errors import UnknownChannel
from ..lib import save_channels_config
from ..models import Channel
from ..models import Channel, Video
logger = logger.getChild(__name__)
@ -202,15 +202,28 @@ def download_channel(id_: int, reset_attempts: bool = False):
@optional_session
async def search_channels_by_name(name: str, limit: int = 5, session: Session = None) -> List[Channel]:
async def search_channels_by_name(name: str, limit: int = 5, session: Session = None,
order_by_video_count: bool = False) -> List[Channel]:
name = name or ''
name_no_spaces = ''.join(name.split(' '))
stmt = session.query(Channel) \
.filter(or_(
Channel.name.ilike(f'%{name}%'),
Channel.name.ilike(f'%{name_no_spaces}%'),
)) \
.order_by(asc(Channel.name)) \
.limit(limit)
channels = stmt.all()
if order_by_video_count:
stmt = session.query(Channel, func.count(Video.id).label('video_count')) \
.filter(or_(
Channel.name.ilike(f'%{name}%'),
Channel.name.ilike(f'%{name_no_spaces}%'),
)) \
.join(Video, Video.channel_id == Channel.id) \
.group_by(Channel.id, Channel.name) \
.order_by(desc('video_count')) \
.limit(limit)
channels = [i[0] for i in stmt]
else:
stmt = session.query(Channel) \
.filter(or_(
Channel.name.ilike(f'%{name}%'),
Channel.name.ilike(f'%{name_no_spaces}%'),
)) \
.order_by(asc(Channel.name)) \
.limit(limit)
channels = stmt.all()
return channels