Skip to content

Commit

Permalink
Filter chat by usernames and message content.
Browse files Browse the repository at this point in the history
  • Loading branch information
PetterKraabol committed Jun 9, 2019
1 parent 058dfc2 commit 91e7273
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
4 changes: 3 additions & 1 deletion tcd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]


Expand All @@ -18,13 +18,15 @@ 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')
parser.add_argument('-q', f'--{Arguments.Name.QUIET}', action='store_true')
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')
Expand Down
7 changes: 7 additions & 0 deletions tcd/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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(',')]
Expand All @@ -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(',')
18 changes: 18 additions & 0 deletions tcd/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 91e7273

Please sign in to comment.