forked from actlaboratory/TCV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.py
316 lines (297 loc) · 13.1 KB
/
manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# -*- coding: utf-8 -*-
# manager
import twitcasting.connection
import datetime
import wx
import globalVars
import simpleDialog
import pathlib
from twitcasting.accessToken import accessToken
import twitcasting.twitcasting
evtComment = 0
evtLiveInfo = 1
evtCountDown = 2
evtTyping = 3
first = 0
update = 1
commentTimerInterval = 5000
liveInfoTimerInterval = 10000
countDownTimerInterval = 1000
typingTimerInterval = 5000
historyData = pathlib.Path("history.dat")
favoritesData = pathlib.Path("favorites.dat")
class manager:
def __init__(self, MainView):
self.MainView = MainView
self.evtHandler = wx.EvtHandler()
self.evtHandler.Bind(wx.EVT_TIMER, self.timer)
if historyData.exists() == False:
historyData.touch()
self.history = historyData.read_text().split("\n")
if len(self.history) == 1 and self.history[0] == "":
del self.history[0]
if favoritesData.exists() == False:
favoritesData.touch()
self.favorites = favoritesData.read_text().split("\n")
if len(self.favorites) == 1 and self.favorites[0] == "":
del self.favorites[0]
self.myAccount = []
self.myAccount.append(twitcasting.twitcasting.VerifyCredentials()["user"])
def connect(self, userId):
self.connection = twitcasting.connection.connection(userId)
if self.connection.connected == False:
simpleDialog.errorDialog(_("指定されたユーザが見つかりません。"))
else:
globalVars.app.say(userId)
if userId not in self.history:
self.history.insert(0, userId)
elif userId in self.history:
del self.history[self.history.index(userId)]
self.history.insert(0, userId)
historyData.write_text("\n".join(self.history))
self.countDownTimer = wx.Timer(self.evtHandler, evtCountDown)
if self.connection.isLive == True:
globalVars.app.say(_("接続。現在配信中。"))
self.resetTimer()
self.countDownTimer.Start(countDownTimerInterval)
globalVars.app.say(_("タイマー開始。"))
else:
globalVars.app.say(_("接続。現在オフライン。"))
self.resetTimer()
initialCommentCount = globalVars.app.config.getint("general", "initialCommentCount", 50)
self.initialComments = self.connection.getInitialComment(initialCommentCount)
self.commentTimer = wx.Timer(self.evtHandler, evtComment)
self.commentTimer.Start(commentTimerInterval)
self.addComments(self.initialComments, first)
self.liveInfoTimer = wx.Timer(self.evtHandler, evtLiveInfo)
self.liveInfoTimer.Start(liveInfoTimerInterval)
self.createLiveInfoList(first)
self.oldCoins = self.connection.coins
self.oldViewers = self.connection.viewers
self.oldIsLive = self.connection.isLive
self.oldMovieId = self.connection.movieId
self.oldSubtitle = self.connection.subtitle
self.oldItem = self.connection.item
self.createItemList(first)
self.typingTimer = wx.Timer(self.evtHandler, evtTyping)
self.typingTimer.Start(typingTimerInterval)
def addComments(self, commentList, mode):
for i in commentList:
result = {
"dispname": i["from_user"]["name"],
"message": i["message"],
"time": datetime.datetime.fromtimestamp(i["created"]).strftime("%H:%M:%S"),
"user": i["from_user"]["screen_id"]
}
self.MainView.commentList.InsertItem(0 , "")
self.MainView.commentList.SetItem(0, 0, result["dispname"])
self.MainView.commentList.SetItem(0, 1, result["message"])
self.MainView.commentList.SetItem(0, 2, result["time"])
self.MainView.commentList.SetItem(0, 3, result["user"])
if mode == update:
commentReadMode = globalVars.app.config.getint("autoReadingOptions", "announceReceivedComments", 1)
if commentReadMode == 2:
for j in self.myAccount:
if i["from_user"]["id"] == j["id"]:
return
if commentReadMode != 0:
announceText = globalVars.app.config["autoReadingOptions"]["receivedCommentsAnnouncement"]
announceText = announceText.replace("$dispname", result["dispname"])
announceText = announceText.replace("$message", result["message"])
announceText = announceText.replace("$time", result["time"])
announceText = announceText.replace("$user", result["user"])
globalVars.app.say(announceText)
def createLiveInfoList(self, mode):
if self.connection.hasMovieId == False:
return
result = [
_("経過時間:%(elapsedTime)s、残り時間:%(remainingTime)s") %{"elapsedTime": self.formatTime(self.elapsedTime).strftime("%H:%M:%S"), "remainingTime": self.formatTime(self.remainingTime).strftime("%H:%M:%S")},
_("タイトル:%(title)s") %{"title": self.connection.movieInfo["movie"]["title"]},
_("テロップ:%(subtitle)s") %{"subtitle": self.connection.movieInfo["movie"]["subtitle"]},
_("閲覧:現在%(current)d人、合計%(total)d人") %{"current": self.connection.movieInfo["movie"]["current_view_count"], "total": self.connection.movieInfo["movie"]["total_view_count"]},
_("カテゴリ:%(category)s") %{"category": self.connection.categoryName},
_("コメント数:%(number)d") %{"number": self.connection.movieInfo["movie"]["comment_count"]},
self.connection.movieInfo["broadcaster"]["screen_id"]
]
if self.connection.movieInfo["movie"]["is_live"] == True:
result.insert(0, _("現在配信中"))
else:
result.insert(0, _("オフライン"))
if self.connection.movieInfo["movie"]["is_collabo"] == True:
result.insert(-1, _("コラボ可能"))
else:
result.insert(-1, _("コラボ不可"))
for i in range(0, len(result)):
result[i] = result[i].replace("None", _("なし"))
if mode == first:
for i in range(0, len(result)):
self.MainView.liveInfo.InsertItem(i, result[i])
elif mode == update:
for i in range(0, len(result)):
bool = result[i] == self.MainView.liveInfo.GetItemText(i)
if bool == False:
self.MainView.liveInfo.SetItemText(i, result[i])
def createItemList(self, mode):
result = []
for i in self.connection.item:
result.append(i["name"] + ":" + str(i["count"]))
result.sort()
if mode == update:
self.MainView.itemList.ClearAll()
for i in range(0, len(result)):
self.MainView.itemList.InsertItem(i, result[i])
def postComment(self, commentBody):
if len(commentBody) == 0:
simpleDialog.errorDialog(_("コメントが入力されていません。"))
return False
elif len(commentBody) > 140:
simpleDialog.errorDialog(_("140字を超えるコメントは投稿できません。現在%s文字のコメントが入力されています。") %(str(len(commentBody))))
return False
result = self.connection.postComment(commentBody)
if "error" in result:
if "comment" in result["error"]["details"] and "length" in result["error"]["details"]["comment"] :
simpleDialog.errorDialog(_("コメント文字数が140字を超えているため、コメントを投稿できません。"))
return False
else:
simpleDialog.errorDialog(_("エラーが発生しました。詳細:%(detail)s") %{"detail": str(result["error"])})
return False
else:
return True
def formatTime(self, second):
time = datetime.time(hour = int(second / 3600), minute = int(second % 3600 / 60), second = int(second % 3600 % 60))
return time
def deleteComment(self):
selected = self.MainView.commentList.GetFocusedItem()
result = self.connection.deleteComment(self.connection.comments[selected])
if result == False:
simpleDialog.errorDialog(_("コメントの削除に失敗しました。このコメントを削除する権限がありません。"))
else:
del self.connection.comments[selected]
self.MainView.commentList.DeleteItem(selected)
def resetTimer(self):
globalVars.app.say(_("タイマーリセット。"))
if self.connection.isLive == True:
self.elapsedTime = self.connection.movieInfo["movie"]["duration"]
self.remainingTime = 1800 - self.elapsedTime % 1800 + int(self.connection.coins / 5) * 1800
if self.elapsedTime + self.remainingTime > 14400:
self.remainingTime = 14400 - self.elapsedTime
globalVars.app.say(_("残り時間:%(remainingTime)s。") %{"remainingTime": self.formatTime(self.remainingTime).strftime("%H:%M:%S")})
elif self.connection.isLive == False:
self.elapsedTime = 0
self.remainingTime = 0
def clearHistory(self):
self.history.clear()
historyData.write_text("\n".join(self.history))
def addFavorites(self):
self.favorites.insert(0, self.connection.userId.lower())
self.favorites.sort()
favoritesData.write_text("\n".join(self.favorites))
def deleteFavorites(self, index):
del self.favorites[index]
self.favorites.sort()
favoritesData.write_text("\n".join(self.favorites))
def clearFavorites(self):
self.favorites.clear()
favoritesData.write_text("\n".join(self.favorites))
def timer(self, event):
timer = event.GetTimer()
id = timer.GetId()
if id == evtComment:
newComments = self.connection.getComment()
self.addComments(newComments, update)
elif id == evtLiveInfo:
self.connection.update()
self.newIsLive = self.connection.isLive
if self.oldIsLive == True and self.newIsLive == False:
globalVars.app.say(_("ライブ終了。"))
self.countDownTimer.Stop()
self.resetTimer()
self.commentTimer.Stop()
elif self.oldIsLive == False and self.newIsLive == True:
globalVars.app.say(_("ライブ開始。"))
self.countDownTimer.Start(countDownTimerInterval)
self.commentTimer.Start(commentTimerInterval)
self.oldIsLive = self.newIsLive
self.newSubtitle = self.connection.subtitle
if self.newSubtitle != self.oldSubtitle:
if self.newSubtitle == None:
globalVars.app.say(_("テロップ削除"))
else:
globalVars.app.say(_("テロップ変更。"))
globalVars.app.say(self.newSubtitle)
self.oldSubtitle = self.newSubtitle
self.newCoins = self.connection.coins
if self.newCoins != self.oldCoins:
if self.newCoins < self.oldCoins:
globalVars.app.say(_("コイン消費"))
globalVars.app.say(_("コイン%(coins)d枚") %{"coins": self.newCoins})
self.resetTimer()
self.oldCoins = self.newCoins
self.newMovieId = self.connection.movieId
if self.newMovieId != self.oldMovieId:
if self.connection.isLive == True:
globalVars.app.say(_("次のライブが開始されました。"))
self.resetTimer()
self.oldMovieId = self.newMovieId
self.newViewers = self.connection.viewers
announceViewers = globalVars.app.config.getboolean("autoReadingOptions", "announceViewers", True)
if announceViewers == True:
if self.newViewers < self.oldViewers:
viewersInfo = globalVars.app.config["autoReadingOptions"]["viewersDecreasedAnnouncement"]
viewersInfo = viewersInfo.replace("$viewers", str(self.newViewers))
globalVars.app.say(viewersInfo)
elif self.newViewers > self.oldViewers:
viewersInfo = globalVars.app.config["autoReadingOptions"]["viewersIncreasedAnnouncement"]
viewersInfo = viewersInfo.replace("$viewers", str(self.newViewers))
globalVars.app.say(viewersInfo)
self.oldViewers = self.newViewers
self.createLiveInfoList(update)
self.newItem = self.connection.item
receivedItem = []
for new in self.newItem:
#if new["name"] not in self.oldItem:
#receivedItem.append({"id": new["id"], "name": new["name"], "count": new["count"]})
for old in self.oldItem:
if new["name"] == old["name"] and new["count"] > old["count"]:
receivedItem.append({"id": new["id"], "name": new["name"], "count": new["count"] - old["count"]})
for i in receivedItem:
id = i["id"]
name = i["name"]
count = i["count"]
users = self.connection.getItemPostedUser(id, count)
sameUser = False
for k in range(1, len(users) - 1):
if users[0] == users[k]:
sameUser = True
announceReceivedItems = globalVars.app.config.getboolean("autoReadingOptions", "announceReceivedItems", True)
if announceReceivedItems == True:
if sameUser == True:
globalVars.app.say(_("%sさんから%sをもらいました。") %(users[0], name))
else:
globalVars.app.say(_("%sさんなどから%sをもらいました。") %(users[0], name))
self.oldItem = self.newItem
self.createItemList(update)
elif id == evtCountDown:
self.elapsedTime += 1
self.remainingTime -= 1
if self.remainingTime % 1800 == 900:
globalVars.app.say(_("残り15分。"))
if self.remainingTime % 1800 == 300:
globalVars.app.say(_("残り5分。"))
if self.remainingTime % 1800 == 180:
globalVars.app.say(_("残り3分。"))
if self.remainingTime % 1800 == 60:
globalVars.app.say(_("残り1分"))
if self.remainingTime % 1800 == 30:
globalVars.app.say(_("残り30秒。"))
if self.remainingTime % 1800 == 10:
globalVars.app.say(_("残り10秒。"))
if self.remainingTime % 1800 == 0:
globalVars.app.say(_("30分経過。"))
self.MainView.liveInfo.SetItemText(1, _("経過時間:%(elapsedTime)s、残り時間:%(remainingTime)s") %{"elapsedTime": self.formatTime(self.elapsedTime).strftime("%H:%M:%S"), "remainingTime": self.formatTime(self.remainingTime).strftime("%H:%M:%S")})
elif id == evtTyping:
typingUser = self.connection.getTypingUser()
announceTypingUser = globalVars.app.config.getboolean("autoReadingOptions", "announceTypingUser", False)
if announceTypingUser == True:
if typingUser != "":
globalVars.app.say(_("%sさんが入力中") %(typingUser))