-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilters.py
241 lines (216 loc) · 8.04 KB
/
filters.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
from .config import BASE_JSON
from backend.settings import es
from .utils import DATETIME_FORMAT
import datetime
import json
from .clustering import flatten_string
class ParseFilterExcpetion(Exception):
"""Exception when parsing the filter fails"""
pass
filterInitialState = {
'id': '',
'selectedFilter': '',
'twitterHandle': '',
'selectedCountry': '',
'followerCount': 0,
'followerCount1': 0,
'friendCount': 0,
'frinedCount1': 0,
'topicsCondition': {
'label': 'AND',
'value': 'and',
'key': 0,
},
'twitterStringCondition': {
'label': 'Is',
'value': 'is',
'key': 0,
},
'selectedNumberCondition': {
'label': 'Is Greater Than',
'value': 'isGreaterThan',
'key': 0,
},
'followerCountCondition': {
'label': 'Is Greater Than',
'value': 'isGreaterThan',
'key': 0,
},
'lastSeenCondition': {
'label': 'Is Greater Than',
'value': 'isGreaterThan',
'key': 0,
},
'friendCountCondition': {
'label': 'Is Greater Than',
'value': 'isGreaterThan',
'key': 0,
},
# 'startDate': new Date(),
# 'endDate': new Date(),
'topics': [],
}
def getESQueryFromFilter(filter):
# if filter['selectedFilter'] == '':
# raise ParseFilterExcpetion
# if filter['selectedFilter']['value'] == 'allFollowers':
# # query["query"] = {
# # "match_all": {}
# # }
# pass
if filter['selectedFilter']['value'] == 'twitterHandle':
return {
"term": {"screen_name": filter['twitterHandle']}
}
elif filter['selectedFilter']['value'] == 'followerCount':
if filter['followerCountCondition']['value'] == 'isGreaterThan':
return {
"range": {"followers_count": {"gte": filter['followerCount']}}
}
elif filter['followerCountCondition']['value'] == 'isLessThan':
return {
"range": {"followers_count": {"lte": filter['followerCount']}}
}
else:
return {
"range": {"followers_count": {"gte": filter['followerCount'], "lte": filter['followerCount1']}}
}
elif filter['selectedFilter']['value'] == 'friendCount':
if filter['friendCountCondition']['value'] == 'isGreaterThan':
return {
"range": {"friends_count": {"gte": filter['friendCount']}}
}
elif filter['friendCountCondition']['value'] == 'isLessThan':
return {
"range": {"friends_count": {"lte": filter['friendCount']}}
}
else:
return {
"range": {"friends_count": {"gte": filter['friendCount'], "lte": filter['friendCount1']}}
}
elif filter['selectedFilter']['value'] == 'topics':
topics = filter['topics']
if filter['topicsCondition']['value'] == 'or':
return {
"query_string": {
"fields": [
"description",
"status.text"
],
"query": ' OR '.join(map(str, topics))
}}
else:
return {
"query_string": {
"fields": [
"description",
"status.text"
],
"query": ' AND '.join(map(str, topics))
}}
elif filter['selectedFilter']['value'] == 'lastSeen':
if filter['lastSeenCondition']['value'] == 'isGreaterThan':
return {
"range": {"status.created_at": {"gte": filter['startDate']}}
}
elif filter['lastSeenCondition']['value'] == 'isLessThan':
return {
"range": {"status.created_at": {"lte": filter['startDate']}}
}
else:
return {
"range": {"status.created_at": {"gte": filter['startDate'], "lte": filter['endDate']}}
}
elif filter['selectedFilter']['value'] == 'country':
return {
"term": {"country": filter['selectedCountry']['value']}
}
pass
elif filter['selectedFilter']['value'] == 'flag':
if filter['flagCondition']['value'] == 'is':
return {
"term": {filter['flagOption']['value']: True}
}
else:
return {
"term": {filter['flagOption']['value']: False}
}
else:
raise ParseFilterExcpetion
default_source_fields = ["id_str", "name", "screen_name", "location", "description", "url", "followers_count", "friends_count", "created_at",
"verified", "statuses_count", "favourites_count", "status.created_at", "profile_image_url_https", "muting", "blocking", "follow_order", "escher_account"]
def getESQueryFromFilters(filters, escher_account_id_str, size, source_fields=default_source_fields):
empty_filters = list(filter(lambda x: x['selectedFilter'] == '', filters))
if len(empty_filters) > 0:
raise ParseFilterExcpetion
all_followers_filters = list(
filter(lambda x: x['selectedFilter']['value'] == 'allFollowers', filters))
if len(filters) > 1 and len(all_followers_filters) > 0:
raise ParseFilterExcpetion
must = [{"term": {"escher_account": escher_account_id_str}},
{"exists": {"field": "profile_image_url_https"}}]
# source_fields = ["id_str", "name", "screen_name", "location", "description", "url", "followers_count", "friends_count", "created_at",
# "verified", "statuses_count", "favourites_count", "status.created_at", "profile_image_url", "muting", "blocking", "follow_order", "escher_account"]
query = {
"_source": source_fields,
"size": size,
"sort": [ # to get latest followers on top
{
"follow_order": {
"order": "desc"
}
}
]
}
if len(all_followers_filters) > 0:
# must will have the escher_account and must_not will only return users
# that have the profile data in them
query["query"] = {
"bool": {
"must": must
}
}
return query
else:
must_queries = list(map(lambda x: getESQueryFromFilter(x), filters))
# must will have all the filters, and must_not will make sure
# users without data will not be returned
query["query"] = {
"bool": {
"must": must_queries + must
}
}
return query
pass
def country_code_filter(followers, code):
return list(filter(lambda x: x['country_code'] == code, followers))
def is_within_daterange(f, days):
def python_date(x): return datetime.datetime.strptime(x, DATETIME_FORMAT)
last_seen = python_date(
f['last_seen']) if f['last_seen'] != '' else python_date(f['created_at'])
days_delta = datetime.datetime.today() - last_seen
return days_delta.days < days
def lastseen_filter(followers, days):
return list(filter(lambda f: is_within_daterange(f, days), followers))
def top_n_filter(followers, limit=10):
followers.sort(key=lambda x: x['followers_count'], reverse=True)
return followers[:limit]
def token_filter(followers, token):
# print(token)
if '+' in token and ',' in token:
raise Exception
if '+' in token or ',' in token:
if '+' in token:
plustokens = list(
map(lambda x: flatten_string(x), token.split('+')))
followers = list(filter(lambda f: all(
map(lambda x: x in f['tokens'], plustokens)), followers))
else:
commatokens = list(
map(lambda x: flatten_string(x), token.split(',')))
followers = list(filter(lambda f: any(
map(lambda x: x in f['tokens'], commatokens)), followers))
else:
token = flatten_string(token)
followers = list(filter(lambda f: token in f['tokens'], followers))
return followers