-
Notifications
You must be signed in to change notification settings - Fork 543
/
Copy pathcontrollers.py
773 lines (689 loc) · 37.2 KB
/
controllers.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
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
# google_custom_search/controllers.py
# Brought to you by We Vote. Be good.
# -*- coding: UTF-8 -*-
# See also WeVoteServer/import_export_twitter/controllers.py for routines that manage incoming twitter data
from image.functions import analyze_remote_url
from .models import GoogleSearchUserManager, GOOGLE_SEARCH_API_NAME, GOOGLE_SEARCH_API_VERSION, GOOGLE_SEARCH_API_KEY, \
GOOGLE_SEARCH_ENGINE_ID, BALLOTPEDIA_LOGO_URL, MAXIMUM_CHARACTERS_LENGTH, URL_PATTERNS_TO_IGNORE
from googleapiclient.discovery import build
from image.controllers import IMAGE_SOURCE_BALLOTPEDIA, LINKEDIN, FACEBOOK, TWITTER, WIKIPEDIA
from import_export_facebook.models import FacebookManager
from import_export_wikipedia.controllers import reach_out_to_wikipedia_with_guess, \
retrieve_candidate_images_from_wikipedia_page
from re import sub
from wevote_functions.functions import positive_value_exists, convert_state_code_to_state_text, \
POSITIVE_SEARCH_KEYWORDS, NEGATIVE_SEARCH_KEYWORDS, extract_facebook_username_from_text_string
from wevote_settings.models import RemoteRequestHistoryManager, RETRIEVE_POSSIBLE_GOOGLE_LINKS
def delete_possible_google_search_users(candidate):
status = ""
google_search_user_manager = GoogleSearchUserManager()
if not candidate:
status += "DELETE_POSSIBLE_GOOGLE_SEARCH_USER-CANDIDATE_MISSING "
results = {
'success': False,
'status': status,
}
return results
results = google_search_user_manager.delete_google_search_users_possibilities(candidate.we_vote_id)
status += results['status']
results = {
'success': True,
'status': status,
}
return results
def bulk_possible_google_search_users_do_not_match(candidate):
status = ""
google_search_user_manager = GoogleSearchUserManager()
if not candidate:
status += "BULK_POSSIBLE_GOOGLE_SEARCH_USERS_DO_NOT_MATCH-CANDIDATE_MISSING "
results = {
'success': False,
'status': status,
}
return results
results = google_search_user_manager.retrieve_google_search_users_list(candidate.we_vote_id)
status += results['status']
google_search_users_list = results['google_search_users_list']
try:
for google_search_user in google_search_users_list:
if not google_search_user.chosen_and_updated:
google_search_user.not_a_match = True
google_search_user.save()
except Exception as e:
pass
results = {
'success': True,
'status': status,
}
return results
def possible_google_search_user_do_not_match(candidate_we_vote_id, item_link):
status = ""
google_search_user_manager = GoogleSearchUserManager()
if not positive_value_exists(candidate_we_vote_id):
status += "DELETE_POSSIBLE_GOOGLE_SEARCH_USER-CANDIDATE_WE_VOTE_ID_MISSING "
results = {
'success': False,
'status': status,
}
return results
if not positive_value_exists(item_link):
status += "DELETE_POSSIBLE_GOOGLE_SEARCH_USER-ITEM_LINK_MISSING "
results = {
'success': False,
'status': status,
}
return results
results = google_search_user_manager.retrieve_google_search_user_from_item_link(candidate_we_vote_id, item_link)
status += results['status']
if results['google_search_user_found']:
google_search_user = results['google_search_user']
try:
if not google_search_user.chosen_and_updated:
google_search_user.not_a_match = True
google_search_user.save()
except Exception as e:
pass
results = {
'success': True,
'status': status,
}
return results
def retrieve_possible_google_search_users(candidate, voter_device_id):
status = ""
google_search_users_list = []
possible_google_search_users_list = []
google_search_user_manager = GoogleSearchUserManager()
if not candidate:
status = "RETRIEVE_POSSIBLE_GOOGLE_SEARCH_USERS-CANDIDATE_MISSING "
results = {
'success': False,
'status': status,
}
return results
status += "RETRIEVE_POSSIBLE_GOOGLE_SEARCH_USERS-REACHING_OUT_TO_GOOGLE "
name_handling_regex = r"[^ \w'-]"
candidate_name = {
'title': sub(name_handling_regex, "", candidate.extract_title().lower()),
'first_name': sub(name_handling_regex, "", candidate.extract_first_name().lower()),
'middle_name': sub(name_handling_regex, "", candidate.extract_middle_name().lower()),
'last_name': sub(name_handling_regex, "", candidate.extract_last_name().lower()),
'suffix': sub(name_handling_regex, "", candidate.extract_suffix().lower()),
'nickname': sub(name_handling_regex, "", candidate.extract_nickname().lower()),
}
candidate_last_name = candidate.extract_last_name()
search_term = candidate.candidate_name
or_terms = " candidate election office politician"
if positive_value_exists(candidate.state_code):
or_terms += " " + convert_state_code_to_state_text(candidate.state_code)
if positive_value_exists(candidate.contest_office_name):
or_terms += " " + candidate.contest_office_name
google_api = build(GOOGLE_SEARCH_API_NAME, GOOGLE_SEARCH_API_VERSION, developerKey=GOOGLE_SEARCH_API_KEY)
try:
search_results = google_api.cse().list(
q=search_term,
cx=GOOGLE_SEARCH_ENGINE_ID,
exactTerms=candidate_last_name,
gl="countryUS",
# lr="lang_en",
# orTerms=or_terms,
filter='1').execute()
google_search_users_list.extend(
analyze_google_search_results(
search_results, search_term, candidate_name, candidate, voter_device_id))
except Exception as e:
status += "GOOGLE_SEARCH1_PAGE1_FAILED: " + str(e) + " "
# PAGE 2
try:
search_results = google_api.cse().list(
q=search_term,
cx=GOOGLE_SEARCH_ENGINE_ID,
exactTerms=candidate_last_name,
gl="countryUS",
# lr="lang_en",
# orTerms=or_terms,
start=11,
filter='1').execute()
google_search_users_list.extend(
analyze_google_search_results(
search_results, search_term, candidate_name, candidate, voter_device_id))
except Exception as e:
status += "GOOGLE_SEARCH1_PAGE2_FAILED: " + str(e) + " "
# Also include search results omitting any single-letter initials and periods in name.
# Example: "A." is ignored while "A.J." becomes "AJ"
modified_search_term = ""
modified_search_term_base = ""
if len(candidate_name['first_name']) > 1:
modified_search_term += candidate_name['first_name'] + " "
if len(candidate_name['middle_name']) > 1:
modified_search_term_base += candidate_name['middle_name'] + " "
if len(candidate_name['last_name']) > 1:
modified_search_term_base += candidate_name['last_name']
if len(candidate_name['suffix']):
modified_search_term_base += " " + candidate_name['suffix']
modified_search_term += modified_search_term_base
if search_term != modified_search_term:
try:
modified_search_results = google_api.cse().list(
q=modified_search_term,
cx=GOOGLE_SEARCH_ENGINE_ID,
exactTerms=candidate_last_name,
gl="countryUS",
# lr="lang_en",
filter='1').execute()
google_search_users_list.extend(
analyze_google_search_results(
modified_search_results, modified_search_term, candidate_name, candidate, voter_device_id))
except Exception as e:
status += "GOOGLE_SEARCH2_PAGE1_FAILED: " + str(e) + " "
# If nickname exists, try searching with nickname instead of first name
if len(candidate_name['nickname']):
modified_search_term_2 = candidate_name['nickname'] + " " + modified_search_term_base
try:
modified_search_results_2 = google_api.cse().list(
q=modified_search_term_2,
cx=GOOGLE_SEARCH_ENGINE_ID,
exactTerms=candidate_last_name,
gl="countryUS",
# lr="lang_en",
filter='1').execute()
google_search_users_list.extend(
analyze_google_search_results(
modified_search_results_2, modified_search_term_2, candidate_name, candidate, voter_device_id))
except Exception as e:
status += "GOOGLE_SEARCH3_PAGE1_FAILED: " + str(e) + " "
# DALE 2024-02-11 This Twitter search block didn't perform as I had hoped
# # Try to find Twitter matches
# try:
# twitter_search_term = search_term
# # if positive_value_exists(candidate.state_code):
# # twitter_search_term += " " + convert_state_code_to_state_text(candidate.state_code)
# twitter_search_results = google_api.cse().list(
# q=twitter_search_term,
# cx=GOOGLE_SEARCH_ENGINE_ID,
# exactTerms=candidate_last_name,
# gl="countryUS",
# # lr="lang_en",
# linkSite="twitter.com",
# filter='1').execute()
# google_search_users_list.extend(
# analyze_google_search_results(
# twitter_search_results, twitter_search_term, candidate_name, candidate, voter_device_id))
# except Exception as e:
# status += "GOOGLE_SEARCH1_PAGE1_FAILED: " + str(e) + " "
# remove duplicates
for possible_user in google_search_users_list:
for existing_user in possible_google_search_users_list:
if possible_user['google_json']['item_link'] == existing_user['google_json']['item_link']:
break
else:
possible_google_search_users_list.append(possible_user)
wikipedia_page_results = retrieve_possible_wikipedia_page(search_term)
if wikipedia_page_results['wikipedia_page_found']:
update_results = update_google_search_with_wikipedia_results(wikipedia_page_results['wikipedia_page'],
search_term, candidate_name, candidate,
possible_google_search_users_list)
if not update_results['wikipedia_user_exist_in_google_search']:
possible_google_search_users_list.extend(update_results['possible_wikipedia_search_user'])
success = bool(possible_google_search_users_list)
possible_google_search_users_list.sort(key=lambda possible_candidate: possible_candidate['likelihood_score'],
reverse=True)
google_search_user_count = 0
if success:
status += "RETRIEVE_POSSIBLE_GOOGLE_SEARCH_USERS-RETRIEVED_FROM_GOOGLE "
for possibility_result in possible_google_search_users_list:
save_google_search_user_results = google_search_user_manager.\
update_or_create_google_search_user_possibility(
candidate_we_vote_id=candidate.we_vote_id,
google_json=possibility_result['google_json'],
search_term=possibility_result['search_term'],
likelihood_score=possibility_result['likelihood_score'],
facebook_json=possibility_result['facebook_json'],
from_ballotpedia=possibility_result['from_ballotpedia'],
from_facebook=possibility_result['from_facebook'],
from_linkedin=possibility_result['from_linkedin'],
from_twitter=possibility_result['from_twitter'],
from_wikipedia=possibility_result['from_wikipedia'])
if save_google_search_user_results['success'] and \
save_google_search_user_results['google_search_user_created']:
google_search_user_count += 1
# if google_search_user_count == MAXIMUM_GOOGLE_SEARCH_USERS:
# break
# Create a record denoting that we have retrieved from Google for this candidate
remote_request_history_manager = RemoteRequestHistoryManager()
save_results_history = remote_request_history_manager.create_remote_request_history_entry(
kind_of_action=RETRIEVE_POSSIBLE_GOOGLE_LINKS,
google_civic_election_id=candidate.google_civic_election_id,
candidate_campaign_we_vote_id=candidate.we_vote_id,
number_of_results=len(possible_google_search_users_list),
status=status)
results = {
'success': True,
'status': status,
'num_of_possibilities': str(google_search_user_count),
}
return results
def should_ignore_google_json(google_json):
'''
Tests if the google_json object should have a likelihood score of 0 and therefore be ignored
based on the list of url patterns
'''
return any((pattern.match(google_json['item_link']) for pattern in URL_PATTERNS_TO_IGNORE))
def analyze_google_search_results(
search_results,
search_term,
candidate_name,
candidate,
voter_device_id):
total_search_results = 0
state_code = candidate.state_code
state_full_name = convert_state_code_to_state_text(state_code)
possible_google_search_users_list = []
if positive_value_exists(search_results):
total_search_results = (search_results.get('searchInformation').get('totalResults')
if 'searchInformation' in search_results.keys() and
search_results.get('searchInformation', {}).get('totalResults', 0) else 0)
if positive_value_exists(total_search_results):
all_search_items = search_results.get('items', [])
for one_result in all_search_items:
likelihood_score = 0
from_ballotpedia = False
from_facebook = False
from_linkedin = False
from_twitter = False
from_wikipedia = False
google_json = parse_google_search_results(search_term, one_result)
if should_ignore_google_json(google_json):
continue
if FACEBOOK in google_json['item_link']:
current_candidate_facebook_search_info = analyze_facebook_search_results(
google_json, search_term, candidate_name, candidate, voter_device_id)
if positive_value_exists(current_candidate_facebook_search_info):
possible_google_search_users_list.append(current_candidate_facebook_search_info)
continue
# if item_image does not exist and this link is not from ballotpedia then skip this
if not positive_value_exists(google_json['item_image']) \
and IMAGE_SOURCE_BALLOTPEDIA not in google_json['item_link']:
continue
elif BALLOTPEDIA_LOGO_URL in google_json['item_image']:
google_json['item_image'] = ""
# Check if name (or parts of name) are in title, snippet and description
name_found_in_title = False
name_found_in_description = False
for name in candidate_name.values():
if len(name) and name in google_json['item_title'].lower():
likelihood_score += 10
name_found_in_title = True
if len(name) and (name in google_json['item_snippet'].lower() or
name in google_json['item_meta_tags_description'].lower()):
likelihood_score += 5
name_found_in_description = True
# If candidate_name is not present in title, snippet and description then skip this search entry
if not name_found_in_title and not name_found_in_description:
continue
if not name_found_in_title:
likelihood_score -= 10
if not name_found_in_description:
likelihood_score -= 5
# Check if state or state code is in location or description
if google_json['item_person_location'] and positive_value_exists(state_full_name) and \
state_full_name in google_json['item_person_location']:
likelihood_score += 20
elif google_json['item_person_location'] and positive_value_exists(state_code) and \
state_code in google_json['item_person_location']:
likelihood_score += 20
if google_json['item_snippet'] and positive_value_exists(state_full_name) and \
state_full_name in google_json['item_snippet']:
likelihood_score += 20
elif google_json['item_meta_tags_description'] and positive_value_exists(state_full_name) and \
state_full_name in google_json['item_meta_tags_description']:
likelihood_score += 20
# Check if candidate's party is in description
political_party = candidate.political_party_display()
if google_json['item_snippet'] and positive_value_exists(political_party) and \
political_party in google_json['item_snippet']:
likelihood_score += 20
elif google_json['item_meta_tags_description'] and positive_value_exists(political_party) and \
political_party in google_json['item_meta_tags_description']:
likelihood_score += 20
if IMAGE_SOURCE_BALLOTPEDIA in google_json['item_link']:
from_ballotpedia = True
likelihood_score += 20
if LINKEDIN in google_json['item_link']:
from_linkedin = True
if '/posts/' in google_json['item_link']:
likelihood_score -= 100
else:
likelihood_score += 20
if FACEBOOK in google_json['item_link']:
from_facebook = True
if '/posts/' in google_json['item_link'] or '/photos/' in google_json['item_link']:
# See also analyze_facebook_search_results. Usually that processes entry and code never gets here.
likelihood_score -= 100
else:
likelihood_score += 20
if TWITTER in google_json['item_link']:
from_twitter = True
if '/status/' in google_json['item_link']:
likelihood_score -= 100
else:
likelihood_score += 20
if WIKIPEDIA in google_json['item_link']:
from_wikipedia = True
likelihood_score += 20
# Check (each word individually) if office name is in description
# This also checks if state code is in description
office_name = candidate.contest_office_name
if positive_value_exists(office_name) and (google_json['item_snippet'] or
google_json['item_meta_tags_description']):
office_name = office_name.split()
office_found_in_description = False
for word in office_name:
if len(word) > 1 and (word in google_json['item_snippet'] or
word in google_json['item_meta_tags_description']):
likelihood_score += 10
office_found_in_description = True
if not office_found_in_description:
likelihood_score -= 5
# Increase the score for every positive keyword we find
for keyword in POSITIVE_SEARCH_KEYWORDS:
if google_json['item_snippet'] and keyword in google_json['item_snippet'].lower() or \
google_json['item_meta_tags_description'] and \
keyword in google_json['item_meta_tags_description'].lower():
likelihood_score += 5
# Decrease the score for every negative keyword we find
for keyword in NEGATIVE_SEARCH_KEYWORDS:
if (google_json['item_snippet'] and keyword in google_json['item_snippet'].lower()) or \
(google_json['item_meta_tags_description'] and
keyword in google_json['item_meta_tags_description'].lower()):
likelihood_score -= 20
if likelihood_score < 0:
continue
current_candidate_google_search_info = {
'search_term': search_term,
'likelihood_score': likelihood_score,
'from_ballotpedia': from_ballotpedia,
'from_facebook': from_facebook,
'from_linkedin': from_linkedin,
'from_twitter': from_twitter,
'from_wikipedia': from_wikipedia,
'google_json': google_json,
'facebook_json': None
}
possible_google_search_users_list.append(current_candidate_google_search_info)
return possible_google_search_users_list
def parse_google_search_results(search_term, result):
search_request_url = "https://www.googleapis.com/customsearch/v1?q={search_term}&" \
"cx={google_search_engine_id}&filter=1&fl=countryUS&key={google_search_api_key}". \
format(search_term=search_term, google_search_engine_id=GOOGLE_SEARCH_ENGINE_ID,
google_search_api_key=GOOGLE_SEARCH_API_KEY)
item_title = result['title'] if 'title' in result else ''
item_link = result['link'] if 'link' in result else ''
item_snippet = result['snippet'] if 'snippet' in result else ''
item_formatted_url = result['formattedUrl'] if 'formattedUrl' in result else ''
item_image = (result.get('pagemap').get('metatags')[0].get('og:image')
if 'pagemap' in result and result.get('pagemap', {}).get('metatags', []) and
result.get('pagemap', {}).get('metatags', [])[0].get('og:image') else '')
if item_image:
image_results = analyze_remote_url(item_image)
item_image = None if not image_results['image_url_valid'] else item_image
if not item_image:
item_image = (result.get('pagemap').get('cse_image')[0].get('src')
if 'pagemap' in result and result.get('pagemap', {}).get('cse_image', []) and
result.get('pagemap', {}).get('cse_image', [])[0].get('src') else '')
if item_image:
image_results = analyze_remote_url(item_image)
item_image = None if not image_results['image_url_valid'] else item_image
if not item_image:
item_image = (result.get('pagemap').get('cse_thumbnail')[0].get('src')
if 'pagemap' in result and result.get('pagemap', {}).get('cse_thumbnail', []) and
result.get('pagemap', {}).get('cse_thumbnail', [])[0].get('src') else '')
if item_image:
image_results = analyze_remote_url(item_image)
item_image = None if not image_results['image_url_valid'] else item_image
item_meta_tags_description = (result.get('pagemap').get('metatags')[0].get('og:description')
if 'pagemap' in result and result.get('pagemap', {}).get('metatags', []) and
result.get('pagemap', {}).get('metatags', [])[0].get('og:description') else '')
item_formatted_url = (item_formatted_url
if item_link.split("//")[-1] != item_formatted_url.split("//")[-1] else "")
item_person_location = (result.get('pagemap').get('person')[0].get('location')
if 'pagemap' in result and result.get('pagemap', {}).get('person', []) and
result.get('pagemap', {}).get('person', [])[0].get('location', {}) else None)
google_json = {
'item_title': item_title,
'item_link': item_link,
'item_snippet': item_snippet[:MAXIMUM_CHARACTERS_LENGTH],
'item_image': item_image,
'item_formatted_url': item_formatted_url,
'item_meta_tags_description': item_meta_tags_description[:MAXIMUM_CHARACTERS_LENGTH],
'item_person_location': item_person_location,
'search_request_url': search_request_url,
}
return google_json
def retrieve_possible_wikipedia_page(search_term):
status = ""
wikipedia_results = reach_out_to_wikipedia_with_guess(search_term, auto_suggest=False, preload=True)
page_found = wikipedia_results['page_found']
wikipedia_page = wikipedia_results['wikipedia_page']
if not page_found:
# search with auto_suggest as True (auto_suggest: If the literal string isn't found, try another page)
wikipedia_auto_suggest_results = reach_out_to_wikipedia_with_guess(search_term, auto_suggest=True, preload=True)
page_found = wikipedia_auto_suggest_results['page_found']
wikipedia_page = wikipedia_auto_suggest_results['wikipedia_page']
if page_found:
status += "RETRIEVED_CANDIDATE_WIKIPEDIA_RESULTS "
else:
status += "RETRIEVE_CANDIDATE_WIKIPEDIA_RESULTS_FAILED "
results = {
'status': status,
'wikipedia_page_found': page_found,
'wikipedia_page': wikipedia_page,
}
return results
def update_google_search_with_wikipedia_results(wikipedia_page, search_term, candidate_name, candidate,
possible_google_search_users_list):
wikipedia_user_exist_in_google_search = False
possible_wikipedia_search_user = analyze_wikipedia_search_results(wikipedia_page, search_term, candidate_name,
candidate)
for google_search_user in possible_google_search_users_list:
if wikipedia_page and wikipedia_page.url == google_search_user['google_json']['item_link']:
wikipedia_user_exist_in_google_search = True
if len(possible_wikipedia_search_user) > 0:
possible_wikipedia_search_user = possible_wikipedia_search_user[0]
google_search_user['likelihood_score'] = possible_wikipedia_search_user['likelihood_score']
break
results = {
'wikipedia_user_exist_in_google_search': wikipedia_user_exist_in_google_search,
'possible_wikipedia_search_user': possible_wikipedia_search_user
}
return results
def analyze_wikipedia_search_results(wikipedia_page, search_term, candidate_name,
candidate):
likelihood_score = 20
possible_google_search_users_list = []
state_code = candidate.state_code
state_full_name = convert_state_code_to_state_text(state_code)
wikipedia_images_result = retrieve_candidate_images_from_wikipedia_page(candidate, wikipedia_page,
force_retrieve=True)
google_json = {
'item_title': wikipedia_page.original_title,
'item_link': wikipedia_page.url,
'item_snippet': wikipedia_page.summary[:MAXIMUM_CHARACTERS_LENGTH],
'item_image': wikipedia_images_result['image'] if wikipedia_images_result['success'] else '',
'item_formatted_url': '',
'item_meta_tags_description': '',
'item_person_location': '',
'search_request_url': '',
}
# Check if name (or parts of name) are in title, snippet and description
name_found_in_title = False
name_found_in_description = False
for name in candidate_name.values():
if len(name) and name in google_json['item_title'].lower():
likelihood_score += 10
name_found_in_title = True
if len(name) and name in google_json['item_snippet'].lower():
likelihood_score += 5
name_found_in_description = True
if not name_found_in_title and not name_found_in_description:
return possible_google_search_users_list
if not name_found_in_title:
likelihood_score -= 10
if not name_found_in_description:
likelihood_score -= 5
if google_json['item_snippet'] and positive_value_exists(state_full_name) and \
state_full_name in google_json['item_snippet']:
likelihood_score += 20
# Check if candidate's party is in description
political_party = candidate.political_party_display()
if google_json['item_snippet'] and positive_value_exists(political_party) and \
political_party in google_json['item_snippet']:
likelihood_score += 20
# Check (each word individually) if office name is in description
# This also checks if state code is in description
office_name = candidate.contest_office_name
if positive_value_exists(office_name) and google_json['item_snippet']:
office_name = office_name.lower()
office_name = office_name.split()
office_found_in_description = False
for word in office_name:
if len(word) > 1 and word in google_json['item_snippet'].lower():
likelihood_score += 10
office_found_in_description = True
if not office_found_in_description:
likelihood_score -= 5
# Increase the score for every positive keyword we find
for keyword in POSITIVE_SEARCH_KEYWORDS:
if google_json['item_snippet'] and keyword in google_json['item_snippet'].lower():
likelihood_score += 5
# Decrease the score for every negative keyword we find
for keyword in NEGATIVE_SEARCH_KEYWORDS:
if google_json['item_snippet'] and keyword in google_json['item_snippet'].lower():
likelihood_score -= 20
if likelihood_score < 0:
return possible_google_search_users_list
current_candidate_wikipedia_search_info = {
'search_term': search_term,
'likelihood_score': likelihood_score,
'from_ballotpedia': False,
'from_facebook': False,
'from_linkedin': False,
'from_twitter': False,
'from_wikipedia': True,
'google_json': google_json,
'facebook_json': None
}
possible_google_search_users_list.append(current_candidate_wikipedia_search_info)
return possible_google_search_users_list
def analyze_facebook_search_results(google_json, search_term, candidate_name,
candidate, voter_device_id):
likelihood_score = 20
state_code = candidate.state_code
state_full_name = convert_state_code_to_state_text(state_code)
if '/posts/' in google_json['item_link'] \
or '/photos/' in google_json['item_link'] \
or '/photo.php' in google_json['item_link']:
likelihood_score -= 100
facebook_user_manager = FacebookManager()
facebook_user_name = extract_facebook_username_from_text_string(google_json['item_link'])
facebook_user_details_results = facebook_user_manager.retrieve_facebook_user_details_from_facebook(
voter_device_id, facebook_user_name)
facebook_user_details = facebook_user_details_results['facebook_user_details']
if facebook_user_details_results['success']:
# Check if name (or parts of name) are in title, snippet and description
name_found_in_title = False
name_found_in_description = False
for name in candidate_name.values():
if len(name) and (name in facebook_user_details['name'].lower()):
likelihood_score += 10
name_found_in_title = True
if len(name) and (name in facebook_user_details['description'].lower() or
name in facebook_user_details['about'].lower() or
name in facebook_user_details['mission'].lower() or
name in facebook_user_details['bio'].lower()):
likelihood_score += 5
name_found_in_description = True
if not name_found_in_title:
likelihood_score -= 10
if not name_found_in_description:
likelihood_score -= 5
# Check if state or state code is in location or description
if positive_value_exists(state_full_name):
if state_full_name in facebook_user_details['location']:
likelihood_score += 20
if state_full_name in facebook_user_details['description'] or \
state_full_name in facebook_user_details['bio'] or \
state_full_name in facebook_user_details['about'] or \
state_full_name in facebook_user_details['general_info'] or \
state_full_name in facebook_user_details['personal_info']:
likelihood_score += 20
elif positive_value_exists(state_code):
if state_code in facebook_user_details['location']:
likelihood_score += 20
if state_code in facebook_user_details['description'] or \
state_code in facebook_user_details['bio'] or \
state_code in facebook_user_details['about'] or \
state_full_name in facebook_user_details['general_info'] or \
state_full_name in facebook_user_details['personal_info']:
likelihood_score += 20
# Check if candidate's party is in description
political_party = candidate.political_party_display()
if positive_value_exists(political_party):
if political_party in facebook_user_details['description'] or \
political_party in facebook_user_details['bio'] or \
political_party in facebook_user_details['about'] or \
political_party in facebook_user_details['mission'] or \
political_party in facebook_user_details['general_info'] or \
political_party in facebook_user_details['personal_info'] or \
political_party in facebook_user_details['posts']:
likelihood_score += 20
# Check (each word individually) if office name is in description
# This also checks if state code is in description
office_name = candidate.contest_office_name
if positive_value_exists(office_name):
office_name = office_name.split()
office_found_in_description = False
for word in office_name:
if len(word) > 1 and (word in facebook_user_details['description'] or
word in facebook_user_details['bio'] or
word in facebook_user_details['about'] or
word in facebook_user_details['mission'] or
word in facebook_user_details['general_info'] or
word in facebook_user_details['personal_info']):
likelihood_score += 10
office_found_in_description = True
if not office_found_in_description:
likelihood_score -= 5
# Increase the score for every positive keyword we find
for keyword in POSITIVE_SEARCH_KEYWORDS:
if keyword in facebook_user_details['description'] or \
keyword in facebook_user_details['bio'] or \
keyword in facebook_user_details['about'] or \
keyword in facebook_user_details['mission'] or \
keyword in facebook_user_details['general_info'] or \
keyword in facebook_user_details['personal_info'] or \
keyword in facebook_user_details['posts']:
likelihood_score += 5
# Decrease the score for every negative keyword we find
for keyword in NEGATIVE_SEARCH_KEYWORDS:
if keyword in facebook_user_details['description'] or \
keyword in facebook_user_details['bio'] or \
keyword in facebook_user_details['about'] or \
keyword in facebook_user_details['mission'] or \
keyword in facebook_user_details['general_info'] or \
keyword in facebook_user_details['personal_info'] or \
keyword in facebook_user_details['posts']:
likelihood_score -= 20
if likelihood_score < 0:
return dict
current_candidate_facebook_search_info = {
'search_term': search_term,
'likelihood_score': likelihood_score,
'from_ballotpedia': False,
'from_facebook': True,
'from_linkedin': False,
'from_twitter': False,
'from_wikipedia': False,
'google_json': google_json,
'facebook_json': facebook_user_details
}
return current_candidate_facebook_search_info