def save_captions(self, series, episode): captions = [] episode_id = episode.episode filename = self.get_filename(episode_id) if os.path.isfile(filename): with open(filename) as f: episode = db.session.query(EpisodeModel).filter_by(series=series, episode=episode_id).first() for timed_text in Reader(f).read(): text = timed_text.text start = (timed_text.start - core.get_date()).total_seconds() end = (timed_text.end - core.get_date()).total_seconds() caption = CaptionModel(series, episode_id, start, end, text) captions.append(caption) db.session.add(caption) db.session.flush() db.session.commit() self.save_to_search(episode, captions) return { 'episode': episode_id, 'count': len(captions), }
def test_format_time(self): caption = core.Caption() caption.start = core.get_date(second=3, millisecond=490) caption.end = core.get_date(second=7, millisecond=430) time_info = self.writer.format_time(caption) self.assertEqual(time_info['start'], '3.490') self.assertEqual(time_info['end'], '7.430')
def test_update_from_start_and_end(self): start = core.get_date(second=3) end = core.get_date(second=3) self.caption.start = start self.caption.end = end duration = end - start self.assertEqual(self.caption.duration, duration)
def setUp(self): self.stringio = StringIO() self.writer = core.Writer(self.stringio) caption = core.Caption() caption.start = core.get_date(second=3) caption.end = core.get_date(second=7) caption.text = u"TEST TEXT" self.writer.add_caption(caption)
def test_read(self): captions = self.reader.read() self.assertTrue(captions is not None) self.assertEqual(len(captions), 4) first = captions[0] self.assertEqual(type(first.text), unicode) self.assertEqual(first.text, u">> FISHER: All right. So, let's begin.\nThis session is: Going Social\n") self.assertEqual(first.start, core.get_date(second=3, millisecond=490)) self.assertEqual(first.end, core.get_date(second=7, millisecond=430)) self.assertEqual(first.duration, timedelta(seconds=3, milliseconds=940))
def test_read(self): captions = self.reader.read() self.assertTrue(captions is not None) self.assertEqual(len(captions), 66) first = captions[0] self.assertEqual(type(first.text), unicode) self.assertEqual(first.text, u"Hi, I'm Emily from Nomensa") self.assertEqual(first.start, core.get_date(second=10)) self.assertEqual(first.end, core.get_date(second=12)) self.assertEqual(first.duration, timedelta(seconds=2))
def test_read(self): captions = self.reader.read() self.assertTrue(captions is not None) self.assertEqual(len(captions), 4) first = captions[0] self.assertEqual(type(first.text), unicode) self.assertEqual(first.text, u"Some text") self.assertEqual(first.start, core.get_date(second=0, millisecond=166)) self.assertEqual(first.end, core.get_date(second=0, millisecond=766)) fourth = captions[3] self.assertEqual(fourth.end, core.get_date(second=10, millisecond=0))
def test_read(self): captions = self.reader.read() self.assertTrue(captions is not None) self.assertEqual(len(captions), 4) first = captions[0] self.assertEqual(type(first.text), unicode) self.assertEqual( first.text, u">> FISHER: All right. So, let's begin.\nThis session is: Going Social\n" ) self.assertEqual(first.start, core.get_date(second=3, millisecond=490)) self.assertEqual(first.end, core.get_date(second=7, millisecond=430)) self.assertEqual(first.duration, timedelta(seconds=3, milliseconds=940))
def test_update_from_start_and_duration(self): start = core.get_date(second=3) duration = timedelta(seconds=3) self.caption.start = start self.caption.duration = duration end = start + duration self.assertEqual(self.caption.end, end)
def test_read(self): captions = self.reader.read() self.assertTrue(captions is not None) self.assertEqual(len(captions), 4) first = captions[0] self.assertEqual(type(first.text), unicode) self.assertEqual(first.text, u"FISHER: All right. So, let's begin.\nThis session is: Going Social\n") self.assertEqual(first.start, core.get_date(second=3, millisecond=490)) self.assertEqual(first.end, core.get_date(second=7, millisecond=430)) self.assertEqual(first.duration, timedelta(seconds=3, milliseconds=940)) third = captions[2] self.assertEqual(third.text, u"and this is Johann Hartmann,\nwe're presenting today.\n") fourth = captions[3] self.assertEqual(fourth.start, core.get_date(second=14, millisecond=9)) self.assertEqual(fourth.end, core.get_date(second=15, millisecond=889))
def test_get_date_default(self): date = core.get_date() self.assertEqual(date.year, 1900) self.assertEqual(date.month, 1) self.assertEqual(date.day, 1) self.assertEqual(date.hour, 0) self.assertEqual(date.minute, 0) self.assertEqual(date.second, 0)
def test_read(self): captions = self.reader.read() self.assertTrue(captions is not None) self.assertEqual(len(captions), 17) first = captions[0] last = captions[-1] self.assertEqual(type(first.text), unicode) self.assertEqual(first.text, u">> SILVER TV HISTORY, I GUESS,") self.assertEqual(first.start, core.get_date(second=24, millisecond=457)) self.assertEqual(first.end, core.get_date(second=27, millisecond=560)) self.assertEqual(first.duration, timedelta(seconds=3, milliseconds=103)) self.assertEqual(type(last.text), unicode) self.assertEqual(last.text, u"LITERALLY THOUSANDS AND") self.assertEqual(last.start, core.get_date(second=54, millisecond=854)) self.assertEqual(last.end, core.get_date(second=54, millisecond=854)) self.assertEqual(last.duration, timedelta(seconds=0, milliseconds=0))
def get_raw_time(self, utime, format="date"): if '.' in utime: second_f = float(utime) second = int(second_f) millisecond = int(1000 * (second_f - second)) else: second = int(utime) millisecond = 0 if format == "date": return core.get_date(second=second, millisecond=millisecond) else: return timedelta(seconds=second, milliseconds=millisecond)
def test_read(self): captions = self.reader.read() self.assertTrue(captions is not None) self.assertEqual(len(captions), 4) first = captions[0] self.assertEqual(type(first.text), unicode) self.assertEqual( first.text, u"FISHER: All right. So, let's begin.\nThis session is: Going Social\n" ) self.assertEqual(first.start, core.get_date(second=3, millisecond=490)) self.assertEqual(first.end, core.get_date(second=7, millisecond=430)) self.assertEqual(first.duration, timedelta(seconds=3, milliseconds=940)) third = captions[2] self.assertEqual( third.text, u"and this is Johann Hartmann,\nwe're presenting today.\n") fourth = captions[3] self.assertEqual(fourth.start, core.get_date(second=14, millisecond=9)) self.assertEqual(fourth.end, core.get_date(second=15, millisecond=889))
def test_get_date_millisecond(self): date = core.get_date(millisecond=940) self.assertEqual(date.second, 0) self.assertEqual(date.microsecond, 940000)
def test_set_end(self): end = core.get_date(second=3) self.caption.end = end self.assertEqual(self.caption.end, end)
def test_get_date_second(self): date = core.get_date(second=3600) self.assertEqual(date.hour, 1) self.assertEqual(date.minute, 0) self.assertEqual(date.second, 0)
def test_set_start(self): start = core.get_date(second=3) self.caption.start = start self.assertEqual(self.caption.start, start)