-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
40 lines (28 loc) · 1.17 KB
/
main.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
from typing import Any
from aws_lambda_powertools import Logger
from aws_lambda_powertools.utilities.typing import LambdaContext
import lib
from crawlers import GithubCrawler, LinkedInCrawler, MediumCrawler
from db.documents import UserDocument
from dispatcher import CrawlerDispatcher
logger = Logger(service="decodingml/crawler")
_dispatcher = CrawlerDispatcher()
_dispatcher.register("medium", MediumCrawler)
_dispatcher.register("linkedin", LinkedInCrawler)
_dispatcher.register("github", GithubCrawler)
def handler(event, context: LambdaContext) -> dict[str, Any]:
first_name, last_name = lib.user_to_names(event.get("user"))
user = UserDocument.get_or_create(first_name=first_name, last_name=last_name)
link = event.get("link")
crawler = _dispatcher.get_crawler(link)
try:
crawler.extract(link=link, user=user)
return {"statusCode": 200, "body": "Link processed successfully"}
except Exception as e:
return {"statusCode": 500, "body": f"An error occurred: {str(e)}"}
if __name__ == "__main__":
event = {
"user": "Paul Iuztin",
"link": "https://www.linkedin.com/in/vesaalexandru/",
}
handler(event, None)