|
|
@ -9,7 +9,7 @@ import flask_restful.reqparse
|
|
|
|
import sqlalchemy
|
|
|
|
import sqlalchemy
|
|
|
|
import sqlalchemy.engine
|
|
|
|
import sqlalchemy.engine
|
|
|
|
|
|
|
|
|
|
|
|
from db import db, Video, Comment, Association, Emote
|
|
|
|
from db import db, Video, Comment, Association, Emote, Clip
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app = flask.Flask(__name__)
|
|
|
|
app = flask.Flask(__name__)
|
|
|
@ -90,6 +90,23 @@ emote_fields = {
|
|
|
|
'code': flask_restful.fields.String(),
|
|
|
|
'code': flask_restful.fields.String(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
clip_fields = {
|
|
|
|
|
|
|
|
'slug': flask_restful.fields.String(),
|
|
|
|
|
|
|
|
'video_id': flask_restful.fields.Integer(),
|
|
|
|
|
|
|
|
'video_offset': flask_restful.fields.Float(),
|
|
|
|
|
|
|
|
'title': flask_restful.fields.String(),
|
|
|
|
|
|
|
|
'game': flask_restful.fields.String(),
|
|
|
|
|
|
|
|
'duration': flask_restful.fields.Float(),
|
|
|
|
|
|
|
|
'curator_id': flask_restful.fields.Integer(),
|
|
|
|
|
|
|
|
'curator_name': flask_restful.fields.String(),
|
|
|
|
|
|
|
|
'curator_display_name': flask_restful.fields.String(),
|
|
|
|
|
|
|
|
'curator_logo': flask_restful.fields.String(),
|
|
|
|
|
|
|
|
'thumbnail_tiny': flask_restful.fields.String(),
|
|
|
|
|
|
|
|
'thumbnail_small': flask_restful.fields.String(),
|
|
|
|
|
|
|
|
'thumbnail_medium': flask_restful.fields.String(),
|
|
|
|
|
|
|
|
'created_at': flask_restful.fields.DateTime(dt_format='iso8601'),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
filter_parser = flask_restful.reqparse.RequestParser()
|
|
|
|
filter_parser = flask_restful.reqparse.RequestParser()
|
|
|
|
filter_parser.add_argument('filter', type=str)
|
|
|
|
filter_parser.add_argument('filter', type=str)
|
|
|
@ -251,6 +268,43 @@ class EmotesResource(flask_restful.Resource):
|
|
|
|
return emotes, 200
|
|
|
|
return emotes, 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ClipResource(flask_restful.Resource):
|
|
|
|
|
|
|
|
@flask_restful.marshal_with(clip_fields)
|
|
|
|
|
|
|
|
def get(self, slug):
|
|
|
|
|
|
|
|
q = db.session.query(Clip).filter(Clip.slug == slug)
|
|
|
|
|
|
|
|
clip = q.first()
|
|
|
|
|
|
|
|
if not clip:
|
|
|
|
|
|
|
|
flask_restful.abort(404, message='Clip {0} does not exist'.format(slug))
|
|
|
|
|
|
|
|
return slug, 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ClipsResource(flask_restful.Resource):
|
|
|
|
|
|
|
|
@flask_restful.marshal_with(clip_fields)
|
|
|
|
|
|
|
|
def get(self):
|
|
|
|
|
|
|
|
args = filter_parser.parse_args()
|
|
|
|
|
|
|
|
q = db.session.query(Clip)
|
|
|
|
|
|
|
|
if args['filter']:
|
|
|
|
|
|
|
|
q = q.filter(Clip.title.ilike('%{}%'.format(args['filter'])))
|
|
|
|
|
|
|
|
count = q.count()
|
|
|
|
|
|
|
|
if args['sort_order'] == 'random':
|
|
|
|
|
|
|
|
q = q.order_by(sqlalchemy.func.random())
|
|
|
|
|
|
|
|
elif args['sort_by']:
|
|
|
|
|
|
|
|
col = getattr(Clip, args['sort_by'], None)
|
|
|
|
|
|
|
|
if col:
|
|
|
|
|
|
|
|
if args['sort_order']:
|
|
|
|
|
|
|
|
order_by = getattr(col, args['sort_order'], None)
|
|
|
|
|
|
|
|
if order_by:
|
|
|
|
|
|
|
|
q = q.order_by(order_by())
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
q = q.order_by(col)
|
|
|
|
|
|
|
|
if args['page_size']:
|
|
|
|
|
|
|
|
q = q.limit(args['page_size'])
|
|
|
|
|
|
|
|
if args['page_number'] and args['page_size']:
|
|
|
|
|
|
|
|
q = q.offset(args['page_number'] * args['page_size'])
|
|
|
|
|
|
|
|
clips = q.all()
|
|
|
|
|
|
|
|
return clips, 200, {'X-Total-Count': count}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
api.add_resource(VideoResource, '/videos/<int:id>')
|
|
|
|
api.add_resource(VideoResource, '/videos/<int:id>')
|
|
|
|
api.add_resource(VideosResource, '/videos')
|
|
|
|
api.add_resource(VideosResource, '/videos')
|
|
|
|
api.add_resource(CommentResource, '/videos/<int:video_id>/comments/<string:comment_id>')
|
|
|
|
api.add_resource(CommentResource, '/videos/<int:video_id>/comments/<string:comment_id>')
|
|
|
@ -258,6 +312,8 @@ api.add_resource(CommentsResource, '/videos/<int:id>/comments')
|
|
|
|
api.add_resource(SearchResource, '/search')
|
|
|
|
api.add_resource(SearchResource, '/search')
|
|
|
|
api.add_resource(EmoteResource, '/emotes/<int:id>')
|
|
|
|
api.add_resource(EmoteResource, '/emotes/<int:id>')
|
|
|
|
api.add_resource(EmotesResource, '/emotes')
|
|
|
|
api.add_resource(EmotesResource, '/emotes')
|
|
|
|
|
|
|
|
api.add_resource(ClipResource, '/clips/<string:slug>')
|
|
|
|
|
|
|
|
api.add_resource(ClipsResource, '/clips')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if __name__ == '__main__':
|