Add time filtering to Twitch Cache API

master
Nikola Forró 6 years ago
parent 79aacd731a
commit 9402662281

@ -1,10 +0,0 @@
export interface Slot {
label: string;
uuid: string;
description: string;
isEnabled: boolean;
isTimeEnabled: boolean;
hours: number;
minutes: number;
offset: number;
}

@ -1,37 +0,0 @@
import {
Pipe,
PipeTransform
} from '@angular/core';
import { Slot } from '../models/slot';
@Pipe({
name: 'getdatetime'
})
export class GetDateTimePipe implements PipeTransform {
transform(slot: Slot): Date {
let result = new Date();
let day = WEEKDAYS.indexOf(slot.label.toLowerCase());
result.setUTCHours(0, 0, 0, 0);
result.setDate(result.getDate() + day - result.getDay());
let remainder = slot.offset % 60;
result.setUTCHours(slot.hours - (slot.offset - remainder) / 60 >> 0);
result.setUTCMinutes(slot.minutes - remainder);
if (result.getTime() < Date.now()) {
result.setDate(result.getDate() + 7);
}
return result;
}
}
const WEEKDAYS = [
'sunday',
'monday',
'tuesday',
'wednesday',
'thursday',
'friday',
'saturday'
];

@ -141,6 +141,8 @@ event_fields = {
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)
filter_parser.add_argument('older_than', type=flask_restful.inputs.datetime_from_iso8601)
filter_parser.add_argument('newer_than', type=flask_restful.inputs.datetime_from_iso8601)
filter_parser.add_argument('sort_by', type=str) filter_parser.add_argument('sort_by', type=str)
filter_parser.add_argument('sort_order', type=str) filter_parser.add_argument('sort_order', type=str)
filter_parser.add_argument('page_number', type=int) filter_parser.add_argument('page_number', type=int)
@ -172,6 +174,10 @@ class VideosResource(flask_restful.Resource):
q = db.session.query(Video) q = db.session.query(Video)
if args['filter']: if args['filter']:
q = q.filter(Video.title.ilike('%{}%'.format(args['filter']))) q = q.filter(Video.title.ilike('%{}%'.format(args['filter'])))
if args['older_than']:
q = q.filter(Video.recorded_at < args['older_than'])
if args['newer_than']:
q = q.filter(Video.recorded_at > args['newer_than'])
count = q.count() count = q.count()
if args['sort_order'] == 'random': if args['sort_order'] == 'random':
q = q.order_by(sqlalchemy.func.random()) q = q.order_by(sqlalchemy.func.random())
@ -257,6 +263,10 @@ class SearchResource(flask_restful.Resource):
Comment.commenter_display_name.ilike('%{}%'.format(args['commenter']))) Comment.commenter_display_name.ilike('%{}%'.format(args['commenter'])))
if args['term']: if args['term']:
q = q.filter(Comment.message_body.ilike('%{}%'.format(args['term']))) q = q.filter(Comment.message_body.ilike('%{}%'.format(args['term'])))
if args['older_than']:
q = q.filter(Comment.created_at < args['older_than'])
if args['newer_than']:
q = q.filter(Comment.created_at > args['newer_than'])
count = q.count() count = q.count()
if args['sort_order'] == 'random': if args['sort_order'] == 'random':
q = q.order_by(sqlalchemy.func.random()) q = q.order_by(sqlalchemy.func.random())
@ -316,6 +326,10 @@ class ClipsResource(flask_restful.Resource):
q = db.session.query(Clip) q = db.session.query(Clip)
if args['filter']: if args['filter']:
q = q.filter(Clip.title.ilike('%{}%'.format(args['filter']))) q = q.filter(Clip.title.ilike('%{}%'.format(args['filter'])))
if args['older_than']:
q = q.filter(Clip.created_at < args['older_than'])
if args['newer_than']:
q = q.filter(Clip.created_at > args['newer_than'])
count = q.count() count = q.count()
if args['sort_order'] == 'random': if args['sort_order'] == 'random':
q = q.order_by(sqlalchemy.func.random()) q = q.order_by(sqlalchemy.func.random())
@ -353,6 +367,10 @@ class EventsResource(flask_restful.Resource):
q = db.session.query(Event) q = db.session.query(Event)
if args['filter']: if args['filter']:
q = q.filter(Event.title.ilike('%{}%'.format(args['filter']))) q = q.filter(Event.title.ilike('%{}%'.format(args['filter'])))
if args['older_than']:
q = q.filter(Event.start < args['older_than'])
if args['newer_than']:
q = q.filter(Event.end > args['newer_than'])
count = q.count() count = q.count()
if args['sort_order'] == 'random': if args['sort_order'] == 'random':
q = q.order_by(sqlalchemy.func.random()) q = q.order_by(sqlalchemy.func.random())

Loading…
Cancel
Save