-
Notifications
You must be signed in to change notification settings - Fork 3
/
tinderApp.py
439 lines (398 loc) · 20.9 KB
/
tinderApp.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
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
#!/Users/marinig/opt/anaconda3/lib/python3.7
# Filename: tinderApp.py
import sys
from PySide2.QtWidgets import *
from PySide2.QtCore import *
from PySide2.QtGui import *
import qdarkstyle
import argparse
import oyaml as yaml
import csv
import json
import GUI.loginWindow as lw
import GUI.mainWindow as mw
from API.tinder_api_extended import TinderApi
from GUI.log import log
import traceback
from Threading.data_reloader import APIBackgroundWorker
from GUI.customwaitingwidget import QtCustomWaitingSpinner
import faulthandler
import time
import os
import re
class TinderApp(QApplication):
def __init__(self, parsed_args, *args, **kwargs):
super(TinderApp, self).__init__(*args, **kwargs)
log.create_log_widget()
self.data_folder = "./Data/"
self.recommendations_folder = self.data_folder + "recommendations/"
self.recommendations_file = self.data_folder + "recommendations.json"
self.matches_folder = self.data_folder + "matches/"
self.matches_file = self.data_folder + "matches.json"
self.profile_folder = self.data_folder + "profile/"
self.profile_file = self.data_folder + "profile.json"
self.tinder_api = TinderApi(self.data_folder)
self.logged_in = False
# self.thread = QThread() # 1a - create Worker and Thread inside the Form. No parent!
# self.obj = worker.Worker() # no parent!
self.thread_pool = QThreadPool() # 1b - create a ThreadPool and then create the Workers when you need!
self.thread_pool.setMaxThreadCount(4)
self.background_tasks_list = []
self.background_info = QLabel("No background stuff!")
self.background_count_label = QLabel("")
self.background_completed = 0
self.background_count_total = 0
self.spinner = QtCustomWaitingSpinner(self, centerOnParent=False)
self.spinner.hideSpinner()
self.spinner.updateSize(QSize(30,30))
"""
I wanted to make this config an object. I tried by calling yaml_load and getting
the dictionary from a pure yaml file (without !!python/object:__main__.ConfigSingleton on top)
Then I tried to pass the dictionary to an object like Struct(**dict) and
class Struct:
def __init__(self, **entries):
self.__dict__.update(entries)
But it did not work
"""
self.configSingleton = ConfigSingleton() # Transform the dict to attributes of the class ConfigSingleton
self.config = self.configSingleton.yaml_config # Transform the dict to attributes of the class ConfigSingleton
self.config_file = self.configSingleton.yaml_config_file
self.recommendations = None
self.matches = None
self.new_recommendations = None
self.new_matches = None
self.profile_info = None
self.window = ""
# set app icon
self.setWindowIcon(QIcon("GUI/icons/tinder.png"))
if parsed_args.css:
self.updateStyle()
else:
if sys.platform!='darwin':
self.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
# self.startMainWindow(True)
self.startLoginWindow()
def export_people_data_csv(self, csv_filename, people_data, keys_to_export):
with open(csv_filename, mode='r') as csv_file:
csv_writer = csv.writer(csv_file, delimiter=',', quotechar='"',
quoting=csv.QUOTE_MINIMAL)
for person in people_data:
for key in person.keys():
to_write = []
if key in keys_to_export:
to_write = to_write + person[key]
if len(to_write) > 0:
csv_writer.writerow(to_write)
def updateData(self, list_to_fill, downloaded_data, file_path):
if list_to_fill is not None and isinstance(list_to_fill, list):
list_to_fill.append(downloaded_data)
data_to_dump = list_to_fill
else:
data_to_dump = downloaded_data
try:
with open(file_path, 'w') as fp:
json.dump(data_to_dump, fp)
log.i("API", "Data written to: " + file_path)
except Exception as e:
log.e("API", "Exception saving " + file_path + " json: " + str(e))
def get_api_data(self, doAsync=False, api_fun=None, args=[], kwargs={},
finished_callback=None, callback_args=None, callback_kwargs=None,
update_callback=None, info=None, tag=None):
if not doAsync:
kwargs["log_to_widget"] = False
kwargs["thread_update_signal"] = None
result = api_fun(*args, **kwargs)
self.new_data_callback(result, finished_callback)
else:
obj = APIBackgroundWorker(api_fun, args, kwargs, callback=finished_callback, callback_args=callback_args, callback_kwargs=callback_kwargs)
if tag is not None:
obj.tag = tag
obj.signals.dataReceived.connect(self.new_data_callback) # 2 - Connect Worker`s Signals to Form method slots to post data.
if update_callback is None:
update_callback = self.updateBackgroundTaskInfo
obj.signals.dataUpdate.connect(update_callback) # 2 - Connect Worker`s Signals to Form method slots to post data.
if info is None:
self.addBackgroundTask(obj, "Getting Data from API: " +str(info))
else:
self.addBackgroundTask(obj, info)
def get_profile(self, doAsync=True, force_update=False):
try:
with open(self.profile_file, "r") as pf:
data= json.load(pf)
log.d("APP", "Profile file read: " +str(data))
self.setProfileData(data, download_data=False)
except Exception as e:
log.e("APP", "No profile found")
if self.profile_info is None or force_update:
self.get_api_data(doAsync, self.tinder_api.get_self,
[], {},
finished_callback=self.setProfileData,
callback_kwargs={'download_data':True},
update_callback=self.updateBackgroundTaskInfo,
info="Getting profile data",
tag="ProfileGetter")
def setProfileData(self, new_data, download_data=False):
log.d("APP", "New profile info, download: " +str(download_data))
if new_data is not None:
self.profile_info = new_data
if self.profile_info is None:
return
if self.window is not None and self.window.chat_widget is not None:
self.window.chat_widget.setLeftId(self.profile_info["_id"])
if download_data:
self.get_api_data(True, self.tinder_api.download_person_data,
[self.profile_info, self.profile_folder, True, True, False, True],
{"force_overwrite": True},
finished_callback=self.updateProfileData,
update_callback=self.updateBackgroundTaskInfo,
info="Downloading Profile data to: " + str(self.profile_folder),
tag="ProfileDownloader")
def updateProfileData(self, downloaded_data):
try:
with open(self.profile_file, 'w') as fp:
json.dump(downloaded_data, fp)
log.i("API", "Profile data written to: " + self.profile_file)
except Exception as e:
log.e("API", "Exception saving " + self.profile_file + " json: " + str(e))
pass
def get_matches(self, doAsync=True):
self.get_api_data(doAsync, self.tinder_api.all_matches,
finished_callback=self.setNewMatches,
info="Getting new Matches")
def setNewMatches(self, data):
if 'data' in data:
self.new_matches = data['data']['matches']
self.updateMatchesWidgets(False, True)
def read_matches(self, doAsync=False):
self.get_api_data(doAsync, self.tinder_api.read_data, [self.matches_file],
info="Reading data from: " + str(self.matches_file),
finished_callback=self.setMatches,
tag="MatchesDownloader")
def reload_matches(self, doAsync=False, photos=True, insta=True, messages=True, force_overwrite=False):
self.get_api_data(doAsync, self.tinder_api.reload_data_from_disk,
[self.matches_folder, self.matches_file, photos, insta, messages],
{"force_overwrite": force_overwrite},
finished_callback=self.setMatches,
update_callback=self.updateBackgroundTaskInfo,
info="Reloading Matches",
tag="MatchesReloader")
def download_new_matches(self, photos=True, insta=True, messages=True, rename_images=False, amount=0, force_overwrite=False):
self.get_api_data(True, self.tinder_api.download_people_data_api,
[self.new_matches, self.matches_folder, photos, insta, messages, rename_images, amount],
{"force_overwrite": force_overwrite},
finished_callback=self.updateMatches,
update_callback=self.updateBackgroundTaskInfo,
info="Downloading Matches data to: " + str(self.matches_folder),
tag="MatchesDownloader")
def setMatches(self, data):
log.d("APP", "updateMatches called")
self.new_matches = None
self.matches = data
self.updateMatchesWidgets()
def updateMatches(self, downloaded_data):
self.updateData(self.matches, downloaded_data, self.matches_file)
self.updateMatchesWidgets()
def updateMatchesWidgets(self, updateList=True, updateWidgets=True):
if self.window is not None:
if updateList:
self.window.matches_list.update_list(self.matches)
if updateWidgets:
self.window.features_panel.update_matches_widgets()
def get_recommendations(self, doAsync=True):
self.get_api_data(doAsync, self.tinder_api.get_recs_v2,
finished_callback=self.setNewRecommendations,
info="Getting new Recommendations")
def setNewRecommendations(self, data):
if 'data' in data:
self.new_recommendations = data['data']['results']
self.updateRecommendationsWidgets(False, True)
# Read recommendations.json file containing all recommendations
def read_recommendations(self, doAsync=False):
self.get_api_data(doAsync, self.tinder_api.read_data, [self.recommendations_file],
info="Reading data from: " + str(self.recommendations_file),
finished_callback=self.setRecommendations,
tag="RecomendationsDownloader")
# Redownload recommendations and writes the output to file file containing all recommendations
def reload_recommendations(self, doAsync=False,photos=True, insta=True, force_overwrite=False):
self.get_api_data(doAsync, self.tinder_api.reload_data_from_disk,
[self.recommendations_folder, self.recommendations_file, photos, insta, False],
{"force_overwrite": force_overwrite},
finished_callback=self.setRecommendations,
update_callback=self.updateBackgroundTaskInfo,
info="Reloading Recommendations",
tag="RecommendationsReloader")
def download_new_recommendations(self, photos=True, insta=True, rename_images=False, amount=0, force_overwrite=False):
self.get_api_data(True, self.tinder_api.download_people_data_api,
[self.new_recommendations, self.recommendations_folder, photos, insta, False, rename_images, amount],
{"force_overwrite": force_overwrite},
finished_callback=self.updateRecommendations,
update_callback=self.updateBackgroundTaskInfo,
info="Downloading Recommendations data to: " + str(self.matches_folder),
tag="RecommendationsDownloader")
def setRecommendations(self, data):
log.d("APP", "setRecommendations called")
self.new_recommendations = None
self.recommendations = data
self.updateRecommendationsWidgets()
def updateRecommendations(self, downloaded_data):
self.updateData(self.recommendations, downloaded_data, self.recommendations_file)
self.updateRecommendationsWidgets()
def updateRecommendationsWidgets(self, updateList=True, updateWidgets=True):
if self.window is not None:
if updateList:
self.window.recommendations_list.update_list(self.recommendations)
if updateWidgets:
self.window.features_panel.update_recommendations_widgets()
def get_messages(self, match_data, finished_callback=None, person_name=""):
self.app.get_api_data(True, self.app.tinder_api.get_messages,
[], {'match_data': match_data, 'count': 100, 'page_token': None},
finished_callback=finished_callback,
info="Getting messages " + person_name,
tag="MessagesDownloader")
def get_match_info(self, id, name):
self.get_api_data(True, self.tinder_api.get_person, [id], finished_callback=self.person_data_received,
info="Getting data of " +str(name + "_"+ str(id)))
def person_data_received(self, data):
self.window.json_view.load_data(data["data"])
log.d("PERSON_DATA", +str(data))
def new_data_callback(self, data, callback, callback_args, callback_kwargs, async_data=None, time_started=None, tag=None):
if async_data is not None:
self.completeBackgroundTask(async_data + ": completed! ", tag=tag)
if time_started is not None:
print(async_data + " execution time: " + str(time.time() - time_started) + "s")
if callback is not None:
print("Callback, args: " +str(callback_args) + ". kwwargs: " +str(callback_kwargs))
if callback_args is not None and callback_kwargs is not None:
callback(data, callback_args, callback_kwargs)
elif callback_args is not None:
callback(data, callback_args)
elif callback_kwargs is not None:
callback(data, callback_kwargs)
else:
callback(data)
def updateStyle(self):
log.i("APP", "Update style!")
try:
with open('./style.css', 'r') as cssfile:
self.setStyleSheet(cssfile.read())
except Exception as e:
log.e("APP", "Update style Exception")
def startLoginWindow(self):
self.window = lw.LoginWindow(self, self.background_info, self.background_count_label, self.spinner, parsed_args)
self.window.show()
if self.config:
self.window.form_widget.verify_login(True)
def verify_login(self, doAsync, callback):
log.i("APP", "Config File: " + str(os.path.abspath(self.config_file)))
log.i("APP", "Verify login: " + str(self.config))
self.get_api_data(doAsync, self.tinder_api.authverif,
[self.config['facebook']['auth_token'], self.config['facebook']['user_id']],
finished_callback=callback, info="Verifying login",
tag="LoginVerifier")
def login_verified(self, isVerified, openMainWindow=False):
self.logged_in = isVerified
QApplication.processEvents()
if openMainWindow:
with open(self.config_file, 'w') as fy:
yaml.dump(self.config, fy)
self.startMainWindow(True)
self.update_status_bar()
def update_status_bar(self):
if self.logged_in:
log.i("APP", "Login verified!")
self.window.statusBar.showMessage("Login verified!")
self.window.setWindowTitle("TinderAI (Online)")
else:
log.i("APP", "Login FAILED!")
self.window.statusBar.showMessage("Login FAILED!")
self.window.setWindowTitle("TinderAI (OFFLINE)")
def startMainWindow(self, doAsync=False):
if self.window is not None:
self.window.close()
if self.config:
self.window = mw.MainWindow(self, self.background_info, self.background_count_label, self.spinner, parsed_args)
self.window.show()
self.update_status_bar()
self.read_recommendations(doAsync)
self.read_matches(doAsync)
self.get_profile(doAsync, force_update=False) #it should be None at the beginning, triggering the get_self_data
else:
self.window = lw.LoginWindow(self, parsed_args)
self.window.show()
self.window.form_widget.statusBar.showMessage("No config file found, login again")
def addBackgroundTask(self, task, info=""):
if task.tag is not None:
while task.tag in self.background_tasks_list:
string_matches = re.match(r"(.*)([0-9]+)", task.tag)
if string_matches is not None:
# print("string_matches: " +str(string_matches.groups()))
task.tag = string_matches.group(1)+str(int(string_matches.group(2))+1)
else:
task.tag += "_1"
self.background_tasks_list.append(task.tag)
self.updateBackgroundTaskInfo(info)
self.background_count_total += 1
if self.background_count_total <= 1:
self.spinner.showSpinner()
self.background_count_label.setVisible(True)
self.updateBackgroundTaskCount()
log.d("THREADS", "Added new background task " +str(info) +" " +str(self.background_completed) + "/" + str(self.background_count_total) +" Tasks: " +str(self.background_tasks_list), False)
self.thread_pool.start(task)
def completeBackgroundTask(self, info="", tag=None):
self.updateBackgroundTaskInfo(info)
if tag is not None and tag in self.background_tasks_list:
self.background_tasks_list.remove(tag)
self.background_completed += 1
if self.background_completed >= self.background_count_total:
self.background_count_total = 0
self.background_completed = 0
self.spinner.hideSpinner()
self.updateBackgroundTaskInfo("All tasks completed!")
self.background_count_label.setVisible(False)
self.updateBackgroundTaskCount()
log.d("THREADS", "Completed background task " +str(info) +" " +str(self.background_completed) + "/" + str(self.background_count_total) +" Tasks: " +str(self.background_tasks_list), False)
def updateBackgroundTaskCount(self):
self.background_count_label.setText(str(self.background_completed) +"/"+str(self.background_count_total))
def updateBackgroundTaskInfo(self, text=""):
self.background_info.setText(str(text))
class ConfigSingleton:
__instance__ = None
yaml_config_file = "./settings.yaml"
try:
yaml_config = yaml.safe_load(open(yaml_config_file))
except Exception:
yaml_config = None
def __init__(self):
""" Constructor. """
if ConfigSingleton.__instance__ is None:
ConfigSingleton.__instance__ = self
else:
raise Exception("You cannot create another SingletonGovt class")
def get_instance():
""" Static method to fetch the current instance. """
if not ConfigSingleton.__instance__:
ConfigSingleton()
return ConfigSingleton.__instance__
def process_cl_args():
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--css', action='store_true', help='Reloads the CSS at each event, used for styling purposes') # optional flag
parsed_args, unparsed_args = parser.parse_known_args()
return parsed_args, unparsed_args
"""
THIS IS SUPER IMPORTANT IN PYQT5 APPS
Without this, python won't print any unhandled exception since they happen within the app.exec_()
"""
def trap_exc_during_debug(*args):
exc_type, exc_value, exc_traceback = sys.exc_info()
log.e("MAIN", "General Exception: " +str(args))
traceback.print_tb(exc_traceback, limit=None, file=sys.stdout)
if __name__ == '__main__':
# install exception hook: without this, uncaught exception would cause application to exit
sys.excepthook = trap_exc_during_debug
parsed_args, unparsed_args = process_cl_args()
# QApplication expects the first argument to be the program name.
qt_args = sys.argv[:1] + unparsed_args
with open("./fault_handler.log", "w") as fobj:
faulthandler.enable(fobj)
app = TinderApp(parsed_args, qt_args)
app.exec_()