@music-league-eras/local-runner 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,58 @@
1
+ from __future__ import annotations
2
+
3
+ import re
4
+ from typing import Optional
5
+
6
+ from bs4 import BeautifulSoup
7
+
8
+ USER_HREF_RE = re.compile(r"^/user/([0-9a-f]{32})/?$", re.I)
9
+ VIEW_PROFILE_LABEL = "view profile"
10
+
11
+
12
+ def extract_viewer_user_id(html: str) -> Optional[str]:
13
+ soup = BeautifulSoup(html or "", "html.parser")
14
+
15
+ a_tag = soup.find(
16
+ "a",
17
+ href=lambda h: bool(h and USER_HREF_RE.match(h)),
18
+ string=lambda s: bool(s and s.strip().lower() == VIEW_PROFILE_LABEL),
19
+ )
20
+ if a_tag and a_tag.get("href"):
21
+ match = USER_HREF_RE.match(a_tag.get("href").strip())
22
+ return match.group(1) if match else None
23
+
24
+ for link in soup.select('a[href^="/user/"]'):
25
+ href = (link.get("href") or "").strip()
26
+ match = USER_HREF_RE.match(href)
27
+ if match:
28
+ return match.group(1)
29
+
30
+ return None
31
+
32
+
33
+ def extract_viewer_avatar_url(html: str) -> Optional[str]:
34
+ soup = BeautifulSoup(html or "", "html.parser")
35
+ viewer_id = extract_viewer_user_id(html)
36
+
37
+ def is_user_link(tag) -> bool:
38
+ href = (tag.get("href") or "").strip()
39
+ match = USER_HREF_RE.match(href)
40
+ return bool(match and (viewer_id is None or match.group(1) == viewer_id))
41
+
42
+ candidate_link = soup.find("a", href=lambda h: bool(h and USER_HREF_RE.match(h)))
43
+ if candidate_link and not is_user_link(candidate_link):
44
+ candidate_link = None
45
+
46
+ if candidate_link:
47
+ parent = candidate_link.find_parent()
48
+ if parent:
49
+ avatar = parent.find("img", src=True)
50
+ if avatar and avatar.get("src"):
51
+ return avatar.get("src")
52
+
53
+ for img in soup.select("img[src]"):
54
+ src = (img.get("src") or "").strip()
55
+ if "musicleague-user-assets" in src and (not viewer_id or viewer_id in src):
56
+ return src
57
+
58
+ return None
@@ -0,0 +1,3 @@
1
+ playwright==1.44.0
2
+ beautifulsoup4==4.12.3
3
+