@@ -52,7 +52,7 @@ async def validate_github_username(username: str) -> bool:
5252 data = response .json ()
5353 return data .get ('type' ) == 'User'
5454 except httpx .HTTPError :
55- return True # Fall back to pattern validation on API error
55+ return True
5656
5757 @staticmethod
5858 async def fetch_user_profile (username : str ) -> dict :
@@ -61,8 +61,8 @@ async def fetch_user_profile(username: str) -> dict:
6161 if not await GitHubProfileFetcher .validate_github_username (username ):
6262 raise ValueError (f"Invalid GitHub username: '{ username } '" )
6363
64- one_year_ago = ( datetime .now () - timedelta (days = 365 )). isoformat () + 'Z'
65- one_year_ago_dt = datetime . now () - timedelta ( days = 365 ) # pre-calculated for loops
64+ one_year_ago_dt = datetime .utcnow () - timedelta (days = 365 )
65+ one_year_ago = one_year_ago_dt . isoformat () + 'Z'
6666
6767 graphql_query = {
6868 "query" : f"""
@@ -109,7 +109,7 @@ async def fetch_user_profile(username: str) -> dict:
109109
110110 graphql_data = graphql_response .json ().get ('data' , {}).get ('user' , {})
111111 if not graphql_data :
112- raise ValueError (f"User '{ username } ' not found or query returned no data. " )
112+ raise ValueError (f"User '{ username } ' not found" )
113113
114114 pr_merged_last_year = sum (
115115 1 for pr in graphql_data .get ('pullRequests' , {}).get ('nodes' , [])
@@ -155,56 +155,44 @@ async def fetch_user_profile(username: str) -> dict:
155155
156156 @staticmethod
157157 async def social_accounts (username ):
158- """Fetch social accounts of the user from GitHub API.
159- If the API returns a 404, it attempts to extract social links from the user's README.md.
160-
161- Returns:
162- list: A list of dictionaries, each representing a social account.
163- """
158+ """Fetch social accounts. Returns list or {"error": "..."} for consistency."""
164159 try :
165160 base_url = f"https://api.github.com/users/{ username } /social_accounts"
166161 async with httpx .AsyncClient () as client :
167- user_response = await client .get (
162+ resp = await client .get (
168163 base_url ,
169164 headers = {
170165 "Accept" : "application/vnd.github.v3+json" ,
171- "Authorization" : f"token { Settings .get_github_token ()} " ,
166+ "Authorization" : f"token { Settings .get_github_token ()} "
172167 }
173168 )
174- user_response .raise_for_status ()
175- return user_response .json ()
169+ resp .raise_for_status ()
170+ return resp .json ()
176171 except httpx .HTTPStatusError as e :
177172 if e .response .status_code == 404 :
178173 return await GitHubProfileFetcher .get_social_from_readme (username )
179- return []
174+ return { "error" : f"HTTP Error: { e . response . status_code } " }
180175 except Exception as e :
181176 logger .exception ("Unexpected error in social_accounts for user %s" , username )
182- return []
177+ return { "error" : "Failed to fetch social accounts" }
183178
184179 @staticmethod
185180 async def get_social_from_readme (username ):
186- """Extract LinkedIn link from user's README.md (simplified - only LinkedIn for reliability)"""
181+ """Extract LinkedIn link from README (simplified for reliability)"""
187182 try :
188- readme_url = f"https://api.github.com/repos/{ username } /{ username } /readme"
183+ url = f"https://api.github.com/repos/{ username } /{ username } /readme"
189184 async with httpx .AsyncClient () as client :
190- readme_response = await client .get (
191- readme_url ,
185+ r = await client .get (
186+ url ,
192187 headers = {
193188 "Accept" : "application/vnd.github.v3+json" ,
194- "Authorization" : f"token { Settings .get_github_token ()} " ,
189+ "Authorization" : f"token { Settings .get_github_token ()} "
195190 }
196191 )
197- readme_response .raise_for_status ()
198- readme_content = base64 .b64decode (readme_response .json ()['content' ]).decode ('utf-8' )
199-
200- social_accounts_list = []
201- linkedin_match = re .search (r'linkedin\.com/in/([a-zA-Z0-9-]+)' , readme_content , re .I )
202- if linkedin_match :
203- social_accounts_list .append ({
204- "provider" : "linkedin" ,
205- "url" : f"https://linkedin.com/in/{ linkedin_match .group (1 )} "
206- })
207- return social_accounts_list
192+ r .raise_for_status ()
193+ content = base64 .b64decode (r .json ()['content' ]).decode ('utf-8' )
194+ match = re .search (r'linkedin\.com/in/([a-zA-Z0-9-]+)' , content , re .I )
195+ return [{"provider" : "linkedin" , "url" : f"https://linkedin.com/in/{ match .group (1 )} " }] if match else []
208196 except Exception as e :
209197 logger .exception ("Unexpected error in get_social_from_readme for user %s" , username )
210198 return []
0 commit comments