Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #/usr/bin/env python3
- """
- For further information read the instructions here:
- https://github.com/uehara1414/youtube_livechat_messages
- You have to install this dependency to use this code.
- In addition you need an Google Application, YouTube v3 API Access,
- and OAUTH. You have to follow the orders and download the credentials.json
- Video about this: https://youtu.be/V2M6yPj7yo4
- """
- import argparse
- import sys
- import re
- from textwrap import TextWrapper
- from urllib.parse import urlparse, parse_qs
- from youtube_livechat_messages import get_credentials, API, EventType
- VERY_STRICT = (
- range(0x1f100, 0x1f9A3),
- )
- NOT_ALLOWED = (
- range(0x1F600, 0x1F650), # emoticons
- range(0x1F300, 0x1F600), # symbols & pictographs
- range(0x1F680, 0x1F700), # transport & map symbols
- range(0x1F1E0, 0x1F200), # flags (iOS)
- )
- def emoji_filter(text):
- text = ''.join(_emoji_filter(text))
- return re.sub(r':\w+:', '', text)
- def _emoji_filter(text):
- for char in text:
- for char_range in VERY_STRICT:
- if ord(char) in char_range:
- yield ''
- break
- else:
- yield char
- def name_shortener(name):
- if len(name) > 13:
- return name[:10] + "...| "
- return name.ljust(13) + "| "
- def print_messages(api, credentials, video_id, cleanup):
- indent = " " * 13 + '| '
- w = TextWrapper(subsequent_indent=indent, width=79-15)
- for message in api.cursor(video_id=video_id).text_messages():
- name = name_shortener(message.author.display_name)
- if cleanup:
- msg = emoji_filter(message.display_message)
- else:
- msg = message.display_message
- print(name, end='')
- for line in w.wrap(msg):
- print(line)
- def main(video_id, *, cleanup=False):
- video_url = urlparse(video_id)
- if 'youtube' in video_url.netloc:
- query = parse_qs(video_url.query)
- if 'v' in query:
- video_id = query.get('v').pop()
- credentials = get_credentials(client_secret_path='client_secret.json')
- api = API(credentials=credentials) # or api = API(access_token)
- print_messages(api, credentials, video_id, cleanup)
- if __name__ == '__main__':
- parser = argparse.ArgumentParser()
- parser.add_argument('video_id', help='Video ID of LiveChat')
- parser.add_argument('-e', help='Emoji filter', action='store_true')
- args = parser.parse_args()
- try:
- main(args.video_id, cleanup=args.e)
- except KeyboardInterrupt:
- pass
Add Comment
Please, Sign In to add comment