|
| 1 | +#coding=utf-8 |
| 2 | + |
| 3 | +from __future__ import unicode_literals |
| 4 | + |
| 5 | +import os |
| 6 | +import re |
| 7 | + |
| 8 | +import ui |
| 9 | +from logHandler import log |
| 10 | + |
| 11 | +from .sound import play |
| 12 | +from .config import conf |
| 13 | +from .subtitle_alg import SubtitleAlg, SupportStatus |
| 14 | +from .object_finder import find, search |
| 15 | +from .compatible import role |
| 16 | + |
| 17 | +class Twitch(SubtitleAlg): |
| 18 | + info = { |
| 19 | + 'name': 'Twitch', |
| 20 | + 'url': 'https://www.twitch.tv/', |
| 21 | + 'status': SupportStatus.supported, |
| 22 | + } |
| 23 | + def __init__(self, *args, **kwargs): |
| 24 | + super(Twitch, self).__init__(*args, **kwargs) |
| 25 | + self.chatRoom = None |
| 26 | + self.chatContainer = None |
| 27 | + self.chatObject = None |
| 28 | + self.chatSender = '' |
| 29 | + self.chatSenderIsOwner = False |
| 30 | + self.chatSenderIsAdmin = '' |
| 31 | + self.chatSenderIsVerified = '' |
| 32 | + self.chat = '' |
| 33 | + self.chatContainerSearchObject = None |
| 34 | + self.chatBannerSearchObject = None |
| 35 | + self.chatSearchObject = None |
| 36 | + |
| 37 | + def getVideoPlayer(self): |
| 38 | + obj = self.main.focusObject |
| 39 | + return find(obj, 'parent', 'tag', 'main') |
| 40 | + |
| 41 | + def getSubtitleContainer(self): |
| 42 | + self.getChatContainer() |
| 43 | + return True |
| 44 | + |
| 45 | + def getChatContainer(self): |
| 46 | + obj = self.main.videoPlayer.next |
| 47 | + |
| 48 | + self.chatRoom = None |
| 49 | + self.chatContainer = None |
| 50 | + self.chatObject = None |
| 51 | + if self.chatContainerSearchObject: |
| 52 | + self.chatContainerSearchObject.cancel() |
| 53 | + |
| 54 | + self.chatContainerSearchObject = search(obj, lambda obj: obj.role == role('list'), self.onFoundChatContainer) |
| 55 | + |
| 56 | + if self.chatBannerSearchObject: |
| 57 | + self.chatBannerSearchObject.cancel() |
| 58 | + |
| 59 | + if self.chatSearchObject: |
| 60 | + self.chatSearchObject.cancel() |
| 61 | + |
| 62 | + |
| 63 | + def onFoundChatContainer(self, obj): |
| 64 | + self.chatContainer = obj |
| 65 | + #self.readChatBanner() |
| 66 | + |
| 67 | + def getSubtitle(self): |
| 68 | + return '' |
| 69 | + |
| 70 | + def onReadingSubtitle(self): |
| 71 | + self.readChat() |
| 72 | + |
| 73 | + |
| 74 | + def readChat(self): |
| 75 | + if not self.chatContainer: |
| 76 | + return |
| 77 | + |
| 78 | + if self.chatSearchObject and not self.chatSearchObject.isStopped: |
| 79 | + return |
| 80 | + |
| 81 | + self.chatObject = self.chatContainer.lastChild if not self.chatObject or not self.chatObject.role else self.chatObject |
| 82 | + nextChat = self.chatObject.next |
| 83 | + if nextChat is None: |
| 84 | + return |
| 85 | + |
| 86 | + self.chatObject = nextChat |
| 87 | + self.chatSender = '' |
| 88 | + self.chatSenderIsOwner = False |
| 89 | + self.chatSenderIsAdmin = '' |
| 90 | + self.chatSenderIsVerified = '' |
| 91 | + |
| 92 | + chat = self.chatObject.firstChild.next.firstChild |
| 93 | + chat = find(chat, 'next', 'name', ':') |
| 94 | + text = '' |
| 95 | + while chat: |
| 96 | + chat = chat.next |
| 97 | + text += getattr(chat, 'name', '') |
| 98 | + |
| 99 | + text = text.strip() |
| 100 | + if not text or text == self.chat: |
| 101 | + return |
| 102 | + |
| 103 | + self.chat = text |
| 104 | + ui.message(text) |
| 105 | + |
| 106 | + def chatCondition(self, obj): |
| 107 | + if 'ytd-sponsorships-live-chat-gift-redemption-announcement-renderer' in obj.IA2Attributes.get('class'): |
| 108 | + return |
| 109 | + |
| 110 | + matchIds = [ |
| 111 | + 'message', |
| 112 | + 'primary-text', |
| 113 | + 'author-name', |
| 114 | + 'chat-badges', |
| 115 | + ] |
| 116 | + |
| 117 | + return obj.IA2Attributes.get('id') in matchIds |
| 118 | + |
| 119 | + def onFoundChatObject(self, chat): |
| 120 | + id = chat.IA2Attributes.get('id') |
| 121 | + if id == 'author-name': |
| 122 | + name = chat.firstChild.name |
| 123 | + self.chatSender = name if name else '' |
| 124 | + self.chatSenderIsOwner = 'owner' in chat.IA2Attributes.get('class', '') |
| 125 | + verified = chat.firstChild.next |
| 126 | + if verified: |
| 127 | + self.chatSenderIsVerified = verified.firstChild.name |
| 128 | + |
| 129 | + return |
| 130 | + |
| 131 | + if id == 'chat-badges': |
| 132 | + if chat.childCount >= 2 or chat.firstChild.firstChild.firstChild.role != role('GRAPHIC'): |
| 133 | + name = chat.firstChild.name |
| 134 | + self.chatSenderIsAdmin = name if name else '' |
| 135 | + |
| 136 | + return |
| 137 | + |
| 138 | + self.chatSearchObject.cancel() |
| 139 | + |
| 140 | + if conf['onlyReadManagersChat'] and not self.chatSenderIsOwner and not self.chatSenderIsAdmin: |
| 141 | + return |
| 142 | + |
| 143 | + chat = getattr(chat, 'firstChild', None) |
| 144 | + if not chat: |
| 145 | + return |
| 146 | + |
| 147 | + text = '' |
| 148 | + loopingChat = chat |
| 149 | + while loopingChat: |
| 150 | + chat = loopingChat |
| 151 | + loopingChat = loopingChat.next |
| 152 | + if conf['omitChatGraphic'] and chat.role == role('graphic'): |
| 153 | + continue |
| 154 | + |
| 155 | + text += chat.name if chat.name else '' |
| 156 | + |
| 157 | + |
| 158 | + text = text.strip() |
| 159 | + if not text: |
| 160 | + return |
| 161 | + |
| 162 | + if text == self.chat: |
| 163 | + return |
| 164 | + |
| 165 | + self.chat = text |
| 166 | + |
| 167 | + sender = '{name}。\r\n{isVerified}。\r\n{isAdmin}。\r\n'.format(name=self.chatSender, isVerified=self.chatSenderIsVerified, isAdmin=self.chatSenderIsAdmin) |
| 168 | + if conf['readChatSender'] or self.chatSenderIsOwner or self.chatSenderIsVerified or self.chatSenderIsAdmin: |
| 169 | + text = sender + text |
| 170 | + |
| 171 | + ui.message(text) |
| 172 | + |
| 173 | + |
| 174 | + def readChatBanner(self): |
| 175 | + if not self.chatContainer: |
| 176 | + return |
| 177 | + |
| 178 | + obj = self.chatContainer.parent.previous |
| 179 | + banner = None |
| 180 | + while obj: |
| 181 | + banner = find(obj, 'firstChild', 'id', 'banner-container') |
| 182 | + if banner: |
| 183 | + break |
| 184 | + |
| 185 | + obj = obj.previous |
| 186 | + |
| 187 | + if not banner: |
| 188 | + return |
| 189 | + |
| 190 | + self.chatBannerSearchObject = search(banner, lambda obj: bool(obj.name) and obj.role != role('LINK'), lambda banner: ui.message(banner.name if banner.role != role('BUTTON') else '聊天室橫幅'), continueOnFound=True) |
| 191 | + |
| 192 | + def readVoting(self): |
| 193 | + if not self.chatRoom: |
| 194 | + return |
| 195 | + |
| 196 | + votingObj = self.chatRoom.lastChild |
| 197 | + votingObj = find(votingObj, 'firstChild', 'class', 'style-scope yt-live-chat-poll-renderer') |
| 198 | + if bool(votingObj) != self.voting: |
| 199 | + self.voting = bool(votingObj) |
| 200 | + if self.voting: |
| 201 | + ui.message(_('意見調查:')) |
| 202 | + search(votingObj, lambda obj: True, self.onSearchVoting, continueOnFound=True) |
| 203 | + |
| 204 | + |
| 205 | + |
| 206 | + def onSearchVoting(self, obj): |
| 207 | + msg = getattr(obj, 'name', '') or '' |
| 208 | + # 掠過有 id 或 name 為 • 的原件 |
| 209 | + if msg == ' • ' or obj.IA2Attributes.get('id'): |
| 210 | + return |
| 211 | + |
| 212 | + if msg: |
| 213 | + ui.message(msg) |
| 214 | + |
| 215 | + |
0 commit comments