Skip to content

Commit da13c16

Browse files
committed
Fix regex to strip non a-z content, and adds question mappings
1 parent 9c41c5a commit da13c16

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

lib/insert_contributor_content.py

+25-9
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212

1313
# Imports
1414
import os
15+
import re
1516
import yaml
1617
import json
1718
import html
18-
import requests
19-
import logging
2019
import time
20+
import logging
21+
import requests
2122
from typing import Dict, List, Union, Optional
2223
from requests.exceptions import RequestException
2324

@@ -73,6 +74,7 @@ def handle_rate_limit(response) -> None:
7374
logger.info(f"Sleeping for {sleep_duration} seconds")
7475
time.sleep(sleep_duration)
7576

77+
7678
# Configure Logging
7779
LOG_LEVEL = os.environ.get("LOG_LEVEL", "INFO").upper()
7880
logging.basicConfig(level=LOG_LEVEL)
@@ -113,6 +115,7 @@ def get_profile_picture(username: str) -> str:
113115
:param username: The username of the GitHub user.
114116
:return: The URL of the profile picture of the GitHub user.
115117
"""
118+
logger.info(f"Using backup method to fetch profile picture for user: {username}")
116119
url_to_check = f"https://github.com/{username}.png"
117120
response = requests.get(url_to_check)
118121
if response.status_code == 200:
@@ -135,7 +138,7 @@ def fetch_github_info(username: str) -> Dict[str, Union[str, None]]:
135138
response = requests.get(url, headers=headers)
136139
handle_rate_limit(response)
137140
if response.status_code != 200:
138-
logger.error(f"GitHub API returned status code {response.status_code} for user {username}.")
141+
logger.error(f"GitHub API returned {response.status_code} for @{username}.")
139142
return { "name": username, "avatar_url": get_profile_picture(username) }
140143
logger.info(f"Successfully fetched GitHub info for user: {username}")
141144
return response.json()
@@ -148,9 +151,15 @@ def map_question_to_heading(question: str) -> str:
148151
:return: The mapped question.
149152
"""
150153
question_mappings = {
151-
"What's your 'Aha!' moment with open source?": "My 'Aha!' moment in open source was:",
152-
"What's the coolest open source project you've ever used or contributed to?": "The coolest open source project I've ever used or contributed to is:",
154+
"Why do you want to get into open source?": "I want to get into open source because:",
155+
"What's the coolest open source project you've ever used or come across?": "The coolest open source project I've ever come across is:",
153156
"What advice would you give to someone new to open source?": "The advice I would give to someone new to open source is:",
157+
"Have you ever built or contributed to a project that you'd like to share with us here?": "I've built or contributed to a project that I'd like to share here, which is:",
158+
"Which tech (tools, languages, libraries, etc) do you most use or love?": "The tech (tools, languages, libraries, etc) I most use and love is:",
159+
"What are your go-to resources, for learning new things in open source?": "My go-to resources for learning new things in open source are:",
160+
"What's the most rewarding thing you've experienced in your open-source journey?": "The most rewarding thing I've experienced in my open-source journey is:",
161+
"How do you balance open source work, alongside your day job?": "I balance open source work alongside my day job by:",
162+
"Where do you see open source going in the future?": "I see open source going in the direction of:"
154163
}
155164
return question_mappings.get(question, question)
156165

@@ -204,6 +213,13 @@ def format_url(url: str) -> str:
204213
return url[:25] + (url[25:] and "...")
205214

206215

216+
def format_bio_text(bio: str) -> str:
217+
"""
218+
Returns only a-z, A-Z, 0-9, spaces, and basic punctuation.
219+
"""
220+
return html.escape(re.sub(r"[^a-zA-Z0-9 ]", "", bio).strip())
221+
222+
207223
def build_markdown_content(
208224
contributors: List[Dict[str, str]], stargazers: List[str]
209225
) -> str:
@@ -228,14 +244,15 @@ def build_markdown_content(
228244
info = fetch_github_info(username)
229245
name = info["name"] if info["name"] else username
230246
picture = info.get("avatar_url", PLACEHOLDER_PROFILE_PICTURE)
231-
is_stargazer = "" if username.lower() in [sg.lower() for sg in stargazers] else ""
247+
is_stargazer = "" if username.lower() in [sg.lower() for sg in stargazers] else ""
232248
blog = f"<br /><sup>🌐 [{format_url(info['blog'])}]({info['blog']})</sup>" if info.get("blog") else ""
233-
bio = html.escape(info["bio"]) if info.get("bio") else ""
249+
bio = format_bio_text(info["bio"]) if info.get("bio") else ""
234250

235251
if info.get("public_repos") and info.get("followers") and info.get("following"):
236252
stats = (
237-
f"<br /><kbd>👣 {info['following']}"
253+
f"<br /><kbd title='Followers, following and repo count for {username}'>"
238254
f"🫂 {info['followers']} ┃ "
255+
f"👣 {info['following']} ┃ "
239256
f"🗃 {info['public_repos']}</kbd>"
240257
)
241258
else:
@@ -259,7 +276,6 @@ def build_markdown_content(
259276
return md_content
260277

261278

262-
263279
""" The main entrypoint of the script """
264280
if __name__ == "__main__":
265281
# Fetch list of stargazers (to insert special emoji next to name)

0 commit comments

Comments
 (0)