-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamefinders.py
104 lines (86 loc) · 5.86 KB
/
namefinders.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
"""
The functions that handle finding Google and Bing names
"""
import queue
import typing as tp
from project import News_api as Na
def get_gglnames(q_newssites: queue.Queue) -> tp.Tuple[queue.Queue, queue.Queue]:
"""
Finds the Google name of all the NewsSite objects in the queue
@param q_newssites: a queue with NewsSite objects to be processed
@return: processed version of q_newssites
"""
q_newssites_new = queue.Queue() # Initiate the result queue
while not q_newssites.empty(): # Iterate through the queue
(newssite,) = q_newssites.get() # Get NewsSite from queue
name = newssite.name # Get the name
ggl_url = Na.News_Search_URL_Builder(name, 'g', 40, eng=False) # Create the google url
try:
ggl_res = Na.News_Search(ggl_url, tme=False) # Do the google search
if ggl_res: # If this yields results
count = {} # Initiate a counter dictionary
for res in ggl_res: # Iterate through the results
count[res[1]] = 1 if res[1] not in count else count[res[1]] + 1 # Do the counting
names = [] # Initiate a names list
shares = [] # Initiate a shares list
for name, num in count.items(): # Iterate through the count dict
names.append(name) # Add each name to the names list
shares.append(num / sum(count.values())) # Calculate the share of that name
for i, share in enumerate(shares): # Iterate through the shares
if share == max(shares) > 0.4: # Return the name with highest share
newssite.set_ggl_name(names[i]) # _that is above share threshold
# Do some Exception handling
except ConnectionRefusedError as e:
print(f'\nEncountered a ConnectionRefusedError in the Google search: {e},',
f'after {q_newssites_new.qsize()} searches.')
q_newssites.put((newssite,))
return q_newssites_new, q_newssites
except Exception as e:
print(f'\nEncountered an unexpected error in the Google search: {e}')
# Filter
if newssite.check_flag('ggl_name'):
q_newssites_new.put((newssite,))
else:
newssite.set_ggl_name('')
q_newssites_new.put((newssite,))
return q_newssites_new, queue.Queue()
def get_bngnames(q_newssites: queue.Queue) -> tp.Tuple[queue.Queue, queue.Queue]:
"""
Finds the Bing name of all the NewsSite objects in the queue
@param q_newssites: a queue with NewsSite objects to be processed
@return: processed version of q_newssites
"""
q_newssites_new = queue.Queue() # Initiate the result queue
while not q_newssites.empty(): # Iterate through the queue
(newssite,) = q_newssites.get() # Get NewsSite from queue
name = newssite.name # Get the name
bng_url = Na.News_Search_URL_Builder(name, 'b', 40, eng=False) # Create the google url
try:
bng_res = Na.News_Search(bng_url, tme=False) # Do the google search
if bng_res: # If this yields results
count = {} # Initiate a counter dictionary
for res in bng_res: # Iterate through the results
count[res[1]] = 1 if res[1] not in count else count[res[1]] + 1 # Do the counting
names = [] # Initiate a names list
shares = [] # Initiate a shares list
for name, num in count.items(): # Iterate through the count dict
names.append(name) # Add each name to the names list
shares.append(num / sum(count.values())) # Calculate the share of that name
for i, share in enumerate(shares): # Iterate through the shares
if share == max(shares) > 0.4: # Return the name with highest share
newssite.set_bng_name(names[i]) # _that is above share threshold
# Do some Exception handling
except ConnectionRefusedError as e:
print(f'\nEncountered a ConnectionRefusedError in the Bing search: {e},',
f'after {q_newssites_new.qsize()} searches.')
q_newssites.put((newssite,))
return q_newssites_new, q_newssites
except Exception as e:
print(f'\nEncountered an unexpected error in the Bing search: {e}')
# Filter
if newssite.check_flag('bng_name'):
q_newssites_new.put((newssite,))
else:
newssite.set_bng_name('')
q_newssites_new.put((newssite,))
return q_newssites_new, queue.Queue()