File size: 1,114 Bytes
7288748
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import json
import sqlite3
from typing import List, Tuple

from utils import accepts_types
from video import YoutubeVideo

class SQLiteBatchVideoStorer:
    """This is class is responsible to insert batch video entries in the db."""
    
    def __init__(self, table: str = "video"):
        self.table = table
    
    @accepts_types(sqlite3.Cursor, list)
    def store(self,
              db_cursor: sqlite3.Cursor,
              videos: List[YoutubeVideo]) -> None:
        """Batch insert list of videos in the 'video' table of the db."""
        video_list = self._convert_videos_to_list(videos)
        db_cursor.executemany(f"INSERT INTO {self.table}(channel_name, url, title, description, transcription, segments) VALUES(?, ?, ?, ?, ?, ?)", 
                              video_list)
        
    @staticmethod 
    def _convert_videos_to_list(videos: List[YoutubeVideo]) -> List[Tuple[str, str, str, str, str, str]]:
        for video in videos:
            # TODO: Find better way to solve this
            video.segments = json.dumps(video.segments)
        return [video.to_tuple() for video in videos]