DeaD_EyE

yt_livechat (read_only)

Jun 27th, 2019
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.54 KB | None | 0 0
  1. #/usr/bin/env python3
  2. """
  3. For further information read the instructions here:
  4. https://github.com/uehara1414/youtube_livechat_messages
  5.  
  6. You have to install this dependency to use this code.
  7. In addition you need an Google Application, YouTube v3 API Access,
  8. and OAUTH. You have to follow the orders and download the credentials.json
  9.  
  10. Video about this: https://youtu.be/V2M6yPj7yo4
  11. """
  12.  
  13. import argparse
  14. import sys
  15. import re
  16. from textwrap import TextWrapper
  17. from urllib.parse import urlparse, parse_qs
  18.  
  19. from youtube_livechat_messages import get_credentials, API, EventType
  20.  
  21. VERY_STRICT = (
  22.     range(0x1f100, 0x1f9A3),
  23. )
  24.  
  25. NOT_ALLOWED = (
  26.     range(0x1F600, 0x1F650),  # emoticons
  27.     range(0x1F300, 0x1F600),  # symbols & pictographs
  28.     range(0x1F680, 0x1F700),  # transport & map symbols
  29.     range(0x1F1E0, 0x1F200),  # flags (iOS)
  30. )
  31.  
  32.  
  33. def emoji_filter(text):
  34.     text = ''.join(_emoji_filter(text))
  35.     return re.sub(r':\w+:', '', text)
  36.  
  37.  
  38. def _emoji_filter(text):
  39.     for char in text:
  40.         for char_range in VERY_STRICT:
  41.             if ord(char) in char_range:
  42.                 yield ''
  43.                 break
  44.         else:
  45.             yield char
  46.  
  47.  
  48. def name_shortener(name):
  49.     if len(name) > 13:
  50.         return name[:10] + "...| "
  51.     return name.ljust(13) + "| "
  52.  
  53.  
  54. def print_messages(api, credentials, video_id, cleanup):
  55.     indent = " " * 13 + '| '
  56.     w = TextWrapper(subsequent_indent=indent, width=79-15)
  57.     for message in api.cursor(video_id=video_id).text_messages():
  58.         name = name_shortener(message.author.display_name)
  59.         if cleanup:
  60.             msg = emoji_filter(message.display_message)
  61.         else:
  62.             msg = message.display_message
  63.         print(name, end='')
  64.         for line in w.wrap(msg):
  65.             print(line)
  66.  
  67.  
  68. def main(video_id, *, cleanup=False):
  69.     video_url = urlparse(video_id)
  70.     if 'youtube' in video_url.netloc:
  71.         query = parse_qs(video_url.query)
  72.         if 'v' in query:
  73.             video_id = query.get('v').pop()
  74.     credentials = get_credentials(client_secret_path='client_secret.json')
  75.     api = API(credentials=credentials)  # or api = API(access_token)
  76.     print_messages(api, credentials, video_id, cleanup)
  77.  
  78.  
  79. if __name__ == '__main__':
  80.     parser = argparse.ArgumentParser()
  81.     parser.add_argument('video_id', help='Video ID of LiveChat')
  82.     parser.add_argument('-e', help='Emoji filter', action='store_true')
  83.     args = parser.parse_args()
  84.     try:
  85.         main(args.video_id, cleanup=args.e)
  86.     except KeyboardInterrupt:
  87.         pass
Add Comment
Please, Sign In to add comment