This repository has been archived by the owner on Mar 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
gbfraidcopier.pyw
1090 lines (995 loc) · 53.2 KB
/
gbfraidcopier.pyw
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import configparser
import time
import queue
import datetime
import base64
import tkinter as Tk
import tkinter.ttk as ttk
from tkinter import messagebox, simpledialog
import sys
import os
import subprocess
import webbrowser
from urllib import request
import re
import threading
import traceback
###########################################################################################################################
# version.json loading
###########################################################################################################################
try:
with open('version.json', encoding="utf-8") as f:
versions = json.load(f)
for key in ["raidfinder", "raidlist", "tweepy", "pyperclip"]:
if key not in versions:
raise Exception()
except Exception as e:
messagebox.showerror("Invalid version.json", "Consider redownloading a fresh copy of this raidfinder\nError: {}".format(e))
raise Exception("Can't load 'version.json'") from e
###########################################################################################################################
# sound
###########################################################################################################################
soundFile = None
try:
import winsound # windows only
except ImportError: # if it fails
def playsound():
os.system('beep -f %s -l %s' % (200,100)) # frequency (Hz) and duration (ms)
else:
# load the sound file
try:
with open('alert.wav','rb') as f: # open the file
soundFile = f.read() # store in string format
soundLoaded = True
except IOError: # not working ? the file probably doesn't exist
soundLoaded = False
def playsound(): # run winsound.PlaySound() in a thread to not block (SND_ASYNC doesn't work when playing a sound from the memory)
if soundLoaded:
threading.Thread(target=winsound.PlaySound, args=(soundFile, winsound.SND_MEMORY)).start()
###########################################################################################################################
# general utility
###########################################################################################################################
def installRequirements(): # run requirements.txt through pip
subprocess.check_call([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"])
def cmpVer(mver, tver): # compare version strings, True if greater or equal, else False
me = mver.split('.')
te = tver.split('.')
for i in range(0, min(len(me), len(te))):
if int(me[i]) < int(te[i]):
return False
return True
###########################################################################################################################
# import tweepy and pyperclip and check their versions
###########################################################################################################################
REQ_OUTDATED = False
if __name__ == "__main__":
try: # try to import
import tweepy
import pyperclip
except: # failed, call pip to install
try:
installRequirements()
import tweepy
import pyperclip
except: # failed again, we exit
if sys.platform == "win32":
import ctypes
try: is_admin = ctypes.windll.shell32.IsUserAnAdmin()
except: is_admin = False
if not is_admin:
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)
else:
print("Can't install the requirements")
exit(0)
# compare and check module versions
if not cmpVer(tweepy.__version__, versions["tweepy"]) or not cmpVer(pyperclip.__version__, versions["pyperclip"]):
REQ_OUTDATED = True
else:
# this part only run if you import the raidfinder in another project
import tweepy
import pyperclip
###########################################################################################################################
# Raidfinder Class
###########################################################################################################################
class Raidfinder():
def __init__(self, versions, use_ui=True):
self.versions = versions
self.settings = {'pause':0, 'jp':1, 'en':1, 'sound':1, 'copy':1, 'author':1, 'dupe':1, 'time':0, 'jst':0, 'lang':'en.json', 'update_raids':1, 'update_raidfinder':1, 'tooltip':1, 'second':0, 'limit':1, 'limit_rule':5, 'limit_char':512}
self.langs = {}
self.strings = {}
self.raids = {}
self.apprunning = True
self.filter = ""
self.stats = {"stat_time":None, "stat_received":0, "stat_matched":0, "stat_dupes":0, "stat_filter":0 ,"stat_delay":0 ,"stat_received_last":None ,"stat_matched_last":None}
self.connected = False
self.custom = []
self.bearer_token = ""
self.keys = {}
self.tracked = {}
self.statlock = threading.Lock()
self.elapsed = 0
self.time = time.time()
self.lastcode = ""
self.secondcode = ""
self.log = Log()
self.lasttab = 0
self.loadSetting()
self.initLangs()
if not self.loadLanguage(self.settings.get("lang", self.settings.get('lang'))):
self.loadLanguage('en.json')
#self.log.push(self.getString('news'))
self.checkUpdate()
loadedRaid = self.loadRaids()
self.stream = Stream(self)
self.ui = (UI(self) if use_ui else None)
if loadedRaid:
self.ui.reloadRaids(self.raids, self.custom)
else:
self.log.push(self.getString("err_load_raid"))
if use_ui and self.bearer_token == "":
messagebox.showinfo("Info", self.getString("err_empty_token"))
return
if REQ_OUTDATED:
self.log.push(self.getString('req_outdated'))
def loadSetting(self):
# load the .cfg with the Twitter API keys and settings
config = configparser.RawConfigParser(strict=True)
try:
with open('gbfraidcopier.cfg', encoding="utf-8") as f:
config.read_file(f)
except:
try:
config['Twitter'] = {'bearer_token':''}
with open('gbfraidcopier.cfg', mode='w', encoding="utf-8") as f:
config.write(f)
except:
pass
# twitter access keys
try: self.bearer_token = config['Twitter']['bearer_token']
except: pass
try: self.keys = config['Keys'] # for retrocompatibility, to remove in the future
except: pass
# check if last used settings are here. if yes, read them
if 'Settings' in config:
try: self.settings['jp'] = int(config['Settings']['japanese'])
except: pass
try: self.settings['en'] = int(config['Settings']['english'])
except: pass
try: self.settings['sound'] = 1 - int(config['Settings']['mute'])
except:
try: self.settings['sound'] = int(config['Settings']['sound'])
except: pass
try: self.settings['copy'] = int(config['Settings']['copy'])
except: pass
try: self.settings['author'] = int(config['Settings']['author'])
except: pass
try: self.settings['dupe'] = int(config['Settings']['duplicate'])
except: pass
try: self.settings['time'] = int(config['Settings']['time'])
except: pass
try: self.settings['jst'] = int(config['Settings']['jst'])
except: pass
try: self.lasttab = int(config['Settings']['lasttab'])
except: pass
try: self.settings['tooltip'] = int(config['Settings']['tooltip'])
except: pass
try: self.filter = base64.b64decode(config['Settings']['filter']).decode('utf-8')
except: pass
try: self.settings['lang'] = base64.b64decode(config['Settings']['lang']).decode('utf-8')
except: pass
try: self.settings['update_raids'] = int(config['Settings']['check_raid_list'])
except: pass
try: self.settings['update_raidfinder'] = int(config['Settings']['check_raidfinder'])
except: pass
try: self.settings['second'] = int(config['Settings']['copy_second'])
except: pass
try: self.settings['limit'] = int(config['Settings']['limit'])
except: pass
try: self.settings['limit_rule'] = int(config['Settings']['limit_rule'])
except: pass
try: self.settings['limit_char'] = int(config['Settings']['limit_char'])
except: pass
# custom user raids
if 'Raids' in config:
for i in range(0, 27):
self.custom.append(["My Raid {}".format(i+1), "Lvl 100 ???", "Lv100 ???"])
try:
self.custom[i][0] = base64.b64decode(config['Raids']['savedName' + str(i)]).decode('utf-8')
self.custom[i][1] = base64.b64decode(config['Raids']['savedEN' + str(i)]).decode('utf-8')
self.custom[i][2] = base64.b64decode(config['Raids']['savedJP' + str(i)]).decode('utf-8')
except:
pass
def saveSetting(self): # called when quitting
# update the values
# NOTE : pause setting isn't saved !
try:
config = configparser.RawConfigParser()
try: self.lasttab = self.ui.raidframe.index(self.ui.raidframe.select())
except: self.lasttab = 0
config['Settings'] = {
'japanese': str(self.settings['jp']),
'english': str(self.settings['en']),
'sound': str(self.settings['sound']),
'copy': str(self.settings['copy']),
'author': str(self.settings['author']),
'duplicate': str(self.settings['dupe']),
'time_mode': str(self.settings['time']),
'lasttab':str(self.lasttab),
'jst':str(self.settings['jst']),
'tooltip':str(self.settings['tooltip']),
'check_raid_list':str(self.settings['update_raids']),
'check_raidfinder':str(self.settings['update_raidfinder']),
'copy_second':str(self.settings['second']),
'limit':str(self.settings['limit']),
'limit_rule':str(self.settings['limit_rule']),
'limit_char':str(self.settings['limit_char']),
'filter':base64.b64encode(self.filter.encode('utf-8')).decode('ascii'),
'lang':base64.b64encode(self.lookupLangFile(self.ui.lang_var.get()).encode('utf-8')).decode('ascii')
}
config['Twitter'] = {'bearer_token':self.bearer_token}
config['Keys'] = self.keys # to remove later
config['Raids'] = {}
for i in range(0, 27): # custom user raids
if i>= len(self.custom):
config['Raids']['savedName' + str(i)] = base64.b64encode("Raid #{}".format(i+1).encode('utf-8')).decode('ascii')
config['Raids']['savedEN' + str(i)] = base64.b64encode("Lvl 100 ???".encode('utf-8')).decode('ascii')
config['Raids']['savedJP' + str(i)] = base64.b64encode("Lv100 ???".encode('utf-8')).decode('ascii')
else:
config['Raids']['savedName' + str(i)] = base64.b64encode(self.custom[i][0].encode('utf-8')).decode('ascii')
config['Raids']['savedEN' + str(i)] = base64.b64encode(self.custom[i][1].encode('utf-8')).decode('ascii')
config['Raids']['savedJP' + str(i)] = base64.b64encode(self.custom[i][2].encode('utf-8')).decode('ascii')
# Writing our configuration file
with open('gbfraidcopier.cfg', 'w', encoding="utf-8") as configfile:
config.write(configfile)
except:
print("Failed to write 'gbfraidcopier.cfg'")
return True
def checkUpdate(self):
errmsg = False
try:
if self.settings['update_raidfinder'] == 0 and self.settings['update_raids'] == 0:
return
root = Tk.Tk() # dummy window
root.withdraw()
# download the latest version.json
req = request.Request("https://raw.githubusercontent.com/MizaGBF/Raidfinder/master/version.json")
url_handle = request.urlopen(req, timeout=60)
data = json.loads(url_handle.read())
url_handle.close()
errmsg = True
# check raid list
if self.settings['update_raids'] == 1 and not cmpVer(self.versions['raidlist'], data['raidlist']):
res = messagebox.askyesno("Raid List Update", self.getString('update_raid'))
if res:
req = request.Request("https://raw.githubusercontent.com/MizaGBF/Raidfinder/master/raid.json")
url_handle = request.urlopen(req, timeout=60)
with open("raid.json", mode="wb") as f:
f.write(url_handle.read())
url_handle.close()
self.versions['raidlist'] = data['raidlist']
with open('version.json', mode='w', encoding="utf-8") as f: # update our version.json
json.dump(self.versions, f)
messagebox.showinfo("Raid List Update", self.getString('update_raid_success'))
# check general version
if self.settings['update_raidfinder'] == 1 and not cmpVer(self.versions['raidfinder'], data['raidfinder']):
res = messagebox.askyesno("Update available", self.getString('update_main').format(data['raidfinder']))
if res:
webbrowser.open('https://github.com/MizaGBF/Raidfinder/archive/refs/heads/master.zip', new=2)
except:
if errmsg: messagebox.showinfo("Update Error", self.getString('update_error'))
try: url_handle.close()
except: pass
root.destroy()
def initLangs(self):
files = [f for f in os.listdir("lang") if os.path.isfile(os.path.join("lang", f))]
for js in files:
try:
with open("lang/" + js, mode="r", encoding="utf-8") as f:
data = json.load(f)
self.langs[js] = data['name']
except Exception as e:
self.log.push("Lang File error for '{}'\n{}".format(js, e))
def loadLanguage(self, lang_file):
try:
with open("lang/en.json", mode="r", encoding="utf-8") as f:
strings = json.load(f)
except:
self.log.push("Can't find 'lang/en.json'")
return False
if lang_file != "en.json":
try:
with open("lang/" + lang_file, mode="r", encoding="utf-8") as f:
overwrite = json.load(f)
except:
self.log.push("Can't find 'lang/{}'".format(lang_file))
return False
self.strings = {**strings, **overwrite}
else:
self.strings = strings
self.settings['lang'] = lang_file
return True
def lookupLangFile(self, lang_name):
for key in self.langs:
if self.langs[key] == lang_name:
return key
return None
def getString(self, key):
return self.strings.get(key, key)
def loadRaids(self):
raids = {
'en':{},
'jp':{},
'index':{},
'pages':[],
'filters':[]
}
try:
with open("raid.json", mode="r", encoding="utf-8") as f:
data = json.load(f)
for p in data['pages']:
for r in p['list']:
raids['index'][r['name']] = {'X':r['posX'], 'Y':r['posY'], 'page':len(raids['pages']), 'tracked':False}
raids['en'][r['name']] = r['en']
raids['jp'][r['name']] = r['jp']
raids['pages'].append({'name':p['name'], 'color':p['color'], 'decorator':p.get('decorator', [])})
raids['pages'].append({'name':'Custom', 'color':data['custom color'], 'decorator':[]})
raids['filters'] = data['filters']
for c in self.custom:
if c[0] not in raids['en']:
raids['en'][c[0]] = c[1]
raids['jp'][c[0]] = c[2]
self.raids = raids
except Exception as e:
print(e)
self.log.push(self.getString('err_raid_load').format(e))
return False
return True
def run(self):
while self.apprunning:
self.elapsed = time.time() - self.time # measure elapsed time
self.time = time.time()
if not self.settings['pause'] and self.connected:
with self.statlock:
if self.stats['stat_time'] is None: self.stats['stat_time'] = self.elapsed
else: self.stats['stat_time'] += self.elapsed
if self.ui:
self.ui.run()
else:
for l in self.log.empty():
print(l)
time.sleep(0.02)
def lastCode(self):
return self.secondcode if self.settings['second'] else self.lastcode
def updateStreamTracking(self, raids):
self.stream.tracked.put(raids)
###########################################################################################################################
# Log
###########################################################################################################################
class Log():
def __init__(self):
self.queue = queue.Queue()
self.size = 0
def push(self, msg):
self.queue.put(msg)
def empty(self):
strings = []
while not self.queue.empty():
strings.append(self.queue.get())
return strings
###########################################################################################################################
# Twitter Streaming Wrapper
###########################################################################################################################
class Stream(tweepy.StreamingClient):
def __init__(self, parent):
self.raidfinder = parent
self.tweetQueue = queue.Queue()
self.idregex = re.compile('([A-F0-9]{8}) :')
super().__init__(bearer_token=self.raidfinder.bearer_token, return_type=dict, wait_on_rate_limit=True, daemon=True)
self.myrules = {}
self.trashrules = {}
self.rulelock = threading.Lock()
self.tracked = queue.Queue()
self.restart_delay = 0
if not self.clearRules():
self.raidfinder.log.push(self.raidfinder.getString("err_auth"))
return
if not self.raidfinder.settings['limit']:
if self.buildRules(self.raidfinder.raids['filters']):
if not self.applyRules():
self.raidfinder.log.push(self.raidfinder.getString("err_unknown"))
return
else:
self.raidfinder.log.push(self.raidfinder.getString("err_unknown"))
return
else:
self.raidfinder.log.push(self.raidfinder.getString("limit_info"))
self.thread_ruleupdater = threading.Thread(target=self.ruleupdater)
self.thread_ruleupdater.daemon = True
self.thread_ruleupdater.start()
self.thread_watchdog = threading.Thread(target=self.watchdog)
self.thread_watchdog.daemon = True
self.thread_watchdog.start()
def clearRules(self):
try:
current_rules = self.get_rules()
ids = []
if 'data' in current_rules:
for r in current_rules['data']:
ids.append(r['id'])
if len(ids) > 0:
self.delete_rules(ids)
return True
except:
return False
def buildRules(self, filters):
try:
new_rules = []
s = ""
for i, f in enumerate(filters):
if s != "":
if len(f) + len(" OR ") + len(s) >= 512:
new_rules.append(s)
if len(new_rules) >= 5: break
s = ""
else:
s += " OR "
s += '"' + f + '"'
if s != "":
new_rules.append(s)
newmyrules = {}
with self.rulelock:
for r in new_rules:
if r in self.myrules:
newmyrules[r] = self.myrules[r]
else:
newmyrules[r] = None
for r in self.myrules:
if r not in newmyrules:
self.trashrules[r] = self.myrules[r]
self.myrules = newmyrules
return True
except:
return False
def applyRules(self):
try:
rules = []
with self.rulelock:
for r, id in self.myrules.items():
if id is None:
rules.append(tweepy.StreamRule(value=r))
while len(rules) > 0 or len(self.trashrules) > 0:
if len(rules) > 0 and len(self.trashrules) < 5:
resp = self.add_rules(rules[-1])
if 'data' in resp:
for k in resp['data']:
self.myrules[k['value']] = k['id']
rules.pop()
with self.rulelock:
if len(self.trashrules) > 0:
k = list(self.trashrules.keys())[0]
if self.trashrules[k] is not None:
self.delete_rules(self.trashrules[k])
self.trashrules.pop(k)
return True
except:
return False
def start_streaming(self):
try:
while self.raidfinder.settings['limit'] and len(self.myrules) == 0:
time.sleep(0.01)
self.applyRules()
self.filter(tweet_fields=['source', 'created_at'], threaded=True)
except Exception as e:
self.raidfinder.log.push(self.raidfinder.getString("err_filter_error").format(e))
# https://docs.tweepy.org/en/latest/streamingclient.html
def on_data(self, raw_data):
self.tweetQueue.put((json.loads(raw_data.decode('utf8'))['data'], datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)))
def on_exception(self, exception):
self.restart_delay = 10
print("on_exception():", traceback.format_exception(type(exception), exception, exception.__traceback__))
s = str(exception)
if self.raidfinder.connected: # exception happened while being connected
if s.lower().find("timed out") != -1 or s.lower().find("connection broken") != -1:
self.raidfinder.log.push(self.raidfinder.getString("exception_timeout"))
else:
self.raidfinder.log.push(self.raidfinder.getString("exception_general").format(exception))
self.raidfinder.connected = False
else: # else, unknown error
self.raidfinder.log.push(self.raidfinder.getString("exception_unknown").format(exception))
self.raidfinder.connected = False
self.raidfinder.log.push(self.raidfinder.getString("twitter_restart"))
def on_errors(self, errors):
print("Errors:", errors)
def on_connect(self):
if not self.raidfinder.connected:
self.raidfinder.log.push(self.raidfinder.getString('twitter_connected'))
self.raidfinder.connected = True
def on_disconnect(self):
if self.raidfinder.connected:
self.raidfinder.log.push(self.raidfinder.getString('twitter_disconnected'))
self.raidfinder.connected = False
def ruleupdater(self):
while self.raidfinder.apprunning:
if not self.tracked.empty():
try:
raids = None
while not self.tracked.empty():
raids = self.tracked.get()
if raids is not None:
self.buildRules(raids)
self.applyRules()
except:
pass
time.sleep(0.5)
def watchdog(self):
time.sleep(5)
stats = {}
dupes = set()
while self.raidfinder.apprunning:
try:
with self.raidfinder.statlock:
self.raidfinder.stats['stat_received'] += stats.get('stat_received', 0)
self.raidfinder.stats['stat_matched'] += stats.get('stat_matched', 0)
self.raidfinder.stats['stat_dupes'] += stats.get('stat_dupes', 0)
self.raidfinder.stats['stat_filter'] += stats.get('stat_filter', 0)
self.raidfinder.stats['stat_delay'] = stats.get('stat_delay', self.raidfinder.stats['stat_delay'])
self.raidfinder.stats['stat_received_last'] = stats.get('stat_received_last', self.raidfinder.stats['stat_received_last'])
self.raidfinder.stats['stat_matched_last'] = stats.get('stat_matched_last', self.raidfinder.stats['stat_matched_last'])
stats = {}
try:
data, reception_time = self.tweetQueue.get(block=True, timeout=0.01) # retrieve next tweet and its reception time
except:
time.sleep(0.001)
if not self.running:
if self.restart_delay > 0:
time.sleep(self.restart_delay)
self.restart_delay = 0
self.start_streaming()
continue
if self.raidfinder.settings['pause']:
continue
created_at = datetime.datetime.strptime(data['created_at'], "%Y-%m-%dT%H:%M:%S.%fZ")
st = data['text']
m = self.idregex.search(st)
if not m:
continue # not found, we skip
code = m.group(1) # get the code
stats = {'stat_delay':(reception_time - created_at).seconds, 'stat_received_last':time.time(), 'stat_received':1}
p = st.rfind("参加者募集!\n") # search the japanese 'I need backup' first (because it's most likely to be a japanese tweet
lg = '(JP)'
mp = 0 # minimal position of I need backup + raidname (used later to retrive the author comment if any)
if p != -1 and p >= 15: # check the minimal position for jp
if not self.raidfinder.settings['jp']: continue
p += 7 # valid, add the size of JP I need backup. p nows points to the raid name
mp = 22
else:
p = st.rfind("I need backup!\n") # same thing but for english
if p < 20 or not self.raidfinder.settings['en']: continue # english isn't valid, so is JP, we skip
p += 15 # size of I need backup
mp = 35
lg = '(EN)'
raidname = st[p:].rsplit('\nhttp', 1)[0]
r = self.raidfinder.tracked.get(raidname, None)
if r is None:
continue
stats['stat_matched'] = 1
comment = "" # build the author comment
for c in range(0, p-mp): # ignoring out of range characters
if ord(st[c]) in range(65536):
comment += st[c]
stats['stat_matched_last'] = stats['stat_received_last']
if self.raidfinder.filter != "" and self.raidfinder.filter.lower() not in comment.lower():
stats['stat_filter'] = 1
continue
if code in dupes:
stats['stat_dupe'] = 1
continue
if self.raidfinder.settings['copy']:
pyperclip.copy(self.raidfinder.lastcode if self.raidfinder.settings['second'] else code) # copy if enabled
if self.raidfinder.settings['sound']:
playsound() # play a sound if enabled
# write to the log
if self.raidfinder.settings['time']:
if self.raidfinder.settings['jst']:
t = (created_at + datetime.timedelta(seconds=32400)).strftime("%H:%M:%S")
else:
t = created_at.strftime("%H:%M:%S UTC")
else:
if self.raidfinder.settings['jst']:
t = (datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None) + datetime.timedelta(seconds=32400)).strftime("%H:%M:%S JST")
else:
t = datetime.datetime.now().strftime("%H:%M:%S")
dupes.add(code)
self.raidfinder.secondcode = self.raidfinder.lastcode
self.raidfinder.lastcode = code
self.raidfinder.log.push('[{}] {} : {} {} {}'.format(t, r, code, lg, comment))
if len(dupes) >= 500:
keep = list(dupes)
dupes = set()
for i in range(250, len(keep)):
dupes.add(keep[i])
except Exception as te:
print("TEST:", data)
print(te)
###########################################################################################################################
# Custom UI ToolTip
###########################################################################################################################
class Tooltip():
def __init__(self, parent, raidfinder, translate_key): # create a tooltip for the parent widget, with the associated text
self.parent = parent
self.raidfinder = raidfinder
self.tip = None
self.text = raidfinder.getString(translate_key)
if parent:
self.parent.bind('<Enter>', self.show) # bind to hover
self.parent.bind('<Leave>', self.hide) # bind to not-hover
def show(self, event): # called by the enter event, show the text
if not self.raidfinder.settings.get('tooltip', True) or self.tip or not self.text: # don't show twice or if no text
return
x, y, cx, cy = self.parent.bbox("insert") # bound box
x = x + self.parent.winfo_rootx()
y = y + cy + self.parent.winfo_rooty() + 40 # 40px lower
self.tip = Tk.Toplevel(self.parent)
self.tip.wm_overrideredirect(1)
self.tip.wm_geometry("+%d+%d" % (x, y))
label = Tk.Label(self.tip, text=self.text, justify=Tk.LEFT, background="#ffffe0", relief=Tk.SOLID, borderwidth=1)
label.pack(ipadx=1)
def hide(self, event): # called by the leave event, destroy the label
tmp = self.tip
self.tip = None
if tmp: tmp.destroy()
###########################################################################################################################
# UI
###########################################################################################################################
class UI(Tk.Tk):
def __init__(self, raidfinder): # the UI is built here
Tk.Tk.__init__(self,None)
# variables
self.parent = None
self.raidfinder = raidfinder # Raidfinder class
self.tracking = {}
self.readonly = {}
self.iconbitmap('favicon.ico')
self.inputting = False
self.mainsett = []
self.advsett = []
self.lang_var = Tk.StringVar()
try: self.lang_var.set(self.raidfinder.langs[self.raidfinder.settings.get('lang', 'en.json')])
except: pass
COLUMNSPAN = 4
# building the UI
## raid part
mainframes = []
s = ttk.Separator(self, orient=Tk.HORIZONTAL)
s.grid(row=0, column=0, columnspan=COLUMNSPAN, sticky="we")
self.raidframe = ttk.Notebook(self)
mainframes.append(self.raidframe)
mainframes[-1].grid(row=1, column=0, columnspan=COLUMNSPAN, sticky="we")
self.raids = [] # empty for now
self.raidchilds = []
self.custom = []
self.decorators = []
## tweet filter
mainframes.append(ttk.Frame(self))
mainframes[-1].grid(row=2, column=0, rowspan=1, columnspan=COLUMNSPAN, sticky="we")
self.filterlabel = Tk.Label(mainframes[-1], text=self.raidfinder.getString("filter"))
self.filterlabel.pack(side=Tk.LEFT)
Tooltip(self.filterlabel, self.raidfinder, "filter_desc")
self.filterlabeloriginal = self.filterlabel.cget("background")
self.filter=Tk.Text(mainframes[-1], height=1, width=35)
self.filter.pack(side=Tk.LEFT, fill=Tk.X)
self.filter.insert(Tk.END, self.raidfinder.filter)
self.filter.bind("<FocusIn>", self.focusin)
self.filter.bind("<FocusOut>", self.focusout)
self.filter.bind('<<Modified>>', self.updateFilter)
self.filterModified = False
ttk.Separator(mainframes[-1], orient=Tk.HORIZONTAL).pack(side=Tk.BOTTOM, fill=Tk.X)
## main settings
s = ttk.Separator(self, orient=Tk.HORIZONTAL)
s.grid(row=3, column=0, columnspan=COLUMNSPAN, sticky="we")
mainframes.append(ttk.Frame(self))
mainframes[-1].grid(row=4, column=0, columnspan=COLUMNSPAN, sticky="we")
self.mainsett_b = []
for count, key in enumerate(["pause","jp","en","sound","copy"]):
self.mainsett_b.append(Tk.Checkbutton(mainframes[-1], text="[{}] {}".format(count, self.raidfinder.getString("setting_" + key)), variable=self.newIntVar(self.mainsett, self.raidfinder.settings.get(key, 0)), command=lambda n=count, k=key: self.toggleSetting(n, k)))
self.mainsett_b[-1].grid(row=1+count // 3, column=count % 3, sticky="ws")
Tooltip(self.mainsett_b[-1], self.raidfinder, "setting_" + key + "_desc")
count += 1
self.mainsett_b.append(Tk.Button(mainframes[-1], text=self.raidfinder.getString("[{}] {}").format(count, self.raidfinder.getString("button_last")), command=self.copyLatest))
self.lastSettingNum = count
self.mainsett_b[-1].grid(row=1+count // 3, column=count % 3, sticky="ews")
Tooltip(self.mainsett_b[-1], self.raidfinder, "button_last_desc")
#### others
self.statusLabel = Tk.Label(mainframes[-1], text=self.raidfinder.getString("offline"), bg='#edc7c7') # for the offline/online text
self.statusLabel.grid(row=1, column=3, sticky="ews")
Tooltip(self.statusLabel, self.raidfinder, "status_desc")
self.timeLabel = Tk.Label(mainframes[-1], text="00:00:00") # for the current time
self.timeLabel.grid(row=2, column=3, sticky="ews")
Tooltip(self.timeLabel, self.raidfinder, "clock_desc")
## bottomn (log / advanced setting / stats)
s = ttk.Separator(self, orient=Tk.HORIZONTAL)
s.grid(row=5, column=0, columnspan=COLUMNSPAN, sticky="we")
mainframes.append(ttk.Notebook(self))
mainframes[-1].grid(row=6, column=0, columnspan=COLUMNSPAN, sticky="we")
subtabs = []
### log
subtabs.append(ttk.Frame(mainframes[-1]))
mainframes[-1].add(subtabs[-1], text=self.raidfinder.getString('log'))
scrollbar = Tk.Scrollbar(subtabs[-1]) # the scroll bar
scrollbar.pack(side=Tk.RIGHT, fill=Tk.Y)
self.logtext = Tk.Text(subtabs[-1], state=Tk.DISABLED, yscrollcommand=scrollbar.set, height=10, width=35, bg='#f8f8f8', wrap=Tk.WORD) # the log box itself, with a height limit
self.logtext.pack(fill=Tk.BOTH, expand=1, side=Tk.LEFT)
scrollbar.config(command=self.logtext.yview)
### advanced settings
subtabs.append(Tk.Frame(mainframes[-1], bg='#dfe5d7')) # setting frame
mainframes[-1].add(subtabs[-1], text=self.raidfinder.getString("advanced")) # tab
####### check buttons
for count, val in enumerate([('dupe', 0, 0), ('jst', 1, 0), ('time', 2, 0), ('tooltip', 3, 0), ('update_raids', 4, 0), ('update_raidfinder', 5, 0), ('second', 0, 1), ('limit', 1, 1)]):
b = Tk.Checkbutton(subtabs[-1], bg=subtabs[-1]['bg'], text=self.raidfinder.getString("setting_" + val[0]), variable=self.newIntVar(self.advsett, self.raidfinder.settings.get(val[0], 0)), command=lambda n=count, k=val[0]: self.toggleAdvSetting(n, k))
b.grid(row=val[1], column=val[2], stick=Tk.W)
Tooltip(b, self.raidfinder, "setting_" + val[0] + "_desc")
####### buttons
for count, val in enumerate([('button_raidlist', 0, 2), ('button_version', 1, 2), ('button_github', 2, 2), ('button_requirements', 3, 2)]):
b = Tk.Button(subtabs[-1], text=self.raidfinder.getString(val[0]), command=lambda n=count: self.buttonCallback(n))
b.grid(row=val[1], column=val[2], sticky="ews")
Tooltip(b, self.raidfinder, val[0] + '_desc')
# language choice
l = Tk.Label(subtabs[-1], bg=subtabs[-1]['bg'], text=self.raidfinder.getString('language')) # for the current time
l.grid(row=4, column=2, sticky="wse")
b = Tk.OptionMenu(subtabs[-1], self.lang_var, *list(self.raidfinder.langs.values()), command=self.lang_changed)
b.grid(row=5, column=2, stick="ws")
Tooltip(b, self.raidfinder, "lang_desc")
### stats
# mostly text labels, you can skip over it
subtabs.append(Tk.Frame(mainframes[-1], bg='#e5e0d7'))
mainframes[-1].add(subtabs[-1], text=self.raidfinder.getString("stat"))
self.stats = {}
Tk.Button(subtabs[-1], text=self.raidfinder.getString("reset"), command=self.resetStats).grid(row=4, column=0, sticky="ews") # reset button
# all stats to be displayed (Label Text, Position X, Position Y, Default Text)
for count, val in enumerate([('stat_time', 0, 0, "0:00:00"), ('stat_received', 0, 1, "0"), ('stat_matched', 0, 2, "0"), ('stat_ratio', 0, 3, "0.00%"), ('stat_received_rate', 1, 1, "0/s"), ('stat_matched_rate', 1, 2, "0/s"), ('stat_dupes', 1, 0, "0"), ('stat_received_last', 2, 1, "?"), ('stat_matched_last', 2, 2, "?"), ('stat_filter', 1, 3, "0"), ('stat_delay', 2, 3, "0s")]):
b = Tk.Label(subtabs[-1], bg=subtabs[-1]['bg'], text=self.raidfinder.getString(val[0]))
b.grid(row=val[2], column=val[1]*2, sticky="ws")
Tooltip(b, self.raidfinder, val[0] + '_desc')
self.stats[val[0]] = Tk.Label(subtabs[-1], bg=subtabs[-1]['bg'], text=val[3])
self.stats[val[0]].grid(row=val[2], column=val[1]*2+1, sticky="nw")
Tooltip(self.stats[val[0]], self.raidfinder, val[0] + '_desc')
# make the window and bind the keyboard
self.title(self.raidfinder.getString("title") + self.raidfinder.versions['raidfinder'])
self.resizable(width=False, height=False) # not resizable
self.protocol("WM_DELETE_WINDOW", self.close) # call close() if we close the window
self.bind_all("<Key>", self.key)
def close(self): # called by the app when closed
self.raidfinder.saveSetting() # update config file
self.raidfinder.apprunning = False
self.destroy() # destroy the window
def focusin(self, event): # event for managing a widget focus
self.inputting = True
def focusout(self, event): # event for managing a widget focus
self.inputting = False
def updateFilter(self, event):
if not self.filterModified:
self.raidfinder.filter = self.filter.get('0.0', 'end')[:-1]
self.filterModified = not self.filterModified
self.tk.call(self.filter, 'edit', 'modified', 0)
def key(self, event): # key event for setting shortcuts
if not self.raidfinder.apprunning or event.type != '2' or self.inputting: # 2 is KeyPress
return
numKey = event.keycode - 48 # 48 is the 0 key
if numKey < 0 or numKey > self.lastSettingNum:
numKey = event.keycode - 96 # 96 is the 0 numpad key
if numKey < 0 or numKey > self.lastSettingNum:
return # not a number, so return
if numKey == self.lastSettingNum:
self.copyLatest() # copy last button
else:
self.mainsett_b[numKey].toggle() # toggle the checkbox
self.toggleSetting(numKey, ["pause","jp","en","sound","copy"][numKey]) # call the event
def toggleSetting(self, n, k): # called when un/checking a main setting
state = self.mainsett[n].get()
if k in self.raidfinder.settings:
self.raidfinder.settings[k] = state
if state: self.raidfinder.log.push(self.raidfinder.getString("setting_enable").format(self.raidfinder.getString("setting_"+k)))
else: self.raidfinder.log.push(self.raidfinder.getString("setting_disable").format(self.raidfinder.getString("setting_"+k)))
if k == 'pause':
if state:
self.raidfinder.updateStreamTracking([])
elif self.raidfinder.settings['limit']:
self.raidfinder.updateStreamTracking(list(self.raidfinder.tracked.keys()))
else:
self.raidfinder.updateStreamTracking(self.raidfinder.raids['filters'])
def toggleAdvSetting(self, n, k): # called when un/checking an advanced setting
state = self.advsett[n].get()
if k in self.raidfinder.settings:
self.raidfinder.settings[k] = state
if k == 'limit':
if self.raidfinder.settings['pause']:
pass
elif state:
self.raidfinder.updateStreamTracking(list(self.raidfinder.tracked.keys()))
else:
self.raidfinder.updateStreamTracking(self.raidfinder.raids['filters'])
def buttonCallback(self, n): # called when an advanced setting button is pressed
if n == 0:
if self.raidfinder.loadRaids():
messagebox.showinfo("Info", self.raidfinder.getString("raid_reload"))
self.raidfinder.updateStreamTracking([])
self.reloadRaids(self.raidfinder.raids, self.raidfinder.custom)
else:
messagebox.showerror("Error", self.raidfinder.getString("raid_reload_err"))
elif n == 1:
webbrowser.open('https://github.com/MizaGBF/Raidfinder/archive/refs/heads/master.zip', new=2)
elif n == 2:
webbrowser.open('https://github.com/MizaGBF/Raidfinder', new=2)
elif n == 3:
try: installRequirements()
except: pass
messagebox.showinfo("Info", self.raidfinder.getString("req_install"))
def copyLatest(self):
code = self.raidfinder.lastCode()
if code is not None:
pyperclip.copy(code)
self.raidfinder.log.push(self.raidfinder.getString("lastcode").format(code))
def lang_changed(self, *args):
messagebox.showinfo("Info", self.raidfinder.getString("lang_msg"))
def resetStats(self): # simply reset the stats
self.raidfinder.stats = {"stat_time":None, "stat_received":0, "stat_matched":0, "stat_dupes":0, "stat_filter":0 ,"stat_delay":0 ,"stat_received_last":None ,"stat_matched_last":None}
def toggleRaid(self, r): # called when un/checking a raid
state = self.tracking[r].get()
self.readonly[r] = state
tr = self.raidfinder.getString(r)
if state:
self.raidfinder.tracked[self.raidfinder.raids['en'][r]] = tr
self.raidfinder.tracked[self.raidfinder.raids['jp'][r]] = tr
self.raidfinder.log.push(self.raidfinder.getString('track_on').format(tr))
else:
self.raidfinder.tracked.pop(self.raidfinder.raids['en'][r], None)
self.raidfinder.tracked.pop(self.raidfinder.raids['jp'][r], None)
self.raidfinder.log.push(self.raidfinder.getString('track_off').format(tr))
if self.raidfinder.settings['limit'] and not self.raidfinder.settings['pause']:
self.raidfinder.updateStreamTracking(list(self.raidfinder.tracked.keys()))
def editCustom(self, i): # called when editing a custom raid
self.inputting = True # to disable the keyboard shortcuts
tmp = self.raidfinder.settings['pause'] # save the pause setting
self.raidfinder.settings['pause'] = True # pause the app
self.raidfinder.updateStreamTracking([])
customEntry = self.raidfinder.custom[i]
v1 = simpledialog.askstring(self.raidfinder.getString("edit_custom"), self.raidfinder.getString( "edit_custom_subA"), initialvalue=customEntry[0]) # ask for user input
if v1 == None: # if the user cancelled
self.inputting = False
self.raidfinder.settings['pause'] = tmp
if not tmp:
self.raidfinder.updateStreamTracking(list(self.raidfinder.tracked.keys()))
return # we return
v2 = simpledialog.askstring(self.raidfinder.getString("edit_custom"), self.raidfinder.getString( "edit_custom_subB"), initialvalue=customEntry[1])
if v2 == None: # same thing
self.inputting = False
self.raidfinder.settings['pause'] = tmp
if not tmp:
self.raidfinder.updateStreamTracking(list(self.raidfinder.tracked.keys()))
return
v3 = simpledialog.askstring(self.raidfinder.getString("edit_custom"), self.raidfinder.getString( "edit_custom_subC"), initialvalue=customEntry[2])
if v3 == None: # same thing
self.inputting = False
self.raidfinder.settings['pause'] = tmp
if not tmp:
self.raidfinder.updateStreamTracking(list(self.raidfinder.tracked.keys()))
return
self.inputting = False # re-enable keyboard shortcuts
self.raidfinder.custom[i] = [v1, v2, v3] # save the user inputs
self.raidfinder.settings['pause'] = tmp # restore the pause setting
if self.raidfinder.loadRaids():
self.raidfinder.updateStreamTracking([])
self.reloadRaids(self.raidfinder.raids, self.raidfinder.custom)
else:
messagebox.showerror("Error", self.raidfinder.getString("raid_reload_err"))
return
# log and end
self.raidfinder.log.push(self.raidfinder.getString("[System] {} saved in slot {}").format(v1, i+1)) # logging for the user to check any mistake
self.raidfinder.log.push("EN : {}".format(v2))
self.raidfinder.log.push("JP : {}".format(v3))
def newIntVar(self, array, init=0): # used by setting checkboxes
array.append(Tk.IntVar(value=init))
return array[-1]
def newTrackingVar(self, key): # used by raid checkboxes
if key not in self.tracking:
self.tracking[key] = Tk.IntVar(value=0)
self.readonly[key] = 0
return self.tracking[key]
def formatStat(self, v):
if v > 1000000: return str(v // 1000000) + "M"
elif v > 1000: return str(v // 1000) + "K"
return v
def reloadRaids(self, raids, custom):
# clean stuff
for c in self.raidchilds:
c.destroy()
for p in self.custom:
p.destroy()
for p in self.raids:
p.destroy()
for d in self.decorators:
p.destroy()