Created
January 24, 2023 04:24
-
-
Save thevickypedia/62582850cca2eca06bb7f2f8ae341783 to your computer and use it in GitHub Desktop.
Check availability of an username and get details of the particular account
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Check availability of an username and get details of the particular account. | |
Requirements: | |
python -m pip install python-dotenv requests | |
""" | |
import json | |
import os | |
import string | |
from typing import Union | |
import dotenv | |
import requests | |
from requests.auth import HTTPBasicAuth | |
dotenv.load_dotenv(dotenv_path=".env", verbose=True) | |
BASE_URL = "https://api.github.com/graphql" | |
class Client: | |
def __init__(self, username: str = os.environ.get('USERNAME') or os.environ.get('username'), | |
git_user: str = os.environ.get('GIT_USER') or os.environ.get('git_user'), | |
git_pass: str = os.environ.get('GIT_PASS') or os.environ.get('git_pass')): | |
if not all((git_user, git_pass, username)): | |
raise ValueError( | |
'git_user, git_pass and username are mandatory args' | |
) | |
self.target = username | |
self.punctuations = [punc for punc in string.punctuation if punc != '-'] | |
self.variable = { | |
"login": username | |
} | |
self.auth = HTTPBasicAuth(username=git_user, password=git_pass) | |
def _check_format(self): | |
"""Checks the format of the target username.""" | |
if any([char in self.punctuations for char in self.target]) or '--' in self.target: | |
exit("Username may only contain alphanumeric characters or single hyphens, and cannot begin or end with a " | |
"hyphen.") | |
def get_availability(self) -> dict: | |
"""Checks if the username is available or not. | |
Returns: | |
dict: | |
""" | |
self._check_format() | |
response = requests.get(f'https://github.com/{self.target}') | |
if response.ok: | |
return {'status': True, 'detail': 'Username is NOT available'} | |
else: | |
return {'status': False, 'detail': 'Username is AVAILABLE'} | |
def fetch(self) -> Union[str, int]: | |
"""Get details of a github account.""" | |
status = self.get_availability() | |
if not status.get('status'): | |
return status.get('detail') | |
query = """ | |
query userInfo($login: String!) { | |
user(login: $login) { | |
name | |
login | |
createdAt | |
starredRepositories { | |
totalCount | |
} | |
repositories { | |
totalCount | |
} | |
following { | |
totalCount | |
} | |
contributionsCollection { | |
totalCommitContributions | |
restrictedContributionsCount | |
hasAnyContributions | |
} | |
} | |
} | |
""" | |
res = requests.post( | |
url=BASE_URL, | |
json={ | |
"query": query, | |
"variables": self.variable | |
}, | |
auth=self.auth | |
) | |
if res.status_code == 200: | |
return json.dumps(res.json().get('data', {}).get('user', {}), indent=4) | |
else: | |
return res.status_code | |
if __name__ == '__main__': | |
print(Client().fetch()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment