-
Notifications
You must be signed in to change notification settings - Fork 1
/
fulgencio.py
242 lines (181 loc) · 7.61 KB
/
fulgencio.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
"""
Gets lots of job vacancies!
Writes them in leads.xlsx
"""
import re
import time
import pandas as pd
from decouple import config
from selenium.common.exceptions import WebDriverException
import requests
import urllib.parse
from openpyxl.utils.exceptions import IllegalCharacterError
import util
import values
from cts import *
EMAIL_ID = 'email'
PASS_ID = 'pass'
LOGIN_BUTTON_ID = 'u_0_2'
SCREEN_HEIGHT = 1080
COORDINATES = (int(config('coordinate_x')), int(config('coordinate_y')))
COLUMNS = ['name', 'post', 'word', 'group_name', 'group_url', 'count']
FULGENCIO_URL = config('fulgencio_url')
COMPANY_URL = 'company_url'
EMAILS = 'emails'
PHONES = 'phones'
NAME_CLASS_TAG = '_2nlw _2nlv'
def get_company_url_from_email(email):
if email and not any([e in email for e in ('gmail', 'hotmail', 'yahoo', 'msn')]):
return email.split('@')[1]
else:
return ''
def get_profile(split):
closest_match = [m.start() for m in re.finditer(FULGENCIO_URL, split)]
if len(closest_match) > 0:
closest_match = closest_match[-1]
almost = split[closest_match:]
else:
return None
pattern = f'{FULGENCIO_URL}\S*'
big_url = re.search(pattern, almost).group()
absolute_url = str(big_url.split('?')[0])
return absolute_url.replace(FULGENCIO_URL, '')
def filter_posts_with_email(df):
return df[df['post'].apply(lambda p: len(re.findall(util.EMAIL_REGEX, p)) > 0)]
def scrape_name(browser, profile):
try:
print(f'browser.get({profile}), ...')
browser.get(profile)
name = browser.find_element_by_xpath(f"//*[@class='{NAME_CLASS_TAG}']")
return name.text
except WebDriverException:
print(f'failed to load {profile}, continuing...')
return ''
def scrap_word(word, df, html, group_name, group_url):
"""
:param word: string
:param df: pandas Dataframe
:param html: str html
:param group_url: str
:return: df
"""
post_pattern = f'>[^>]*\s{word}\s[^<]*<'
splits = re.compile(post_pattern).split(html)[:-1]
# found nothing
if len(splits) == 0:
print(f'nothing found :( for word {word} on group {group_url}')
return df
posts = re.findall(post_pattern, html)
for idx, split in enumerate(splits):
profile = get_profile(split)
if profile:
post = posts[idx].replace('>', '').replace('<', '')
post = post[:min(2000, len(post))]
if profile in list(df.index.values):
if post == df.loc[profile, 'post']:
df.loc[profile, 'count'] += 1
else:
df.loc[profile, 'post'] += post
else:
phones = util.get_patterns(util.PHONE_REGEX, post)
emails = util.get_patterns(util.EMAIL_REGEX, post)
if emails or phones:
if len(emails) > 0:
company_url = get_company_url_from_email(emails[0])
else:
company_url = ''
#name_text = scrape_name(browser, profile)
name_text = ''
# By default will assign It to all positions
row = pd.Series({'name': name_text,
'post': post,
'phones': util.print_list(phones),
'emails': util.print_list(emails),
COMPANY_URL: company_url,
'word': word,
'group_name': group_name,
'group_url': group_url,
'count': 1,
WORK_AREA_CODE: 'IT'}, name=profile)
df = df.append(row)
return df
def scroll_down(scroll_steps, browser):
for i in range(scroll_steps):
browser.execute_script(f'window.scrollTo({i * SCREEN_HEIGHT}, {(i + 1) * SCREEN_HEIGHT})')
time.sleep(0.3)
def get_file(name):
with open(name, 'rb', encoding='utf-8') as my_file:
return my_file.readlines()
def scrape_company_url(results, browser, leads_to_filter):
"""
The Angarita automation
:return:
"""
for profile, row in results.iterrows():
if row[COMPANY_URL]:
try:
print(f'browser.get({row[COMPANY_URL]}), ...')
browser.get('http://www.' + row[COMPANY_URL])
html = util.get_html(browser)
emails = util.get_list_from_print(results.loc[profile, EMAILS]) + util.get_patterns(util.EMAIL_REGEX, html)
emails = util.filter_emails(emails)
phones = util.get_list_from_print(results.loc[profile, PHONES]) + util.get_patterns(util.PHONE_REGEX, html)
phones = util.filter_phones(phones)
results.loc[profile, EMAILS] = util.print_list(emails)
results.loc[profile, PHONES] = util.print_list(phones)
except WebDriverException:
print(f'failed to load {row[COMPANY_URL]}, continuing...')
save_leads_to_excel(results, leads_to_filter)
def get_leads_to_filter():
r = requests.post(urllib.parse.urljoin(util.get_root_url(), 'api/get_leads_to_filter'))
print(r.status_code)
if r.status_code != 200:
raise AssertionError('leads to filter cannot be obtained')
return r.json()
def save_leads_in_api(results):
results.fillna('', inplace=True)
r = requests.post(urllib.parse.urljoin(util.get_root_url(), 'api/save_leads'),
{'names': results['name'], 'facebook_urls': results.index.values,
'phones': results['phone'], 'emails': results['email']})
print(r.status_code)
if r.status_code != 200:
raise AssertionError("leads couldn't be saved")
def filter_results_with_leads(results, leads_to_filter):
if FILTER_LEADS:
results = results[results.index.map(lambda x: x not in leads_to_filter)]
return results
def filter_results(results):
leads_to_filter = get_leads_to_filter()
return filter_results_with_leads(results, leads_to_filter)
def save_leads_to_excel(leads, leads_to_filter):
# Escape odd chars and Save partial result
leads = leads.apply(lambda x: x.encode('unicode_escape').decode('utf-8') if isinstance(x, str) else x)
leads = filter_results_with_leads(leads, leads_to_filter)
try:
leads.to_excel('leads.xlsx')
except IllegalCharacterError:
print('caught and IllegalCharacterError while saving leads, will not save and continue...')
def scrape_all(browser):
results = pd.DataFrame(columns=COLUMNS)
leads_to_filter = get_leads_to_filter()
for idx, (group_name, group_url, scroll_steps) in enumerate(values.get_groups()):
print(f'browser.get({group_name}), ...')
browser.get(group_url)
scroll_down(scroll_steps, browser)
html = util.get_html(browser)
try:
for word in values.get_keywords():
results = scrap_word(word=word.lower().replace('\n', ''),
df=results,
html=html,
group_url=group_url,
group_name=group_name)
print(f'scraped word: {word}, done')
save_leads_to_excel(results, leads_to_filter)
print(f'saved results for: {group_name}')
except MemoryError:
pass
scrape_company_url(results, browser, leads_to_filter)
return results
if __name__ == '__main__':
scrape_all(util.load_browser_and_login(FULGENCIO_URL))