-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrapers.py
388 lines (336 loc) · 16.2 KB
/
scrapers.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
import json
import os
import sys
from abc import ABC, abstractmethod
from base64 import b64encode
from urllib.request import urlopen
from logging import getLogger, basicConfig
import requests
from bs4 import BeautifulSoup
from playwright.sync_api import sync_playwright
from github import Github
# Show the date/time, logger name, level, and message of the log
basicConfig(format='%(asctime)s %(name)s: [%(levelname)s] %(message)s', level=os.environ.get('LOG_LEVEL', 'INFO'))
class Scraper(ABC):
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:126.0) Gecko/20100101 Firefox/126.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br, zstd',
'DNT': '1',
'Sec-GPC': '1',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'none',
'Sec-Fetch-User': '?1',
'Priority': 'u=1',
}
def __init__(self):
self.logger = getLogger(self.__class__.__qualname__)
self.repository = Github(os.environ['GITHUB_PROFILE_TOKEN']).get_repo(os.environ['GITHUB_REPOSITORY'])
def save_to_github(self, variable_name: str):
"""Save profile data as JSON to a GitHub variable"""
profile_var = self.repository.get_variable(variable_name)
profile_var.edit(json.dumps(self.to_json(), ensure_ascii=False))
self.logger.info(f"Saved profile data to GitHub variable \"{profile_var.name}\"")
@abstractmethod
def to_json(self):
"""Convert profile data to a JSON object"""
raise NotImplementedError
@abstractmethod
def scrap_profile(self, url: str):
"""Scrap profile data from profile page at `url`"""
raise NotImplementedError
class LinkedInProfileScraper(Scraper):
"""
A LinkedIn profile scraper that uses Playwright to scrape profile data from LinkedIn profile pages.
"""
def __init__(self):
super().__init__()
playwright = sync_playwright().start()
browser = playwright.chromium.launch()
self.context = browser.new_context()
self.context.add_init_script(
'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'
)
self.name = None
self.profile_picture_url = None
self.connection_count = None
self.about = None
self.affiliations = None
def set_cookies(self, cookies: list[dict]):
for cookie in cookies:
cookie['sameSite'] = cookie['sameSite'].capitalize()
if cookie['sameSite'] not in ["Strict", "Lax"]:
cookie['sameSite'] = 'None'
if 'expirationDate' in cookie:
cookie['expires'] = cookie['expirationDate']
for key in cookie.copy().keys():
if key not in ['name', 'value', 'domain', 'path', 'expires', 'httpOnly', 'secure', 'sameSite']:
del cookie[key]
self.context.add_cookies(cookies)
def get_cookies(self):
return self.context.cookies('https://www.linkedin.com/')
def load_cookies(self):
"""Load cookies from a GitHub environment variable loaded from GitHub secrets"""
linkedin_cookies = json.loads(os.environ['LINKEDIN_COOKIES'])
self.set_cookies(linkedin_cookies)
self.logger.info(f"Loaded {len(self.get_cookies())} cookies")
def save_cookies(self):
"""Save cookies to GitHub secrets"""
cookies = self.get_cookies()
self.repository.create_secret('LINKEDIN_COOKIES', json.dumps(cookies))
self.logger.info(f"Saved {len(cookies)} cookies")
def scrap_profile(self, url: str):
"""Scrap profile data from LinkedIn profile page at `url`"""
self.logger.info(f"Getting profile for {url}")
self.load_cookies()
page = self.context.new_page()
page.goto(url)
self.logger.info(f"Loaded page: {page.title()}")
main_section = page.locator("main section")
self.name = main_section.locator("h1").text_content().strip()
self.profile_picture_url = main_section.locator(".profile-photo-edit img").get_attribute("src")
self.about = main_section.locator("div.text-body-medium").text_content().strip()
self.connection_count = int(main_section.locator("a[href*='connections'] .t-bold").text_content().strip())
self.affiliations = [
{
"name": affiliation.text_content().strip(),
"logo_url": affiliation.locator("img").get_attribute("src")
}
for affiliation in main_section.locator(".mt2 ul li").all()
]
self.logger.info(f"Got profile data for \"{self.name}\"")
self.logger.info(f"About: {self.about}")
self.logger.info(f"Connection count: {self.connection_count}")
self.logger.info("Affiliations: " + " | ".join([affiliation['name'] for affiliation in self.affiliations]))
self.save_cookies()
def to_json(self):
"""Convert profile data to a JSON object"""
with urlopen(self.profile_picture_url) as f:
profile_picture_base64 = b64encode(f.read()).decode()
for affiliation in self.affiliations:
with urlopen(affiliation['logo_url']) as f:
affiliation['logo_base64'] = b64encode(f.read()).decode()
return {
"name": self.name,
"profile_picture_url": self.profile_picture_url,
"profile_picture_base64": profile_picture_base64,
"about": self.about,
"connection_count": self.connection_count,
"affiliations": self.affiliations
}
def close(self):
self.context.close()
self.context.browser.close()
def __repr__(self):
return f'<{__class__.__name__} name="{self.name}" about="{self.about}" connection_count={self.connection_count}>'
def __str__(self):
return f"{self.name} ({self.connection_count})\n{self.profile_picture_url}\n{self.about}"
class HsoubAcademyScraper(Scraper):
"""
A HsoubAcademy profile scraper that scraps profile data from HsoubAcademy profile.
"""
def __init__(self):
super().__init__()
self.name = None
self.profile_picture_url = None
self.level = None
self.postCount = None
self.reputation = None
self.bestAnswerCount = None
self.about = None
def scrap_profile(self, url: str):
"""Scrap profile data from HsoubAcademy profile page at `url`"""
self.logger.info(f"Getting profile for {url}")
response = requests.get(url, headers=self.headers)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'html.parser')
self.name = soup.find("h1").text.strip()
self.profile_picture_url = soup.find("div", id="elProfilePhoto").find("img")['src']
self.postCount = int(soup.find("div", id="elProfileStats").find("h4").nextSibling.text.strip())
achievements_section = soup.find("div", class_="cProfileAchievements")
self.level = achievements_section.find("img").attrs['alt']
self.reputation = int(achievements_section.find("p", class_="cProfileRepScore").text)
self.bestAnswerCount = int(achievements_section.find("p", class_="cProfileSolutions").text)
personal_info_section = soup.find('div', id='elFollowers').find_next_sibling('div')
self.about = personal_info_section.find('li').find('div').text.strip()
self.logger.info(f"Got profile data for \"{self.name}\"")
self.logger.info(f"Level: {self.level}")
self.logger.info(f"Post count: {self.postCount}")
self.logger.info(f"Reputation: {self.reputation}")
self.logger.info(f"Best answer count: {self.bestAnswerCount}")
self.logger.info(f"About: {self.about}")
def to_json(self):
response = requests.get(self.profile_picture_url, headers=self.headers)
profile_picture_base64 = b64encode(response.content).decode()
return {
"name": self.name,
"profile_picture_url": self.profile_picture_url,
"profile_picture_base64": profile_picture_base64,
"level": self.level,
"postCount": self.postCount,
"reputation": self.reputation,
"bestAnswerCount": self.bestAnswerCount,
"about": self.about
}
class MostaqlReviewsScraper(Scraper):
"""
A Mostaql reviews scraper that scraps reviews data from profile reviews page.
"""
def __init__(self):
super().__init__()
self.average_proficiency = None
self.average_contact = None
self.average_quality = None
self.average_experience = None
self.average_timing = None
self.average_repeat = None
self.reviews = []
def scrap_profile(self, url: str):
"""Scrap reviews data from Mostaql profile reviews page at `url`"""
self.logger.info(f"Getting reviews for {url}")
response = requests.get(url, headers=self.headers)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'html.parser')
average_rating_section = soup.find("div", class_="review--factors_container")
review_factors = average_rating_section.find_all("div", class_="pdn--bs")
def get_review_factor_value(factor):
if text_value := factor.find(class_="pull-left").text.strip():
return float(text_value)
return len(factor.find(class_="pull-left").find_all("i", class_="fa fa-star clr-amber rating-star"))
self.average_proficiency = get_review_factor_value(review_factors[0])
self.average_contact = get_review_factor_value(review_factors[1])
self.average_quality = get_review_factor_value(review_factors[2])
self.average_experience = get_review_factor_value(review_factors[3])
self.average_timing = get_review_factor_value(review_factors[4])
self.average_repeat = get_review_factor_value(review_factors[5])
for review_section in soup.find_all("div", class_="review"):
title = review_section.find(class_="project__title").text.strip()
review_factors = review_section.find_all("div", class_="pdn--bs")
proficiency = get_review_factor_value(review_factors[0])
contact = get_review_factor_value(review_factors[1])
quality = get_review_factor_value(review_factors[2])
experience = get_review_factor_value(review_factors[3])
timing = get_review_factor_value(review_factors[4])
repeat = get_review_factor_value(review_factors[5])
review_author = review_section.find(class_="profile__name").text.strip()
review_text = '\n'.join(review_section.find("div", class_="review__details").stripped_strings)
author_avatar = review_section.find(class_='profile-card--avatar').find("img")['src']
with urlopen(author_avatar) as f:
author_avatar_base64 = b64encode(f.read()).decode()
self.reviews.append({
"title": title,
"proficiency": proficiency,
"contact": contact,
"quality": quality,
"experience": experience,
"timing": timing,
"repeat": repeat,
"author": review_author,
"text": review_text,
"avatar_url": author_avatar,
"avatar_base64": author_avatar_base64
})
self.logger.info(f"Got review for \"{title}\" by \"{review_author}\"")
def to_json(self):
return {
"average_proficiency": self.average_proficiency,
"average_contact": self.average_contact,
"average_quality": self.average_quality,
"average_experience": self.average_experience,
"average_timing": self.average_timing,
"average_repeat": self.average_repeat,
"reviews": self.reviews
}
class KhamsatReviewsScraper(Scraper):
"""
A Khamsat reviews scraper that scraps reviews data from profile reviews page.
"""
def __init__(self):
super().__init__()
self.average_contact = None
self.average_quality = None
self.average_timing = None
self.reviews = []
def scrap_profile(self, url: str):
"""Scrap reviews data from Khamsat profile reviews page at `url`"""
self.logger.info(f"Getting reviews for {url}")
response = requests.get(url, headers=self.headers)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'html.parser')
average_rating_section = soup.find("div", id="reviews-section").find("div", class_="card-body")
review_factors = average_rating_section.find_all("div", class_="text-end")
def get_review_factor_value(factor):
if numeric_value := factor.find(class_="numeric_rate"):
return float(numeric_value.text.strip())
return len(factor.find_all("i", class_="fa fa-star"))
self.average_contact = get_review_factor_value(review_factors[0])
self.average_quality = get_review_factor_value(review_factors[1])
self.average_timing = get_review_factor_value(review_factors[2])
for review_section in soup.find_all("div", class_="review_section"):
review_id = review_section.attrs['id'].split('-')[-1]
title = review_section.find(class_="details-head").find('a').text.strip()
review_factors = review_section.find_all("div", class_="text-end")
contact = get_review_factor_value(review_factors[0])
quality = get_review_factor_value(review_factors[1])
timing = get_review_factor_value(review_factors[2])
review_author = review_section.find(class_="meta--user").text.strip()
review_text = review_section.find("p").text.strip()
author_avatar = review_section.find(class_="meta--avatar").find("img")['src']
with urlopen(author_avatar) as f:
author_avatar_base64 = b64encode(f.read()).decode()
self.reviews.append({
"title": title,
"contact": contact,
"quality": quality,
"timing": timing,
"author": review_author,
"text": review_text,
"link": f"{url}/{review_id}",
"avatar_url": author_avatar,
"avatar_base64": author_avatar_base64
})
self.logger.info(f"Got review for \"{title}\" by \"{review_author}\"")
def to_json(self):
return {
"average_contact": self.average_contact,
"average_quality": self.average_quality,
"average_timing": self.average_timing,
"reviews": self.reviews
}
def run_hsoub_academy_scraper():
profile_scraper = HsoubAcademyScraper()
profile_scraper.scrap_profile(os.environ['HSOUB_ACADEMY_PROFILE_URL'])
profile_scraper.save_to_github('HSOUB_ACADEMY_PROFILE')
def run_mostaql_reviews_scraper():
reviews_scraper = MostaqlReviewsScraper()
reviews_scraper.scrap_profile(os.environ['MOSTAQL_REVIEWS_URL'])
reviews_scraper.save_to_github('MOSTAQL_REVIEWS')
def run_khamsat_reviews_scraper():
reviews_scraper = KhamsatReviewsScraper()
reviews_scraper.scrap_profile(os.environ['KHAMSAT_REVIEWS_URL'])
reviews_scraper.save_to_github('KHAMSAT_REVIEWS')
def run_linkedin_scraper():
profile_scraper = LinkedInProfileScraper()
profile_scraper.scrap_profile(os.environ['LINKEDIN_PROFILE_URL'])
profile_scraper.close()
profile_scraper.save_to_github('LINKEDIN_PROFILE')
if __name__ == '__main__':
if len(sys.argv) == 2:
match sys.argv[1]:
case 'linkedin':
run_linkedin_scraper()
case 'hsoub_academy':
run_hsoub_academy_scraper()
case 'mostaql_reviews':
run_mostaql_reviews_scraper()
case 'khamsat_reviews':
run_khamsat_reviews_scraper()
else:
run_hsoub_academy_scraper()
run_mostaql_reviews_scraper()
run_khamsat_reviews_scraper()
run_linkedin_scraper()