From 91e7273142e041a46968bf62bec17262f888436a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petter=20Kraab=C3=B8l?= Date: Sun, 9 Jun 2019 15:21:31 +0200 Subject: [PATCH] Filter chat by usernames and message content. --- setup.py | 2 +- tcd/__init__.py | 4 +++- tcd/arguments.py | 7 +++++++ tcd/downloader.py | 18 ++++++++++++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index ea92c97..ef8abc0 100644 --- a/setup.py +++ b/setup.py @@ -44,6 +44,6 @@ test_suite='tests', tests_require=test_requirements, url='https://github.com/PetterKraabol/Twitch-Chat-Downloader', - version='3.0.9', + version='3.1.0', zip_safe=True, ) diff --git a/tcd/__init__.py b/tcd/__init__.py index 01f9330..02f5829 100644 --- a/tcd/__init__.py +++ b/tcd/__init__.py @@ -9,7 +9,7 @@ from .settings import Settings __name__: str = 'tcd' -__version__: str = '3.0.9' +__version__: str = '3.1.0' __all__: List[Callable] = [Arguments, Settings, Downloader, Logger, Log] @@ -18,6 +18,7 @@ def main(): parser = argparse.ArgumentParser(description=f'Twitch Chat Downloader {__version__}') parser.add_argument('-v', f'--{Arguments.Name.VIDEO}', type=str, help='Video IDs separated by commas') parser.add_argument('-c', f'--{Arguments.Name.CHANNEL}', type=str, help='Channel names separated by commas') + parser.add_argument('-u', f'--{Arguments.Name.USER}', type=str, help='Messages from users, separated by commas') parser.add_argument(f'--{Arguments.Name.FIRST}', type=int, default=5, help='Download chat from the last n VODs') parser.add_argument(f'--{Arguments.Name.CLIENT_ID.replace("_", "-")}', type=str, help='Twitch client ID') parser.add_argument(f'--{Arguments.Name.VERBOSE}', action='store_true', help='Verbose output') @@ -25,6 +26,7 @@ def main(): parser.add_argument('-o', f'--{Arguments.Name.OUTPUT}', type=str, help='Output directory', default='./') parser.add_argument('-f', f'--{Arguments.Name.FORMAT}', type=str, help='Message format', default='default') parser.add_argument(f'--{Arguments.Name.TIMEZONE}', type=str, help='Timezone name') + parser.add_argument(f'--includes', type=str, help='Download messages includes specified text') parser.add_argument(f'--{Arguments.Name.INIT}', action='store_true', help='Script setup') parser.add_argument(f'--{Arguments.Name.VERSION}', action='store_true', help='Settings version') parser.add_argument(f'--{Arguments.Name.FORMATS}', action='store_true', help='List available formats') diff --git a/tcd/arguments.py b/tcd/arguments.py index 9c93ac4..261cdca 100644 --- a/tcd/arguments.py +++ b/tcd/arguments.py @@ -20,6 +20,8 @@ class Name: OUTPUT: str = 'output' CLIENT_ID: str = 'client_id' CHANNEL: str = 'channel' + USER: str = 'user' + INCLUDES: str = 'includes' FIRST: str = 'first' VIDEO: str = 'video' FORMAT: str = 'format' @@ -52,11 +54,13 @@ def __init__(self, arguments: Optional[Dict[str, Union[str, bool, int]]] = None) self.client_id: Optional[str] = arguments[Arguments.Name.CLIENT_ID] self.first: Optional[int] = arguments[Arguments.Name.FIRST] self.timezone: Optional[str] = arguments[Arguments.Name.TIMEZONE] + self.includes: Optional[str] = arguments[Arguments.Name.INCLUDES] # Arguments that require some formatting self.video_ids: List[int] = [] self.formats: List[str] = [] self.channels: List[str] = [] + self.users: List[str] = [] if arguments[Arguments.Name.VIDEO]: self.video_ids = [int(video_id) for video_id in arguments[Arguments.Name.VIDEO].lower().split(',')] @@ -66,3 +70,6 @@ def __init__(self, arguments: Optional[Dict[str, Union[str, bool, int]]] = None) if arguments[Arguments.Name.CHANNEL]: self.channels = arguments[Arguments.Name.CHANNEL].lower().split(',') + + if arguments[Arguments.Name.USER]: + self.users = arguments[Arguments.Name.USER].lower().split(',') diff --git a/tcd/downloader.py b/tcd/downloader.py index ad89020..dd85a1c 100644 --- a/tcd/downloader.py +++ b/tcd/downloader.py @@ -97,6 +97,16 @@ def video(self, video: Video) -> None: } for comment in video.comments(): + + # Skip unspecified users if a list is provided. + if Arguments().users and comment.commenter.name.lower() not in Arguments().users: + continue + + # If specified, only include messages that include a specified string + if Arguments().includes and Arguments().includes not in comment.message.body.lower(): + continue + + # Add comment to dictionary data['comments'].append(comment.data) # Ignore comments that were posted after the VOD finished @@ -130,6 +140,14 @@ def video(self, video: Video) -> None: # For every comment in video for formatted_comment, comment in comment_tuple: + # Skip unspecified users if a list is provided. + if Arguments().users and comment.commenter.name.lower() not in Arguments().users: + continue + + # If specified, only include messages that include a specified string + if Arguments().includes and Arguments().includes.lower() not in comment.message.body.lower(): + continue + # Ignore comments that were posted after the VOD finished if Settings().config['formats'][format_name].get('comments', {}).get('ignore_new_comments', False): comment_date = dateutil.parser.parse(comment.created_at)