1
0

common.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. from peewee import fn
  2. from db import db
  3. from db import Video, User, Tag, VideoTag
  4. from video import serialize_video
  5. from tools import parse_tags, parse_tag, parse_offset
  6. async def search_videos(tags, offset=0):
  7. tags = parse_tags(tags)
  8. videos = await db.execute(Video
  9. .select(User, Video)
  10. .join(User)
  11. .switch(Video)
  12. .join(VideoTag)
  13. .join(Tag)
  14. .where(
  15. (Tag.tag << tags) &
  16. (~Video.is_hidden)
  17. )
  18. .group_by(User, Video)
  19. .having(fn.Count(Tag.id.distinct()) == len(tags))
  20. .order_by(Video.upload_date.desc())
  21. .offset(parse_offset(offset))
  22. .limit(6))
  23. return [await serialize_video(video) for video in videos]
  24. async def suggest_tag(tag):
  25. tags = await db.execute(Tag
  26. .select()
  27. .where(Tag.tag.startswith(parse_tag(tag))))
  28. return [tag.tag for tag in tags]