123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- import html
- from hashlib import sha1
- from peewee import fn
- from aiofiles.tempfile import NamedTemporaryFile
- from db import db
- from db import Video, User, Like, Tag, VideoTag
- from tools import parse_tags, parse_offset
- from tools import probe_video, create_preview
- from tools import save_content
- class VideoError(Exception): pass
- async def find_video_by_id(video_id):
- try:
- video_id = int(video_id)
- video_id = abs(video_id)
- except ValueError:
- raise VideoError('illegal video_id')
- try:
- return await db.get(Video
- .select(Video, User)
- .join(User)
- .switch(Video)
- .where(
- (Video.id == video_id) &
- (~Video.is_hidden)
- ))
- except Video.DoesNotExist:
- raise VideoError('video is unavailable or doesn\'t exist')
- async def get_tags(video_id):
- video = await find_video_by_id(video_id)
- tags = await db.execute(Tag
- .select()
- .join(VideoTag)
- .join(Video)
- .where(Video.id == video.id))
- return [tag.tag for tag in tags]
- async def get_likes(video_id):
- video = await find_video_by_id(video_id)
- return await db.count(Like
- .select(Like, Video)
- .join(Video)
- .switch(Like)
- .where(Like.video.id == video.id))
- async def get_random_video():
- return await db.get(Video
- .select(Video, User)
- .join(User)
- .switch(Video)
- .order_by(fn.Random())
- .limit(1))
- async def serialize_video(video):
- return {
- 'id': video.id,
- 'upload_ts': int(video.upload_date.timestamp()),
- 'thumbnail': video.thumbnail,
- 'uploader_id': video.uploader.id,
- 'likes_count': await get_likes(video.id),
- 'tags_list': await get_tags(video.id)
- }
-
|