From 9402662281eeb661a840d0982b3f7af38a773cd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Forr=C3=B3?= Date: Mon, 27 Aug 2018 14:22:05 +0200 Subject: [PATCH] Add time filtering to Twitch Cache API --- apps/schedule/src/app/models/slot.ts | 10 ----- .../src/app/pipes/getdatetime.pipe.ts | 37 ------------------- twitch-cache-api/app.py | 18 +++++++++ 3 files changed, 18 insertions(+), 47 deletions(-) delete mode 100644 apps/schedule/src/app/models/slot.ts delete mode 100644 apps/schedule/src/app/pipes/getdatetime.pipe.ts diff --git a/apps/schedule/src/app/models/slot.ts b/apps/schedule/src/app/models/slot.ts deleted file mode 100644 index 832ef71..0000000 --- a/apps/schedule/src/app/models/slot.ts +++ /dev/null @@ -1,10 +0,0 @@ -export interface Slot { - label: string; - uuid: string; - description: string; - isEnabled: boolean; - isTimeEnabled: boolean; - hours: number; - minutes: number; - offset: number; -} diff --git a/apps/schedule/src/app/pipes/getdatetime.pipe.ts b/apps/schedule/src/app/pipes/getdatetime.pipe.ts deleted file mode 100644 index e0a142c..0000000 --- a/apps/schedule/src/app/pipes/getdatetime.pipe.ts +++ /dev/null @@ -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' -]; diff --git a/twitch-cache-api/app.py b/twitch-cache-api/app.py index d1bfd82..a24a540 100644 --- a/twitch-cache-api/app.py +++ b/twitch-cache-api/app.py @@ -141,6 +141,8 @@ event_fields = { filter_parser = flask_restful.reqparse.RequestParser() 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_order', type=str) filter_parser.add_argument('page_number', type=int) @@ -172,6 +174,10 @@ class VideosResource(flask_restful.Resource): q = db.session.query(Video) if 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() if args['sort_order'] == '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']))) if 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() if args['sort_order'] == 'random': q = q.order_by(sqlalchemy.func.random()) @@ -316,6 +326,10 @@ class ClipsResource(flask_restful.Resource): q = db.session.query(Clip) if 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() if args['sort_order'] == 'random': q = q.order_by(sqlalchemy.func.random()) @@ -353,6 +367,10 @@ class EventsResource(flask_restful.Resource): q = db.session.query(Event) if 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() if args['sort_order'] == 'random': q = q.order_by(sqlalchemy.func.random())