Skip to content

Commit

Permalink
Created the basic function of the YoutubeMysqlDatastore class #4
Browse files Browse the repository at this point in the history
  • Loading branch information
drkostas committed Jun 20, 2021
1 parent 616cdfd commit b892c4d
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 13 deletions.
2 changes: 1 addition & 1 deletion youbot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from youbot.fancy_logger import ColorizedLogger
from youbot.configuration import Configuration, validate_json_schema
from youbot.cloudstore import DropboxCloudstore
from youbot.datastore import MySqlDatastore
from youbot.datastore import YoutubeMySqlDatastore
from youbot.emailer import GmailEmailer
from youbot.youtube_utils import YoutubeManagerV3

Expand Down
122 changes: 110 additions & 12 deletions youbot/datastore/mysql_datastore.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import List, Tuple, Dict

from datetime import datetime
from mysql import connector as mysql_connector
import mysql.connector.cursor

Expand Down Expand Up @@ -198,6 +199,8 @@ def __exit__(self) -> None:


class YoutubeMySqlDatastore(MySqlDatastore):
CHANNEL_TABLE = 'channels'
COMMENTS_TABLE = 'comments'

def __init__(self, config: Dict) -> None:
"""
Expand All @@ -207,33 +210,128 @@ def __init__(self, config: Dict) -> None:
"""

super().__init__(config)
self.create_tables_if_not_exist()

def create_empty_tables(self):
def create_tables_if_not_exist(self):
channels_schema = \
"""id varchar(100) default '' not null,
"""
channel_id varchar(100) default '' not null,
username varchar(100) not null,
added_on varchar(100) not null,
last_commented varchar(100) not null,
priority int auto_increment,
channel_photo varchar(100) default '-1' null,
constraint id_pk PRIMARY KEY (id),
constraint id unique (id),
constraint id_pk PRIMARY KEY (channel_id),
constraint channel_id unique (channel_id),
constraint priority unique (priority),
constraint username unique (username)"""
comments_schema = \
"""
id varchar(100) not null,
link varchar(100) not null,
channel_id varchar(100) not null,
video_link varchar(100) not null,
comment varchar(255) not null,
timestamp varchar(100) not null,
comment_time varchar(100) not null,
like_count int default -1 null,
reply_count int default -1 null,
comment_id varchar(100) default '-1' null,
video_id varchar(100) default '-1' null,
comment_link varchar(100) default '-1' null,
constraint link_pk PRIMARY KEY (link),
constraint link unique (link),
constraint channel_id foreign key (id) references channels (id) on update cascade on delete cascade"""
constraint video_link_pk PRIMARY KEY (video_link),
constraint comment_link unique (comment_link),
constraint channel_id foreign key (channel_id) references channels (channel_id) on update cascade on delete cascade"""

self.create_table(table=self.CHANNEL_TABLE, schema=channels_schema)
self.create_table(table=self.COMMENTS_TABLE, schema=comments_schema)

def get_channels(self) -> List[Tuple]:
""" Retrieve all channels from the database. """

result = self.select_from_table(table=self.CHANNEL_TABLE)

return result

def add_channel(self, channel_data: Dict) -> None:
""" Insert the provided channel into the database"""

try:
self.insert_into_table(table=self.CHANNEL_TABLE, data=channel_data)
except mysql.connector.errors.IntegrityError as e:
logger.error(f"MySQL error: {e}")

def get_channel_by_id(self, ch_id: str) -> Tuple:
"""Retrieve a channel from the database by its ID
Args:
ch_id (str): The channel ID
"""

where_statement = f"id='{ch_id}'"
result = self.select_from_table(table=self.CHANNEL_TABLE, where=where_statement)
if len(result) > 1:
logger.warning("Duplicate channel retrieved from SELECT statement:{result}")
elif len(result) == 0:
result.append(())

return result[0]

def get_channel_by_username(self, ch_username: str) -> Tuple:
"""Retrieve a channel from the database by its Username
Args:
ch_username (str): The channel ID
"""

where_statement = f"username='{ch_username}'"
result = self.select_from_table(table=self.CHANNEL_TABLE, where=where_statement)
if len(result) > 1:
logger.warning("Duplicate channel retrieved from SELECT statement:{result}")
elif len(result) == 0:
result.append(())

return result[0]

def remove_channel_from_id(self, ch_id: str) -> None:
"""Retrieve a channel from the database by its ID
Args:
ch_id (str): The channel ID
"""

where_statement = f"id='{ch_id}'"
self.delete_from_table(table=self.CHANNEL_TABLE, where=where_statement)

def remove_channel_by_username(self, ch_username: str) -> None:
"""Delete a channel from the database by its Username
Args:
ch_username (str): The channel ID
"""

where_statement = f"username='{ch_username}'"
self.delete_from_table(table=self.CHANNEL_TABLE, where=where_statement)

def add_comment(self, ch_id: str, video_link: str, comment_text: str) -> None:
""" TODO: check the case where a comment contains single quotes
Add comment data and update the `last_commented` channel column.
Args:
ch_id:
video_link:
comment_text:
"""

datetime_now = datetime.utcnow().isoformat()
comments_data = {'channel_id': ch_id,
'video_link': video_link,
'comment': comment_text,
'comment_time': datetime_now}
update_data = {'last_commented': datetime_now}
where_statement = f"channel_id='{ch_id}'"

try:
self.insert_into_table(self.COMMENTS_TABLE, data=comments_data)
# Update Channel's last_commented timestamp
self.update_table(table=self.CHANNEL_TABLE, set_data=update_data, where=where_statement)
except mysql.connector.errors.IntegrityError as e:
logger.error(f"MySQL Error: {e}")

self.create_table(table='channels', schema=channels_schema)
self.create_table(table='comments', schema=comments_schema)

0 comments on commit b892c4d

Please sign in to comment.