-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
140 lines (105 loc) · 4.03 KB
/
main.py
File metadata and controls
140 lines (105 loc) · 4.03 KB
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
import logging.config
logging.config.dictConfig({
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'default': {
'format': '%(levelname)s %(asctime)s %(module)s %(message)s'
},
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'default',
},
},
'root': {
'handlers': ['console'],
'level': 'DEBUG',
},
})
import logging
import tinder_client
import threading
import time
import database as db
logger = logging.getLogger(__name__)
database = db.MongoDataStore()
positions = [
(37.4507, -122.1634),
]
opening_lines = [
'Early to bed or early to rise?',
]
def _get_random(items):
return items[0] # todo: fixme
class UpdatesThread(threading.Thread):
def __init__(self, last_timestamp, pause_in_seconds):
super(UpdatesThread, self).__init__()
self._last_timestamp = last_timestamp
self._pause_in_seconds = pause_in_seconds
def run(self):
while True:
logger.info('getting updates ... ')
updates_json = tinder_client.get_updates(self._last_timestamp)
def get_full_match(match_id):
return tinder_client.get_match_info(match_id)
map(lambda m: database.save_match(m['_id'], get_full_match(m['_id'])), updates_json['matches'])
map(lambda b: database.save_block(b, {'_id': b}), updates_json['blocks'])
map(lambda l: database.save_list(l['_id'], l), updates_json['lists'])
map(lambda d: database.save_deleted_list(d['_id'], d), updates_json['deleted_lists'])
self._last_timestamp = updates_json['last_activity_date']
logger.info('sleeping for %s seconds ... ' % self._pause_in_seconds)
time.sleep(self._pause_in_seconds / 1000)
class ProcessRecordsThread(threading.Thread):
def __init__(self, pause_in_seconds):
super(ProcessRecordsThread, self).__init__()
self._pause_in_seconds = pause_in_seconds
def run(self):
while True:
for lat, lng in positions:
try:
self._process_location(lat, lng)
except:
logging.exception('error processing (%s, %s)' % (lat, lng))
time.sleep(self._pause_in_seconds)
def _process_location(self, lat, lng):
logger.info('posting location ... ')
tinder_client.set_location(lat, lng)
logger.info('getting recs ... ')
while True:
records = tinder_client.get_records()
if len(records) == 0:
logger.info('received zero recs')
break
for record in records:
self._process_record(record)
def _process_record(self, record):
record_id = record['_id']
database.save_record(record_id, record)
logger.info('liking %s ... ' % record_id)
like_json = tinder_client.like(record_id)
database.save_match(record_id, like_json)
if like_json['match'] is False:
return
tinder_client.send_message(
like_json['match']['_id'],
_get_random(opening_lines),
)
def main():
auth_json = tinder_client.auth(
'CAAGm0PX4ZCpsBAP8ZCogfY2chw42KUbiepcJjU0YA2x4zlZCe7Y6e3IFLfyZBThfBIEFLnxIUjUpuEDOX'
'QMY0FxPOEnNW2tIhSR63Sjfrr52u3lp6M8QDbIzZC5ZAnHEydftDWKkVmzVNwmeeRsCgZA52RMxtE9uMY0'
'YZBTkS7fx4QP6yPZB9xkQ0BxbvlOX2KXY5NlUNznP7lsIeOXZAeb4z1tVfwTcTH0NHbKIRrMso7ZBgZDZD'
)
tinder_client.common_headers['X-Auth-Token'] = auth_json['token']
tinder_client.max_records_per_request = auth_json['globals']['recs_size']
sleep_between_updates = int(auth_json['globals']['updates_interval'])
updates_thread = UpdatesThread(None, sleep_between_updates)
updates_thread.start()
process_records_thread = ProcessRecordsThread(sleep_between_updates)
process_records_thread.start()
updates_thread.join()
process_records_thread.join()
if __name__ == '__main__':
main()