forked from QuinterApp/Quinter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeline.py
330 lines (310 loc) · 9.75 KB
/
timeline.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
from tweepy import TweepError
import time
import globals
import utils
import speak
import sound
import threading
import os
from GUI import main
class TimelineSettings(object):
def __init__(self,account,tl):
self.account_id=account
self.tl=tl
self.mute=False
self.read=False
class timeline(object):
def __init__(self,account,name,type,data=None,user=None,status=None,silent=False):
self.members=[]
self.account=account
self.status=status
self.name=name
self.removable=False
self.initial=True
self.statuses=[]
self.type=type
self.data=data
self.user=user
self.index=0
self.page=0
self.mute=False
self.read=False
for i in globals.timeline_settings:
if i.account_id==self.account.me.id and i.tl==self.name:
self.mute=i.mute
self.read=i.read
if self.type=="user" and self.name!="Sent" or self.type=="conversation" or self.type=="search" or self.type=="list":
if silent==False:
sound.play(self.account,"open")
self.removable=True
if self.type!="messages":
self.update_kwargs={"count":globals.prefs.count,"tweet_mode":'extended'}
self.prev_kwargs={"count":globals.prefs.count,"tweet_mode":'extended'}
else:
self.update_kwargs={"count":50}
if self.type=="home":
self.func=self.account.api.home_timeline
elif self.type=="mentions":
self.func=self.account.api.mentions_timeline
elif self.type=="messages":
self.func=self.account.api.list_direct_messages
elif self.type=="likes":
self.func=self.account.api.favorites
elif self.type=="user":
self.func=self.account.api.user_timeline
self.update_kwargs['id']=self.data
self.prev_kwargs['id']=self.data
elif self.type=="list":
self.func=self.account.api.list_timeline
self.update_kwargs['list_id']=self.data
self.prev_kwargs['list_id']=self.data
elif self.type=="search":
self.func=self.account.api.search
self.update_kwargs['q']=self.data
self.prev_kwargs['q']=self.data
if self.type!="conversation":
threading.Thread(target=self.load,daemon=True).start()
else:
self.load_conversation()
if self.type=="messages":
m=globals.load_messages(self.account)
if m!=None:
self.statuses=m
self.initial=False
def read_items(self,items):
pref=""
if len(globals.accounts)>1:
pref=self.account.me.screen_name+": "
if len(items)>=4:
speak.speak(pref+str(len(items))+" new in "+self.name)
return
speak.speak(pref+", ".join(self.prepare(items)))
def load_conversation(self):
status=self.status
self.process_status(status)
if globals.prefs.reversed==True:
self.statuses.reverse()
if self.account.currentTimeline==self:
main.window.refreshList()
sound.play(self.account,"search")
def play(self):
if self.type=="user":
if not os.path.exists("sounds/"+self.account.prefs.soundpack+"/"+self.user.screen_name+".ogg"):
sound.play(self.account,"user")
else:
sound.play(self.account,self.user.screen_name)
else:
if self.type=="search":
sound.play(self.account,"search")
elif self.type=="list":
sound.play(self.account,"list")
else:
sound.play(self.account,self.name)
def process_status(self,status):
self.statuses.append(status)
try:
if hasattr(status,"in_reply_to_status_id") and status.in_reply_to_status_id!=None:
self.process_status(utils.lookup_status(self.account,status.in_reply_to_status_id))
if hasattr(status,"retweeted_status"):
self.process_status(status.retweeted_status)
if hasattr(status,"quoted_status"):
self.process_status(status.quoted_status)
except:
pass
def load(self,back=False,speech=False,items=[]):
if items==[]:
if back==True:
if globals.prefs.reversed==False:
self.prev_kwargs['max_id']=self.statuses[len(self.statuses)-1].id
else:
self.prev_kwargs['max_id']=self.statuses[0].id
tl=None
try:
if back==False:
tl=self.func(**self.update_kwargs)
else:
tl=self.func(**self.prev_kwargs)
except TweepError as error:
utils.handle_error(error,self.account.me.screen_name+"'s "+self.name)
if self.removable==True:
if self.type=="user" and self.data in self.account.prefs.user_timelines:
self.account.prefs.user_timelines.remove(self.data)
if self.type=="list" and self.data in self.account.prefs.list_timelines:
self.account.prefs.list_timelines.remove(self.data)
if self.type=="search" and self.data in self.account.prefs.search_timelines:
self.account.prefs.search_timelines.remove(self.data)
self.account.timelines.remove(self)
if self.account==globals.currentAccount:
main.window.refreshTimelines()
if self.account.currentTimeline==self:
main.window.list.SetSelection(0)
self.account.currentIndex=0
main.window.on_list_change(None)
return
else:
tl=items
if tl!=None:
newitems=0
objs=[]
objs2=[]
for i in tl:
if self.type!="messages":
utils.add_users(i)
if utils.isDuplicate(i,self.statuses)==False:
newitems+=1
if self.initial==True or back==True:
if globals.prefs.reversed==False:
self.statuses.append(i)
objs2.append(i)
else:
self.statuses.insert(0,i)
objs2.insert(0,i)
else:
if globals.prefs.reversed==False:
objs.append(i)
objs2.append(i)
else:
objs.insert(0,i)
objs2.insert(0,i)
if newitems==0 and speech==True:
speak.speak("Nothing new.")
if newitems>0:
if self.read==True:
self.read_items(objs2)
if len(objs)>0:
if globals.prefs.reversed==False:
objs.reverse()
objs2.reverse()
for i in objs:
if globals.prefs.reversed==False:
self.statuses.insert(0,i)
else:
self.statuses.append(i)
if globals.currentAccount==self.account and self.account.currentTimeline==self:
if back==False and self.initial==False:
if globals.prefs.reversed==False:
main.window.add_to_list(self.prepare(objs2))
else:
objs2.reverse()
main.window.append_to_list(self.prepare(objs2))
else:
if globals.prefs.reversed==False:
main.window.append_to_list(self.prepare(objs2))
else:
main.window.add_to_list(self.prepare(objs2))
if items==[] and self.type!="messages":
if globals.prefs.reversed==False:
self.update_kwargs['since_id']=tl[0].id
else:
self.update_kwargs['since_id']=tl[len(tl)-1].id
if back==False and self.initial==False:
if globals.prefs.reversed==False:
self.index+=newitems
if globals.currentAccount==self.account and self.account.currentTimeline==self and len(self.statuses)>0:
try:
main.window.list2.SetSelection(self.index)
except:
pass
if back==True and globals.prefs.reversed==True:
self.index+=newitems
if globals.currentAccount==self.account and self.account.currentTimeline==self and len(self.statuses)>0:
main.window.list2.SetSelection(self.index)
if self.initial==True:
if globals.prefs.reversed==False:
self.index=0
else:
self.index=len(self.statuses)-1
if self.mute==False:
self.play()
globals.prefs.statuses_received+=newitems
if speech==True:
speak.speak(str(newitems)+" new items.")
if self.initial==True:
self.initial=False
# if globals.currentTimeline==self:
# main.window.refreshList()
if self.type=="messages":
globals.save_messages(self.account,self.statuses)
if self==self.account.timelines[len(self.account.timelines)-1] and self.account.ready==False:
self.account.ready=True
sound.play(self.account,"ready")
def toggle_read(self):
if self.read==True:
self.read=False
speak.speak("Autoread off")
else:
self.read=True
speak.speak("Autoread on")
globals.get_timeline_settings(self.account.me.id,self.name).read=self.read
globals.save_timeline_settings()
def toggle_mute(self):
if self.mute==True:
self.mute=False
speak.speak("Unmuted")
else:
self.mute=True
speak.speak("Muted")
globals.get_timeline_settings(self.account.me.id,self.name).mute=self.mute
globals.save_timeline_settings()
def get(self):
items=[]
for i in self.statuses:
if self.type!="messages":
items.append(utils.process_tweet(i))
else:
items.append(utils.process_message(i))
return items
def prepare(self,items):
items2=[]
for i in items:
if self.type!="messages":
if globals.prefs.reversed==False:
items2.append(utils.process_tweet(i))
else:
items2.insert(0,utils.process_tweet(i))
else:
if globals.prefs.reversed==False:
items2.append(utils.process_message(i))
else:
items2.insert(0,utils.process_message(i))
return items2
def add(account,name,type,data=None,user=None):
account.timelines.append(timeline(account,name,type,data,user))
if account==globals.currentAccount:
main.window.refreshTimelines()
def timelineThread(account):
while 1:
time.sleep(globals.prefs.update_time*60)
for i in account.timelines:
try:
if i.type=="list":
members=account.api.list_members(list_id=i.data)
i.members=[]
for i2 in members:
i.members.append(i2.id)
if i.type!="conversation":
i.load()
except TweepError as error:
sound.play(account,"error")
if hasattr(error,"response"):
speak.speak(error.response.text)
else:
speak.speak(str(error))
if globals.prefs.streaming==True and (account.stream!=None and account.stream.running==False or account.stream==None):
account.start_stream()
if len(globals.unknown_users)>0:
try:
new_users=account.api.lookup_users(user_ids=globals.unknown_users)
for i in new_users:
if i not in globals.users:
globals.users.insert(0,i)
globals.unknown_users=[]
except:
globals.unknown_users=[]
globals.save_users()
def reverse():
for i in globals.accounts:
for i2 in i.timelines:
i2.statuses.reverse()
i2.index=(len(i2.statuses)-1)-i2.index
main.window.on_list_change(None)