def getGithubApiRequestJson(query: str, variables: dict[Any, Any] | None = None) -> dict[Any, Any]: """Use this to get json from api (returns some data to module variables).""" requestJson = getGithubApiRequest(query, variables).json() if "message" in requestJson: printf.logPrint("[GRAPHQL] Some error has occurred", LogType.ERROR) printf.logPrint(requestJson) return requestJson
def printIssue(issue: dict[Any, Any]): """Print issue function.""" printf.logPrint( ("[\033[91mClosed\033[00m] " if issue["state"] == "closed" else "") + issue["title"], LogType.BOLD, ) printf.logPrint(issue["pushedAt"])
def getGithubApiRequestJson(urlExcBase: str) -> dict[Any, Any]: """Use this to get json from api (returns some data to module variables).""" requestJson = getGithubApiRequest(urlExcBase).json() if "message" in requestJson: printf.logPrint("[REST] Some error has occurred", LogType.ERROR) printf.logPrint(f"{urlExcBase} -> {requestJson}") return requestJson
def mergeData(self, repoName: str, trafficType: str): """Merge traffic type for a repo with the traffic cache Args: repoName (str): name of the repo to get traffic info for trafficType (str): type of traffic. views | clones """ if repoName not in self.data: printf.logPrint(f"{repoName} does not exist - creating", LogType.WARNING) self.data[repoName] = {} if trafficType not in self.data[repoName]: printf.logPrint(f"{trafficType} does not exist - creating", LogType.WARNING) userRepoTrafficType = [{ "timestamp": "2000-01-01T00:00:00Z", "uniques": 0 }] else: userRepoTrafficType = self.data[repoName][trafficType] rawTraffic = getRepoTraffic(repoName, trafficType) days = (getRepoTraffic(repoName, trafficType)[trafficType] if trafficType in rawTraffic else []) for day in days: if getDatetime(userRepoTrafficType[-1]["timestamp"]) < getDatetime( day["timestamp"]): userRepoTrafficType.append({ "timestamp": day["timestamp"], "uniques": day["uniques"] }) self.data[repoName][trafficType] = userRepoTrafficType
def getGithubApiRequest(urlExcBase: str) -> requests.Response: """Use this to get json from api (returns some data to module variables).""" fullUrl = "https://api.github.com/" + urlExcBase request = requests.get(url=fullUrl, auth=AUTH) if int(request.headers["X-RateLimit-Remaining"]) < 1: printf.logPrint( "Remaining rate limit is zero. " f"Try again at {time.ctime(float(request.headers['X-RateLimit-Reset']))}", LogType.ERROR, ) return request
def getGithubApiRequest(query: str, variables: dict[Any, Any] | None = None) -> requests.Response: """Use this to get raw from api (returns some data to module variables).""" variables = variables or {} for key in variables: query = query.replace("$" + key, variables[key]) request = requests.post( "https://api.github.com/graphql", json={"query": query}, headers={"Authorization": "bearer " + getPassword()}, ) if int(request.headers["X-RateLimit-Remaining"]) < 1: printf.logPrint( "Remaining rate limit is zero. " f"Try again at {time.ctime(float(request.headers['X-RateLimit-Reset']))}", LogType.ERROR, ) return request
def forEachRepo(sourceRepo: dict[Any, Any]): """Is source repo alive?""" printStr = ["dead", LogType.ERROR] if github_rest.sourceAlive(sourceRepo, death): printStr = ["alive", LogType.SUCCESS] printf.logPrint( f"Source repo is {printStr[0]}! Head to {sourceRepo['html_url']}", printStr[1]) """Get list of forked repos that are alive and newer than the source repo """ aliveRepos, forkedRepos = github_rest.getListOfAliveForks(repo, death) printf.logPrint( f"{len(aliveRepos)} out of {len(forkedRepos)} Forked repos are alive and newer than the source!", LogType.BOLD, ) for aliveRepo in aliveRepos: github_rest.printRepo(aliveRepo)
def getPaginatedGithubApiRequest(apiUrl: str) -> list[Any]: """Get a api request over multiple pages.""" firstPage = getGithubApiRequest(apiUrl + "?per_page=100") iterable = firstPage.json() try: lastPage = int(firstPage.links["last"]["url"].split("&page=")[1]) except (KeyError, IndexError): lastPage = 1 pageLimit = 10 if lastPage > pageLimit: printf.logPrint( f"There are over {pageLimit} pages! Limiting to {pageLimit} pages", LogType.WARNING, ) lastPage = pageLimit for page in range(2, lastPage + 1): iterationsInstance = getGithubApiRequestJson(apiUrl + "?per_page=100&page=" + str(page)) iterable.extend(iterationsInstance) return iterable
def printRepo(repo: dict[Any, Any]): """Print repo function.""" if all(key in repo for key in ("isArchived", "name")): printf.logPrint( ("[\033[91mArchived\033[00m] " if repo["isArchived"] else "") + repo["name"], LogType.BOLD, ) else: return description = repo["description"] if "description" in repo else "[description]" language = ( repo["primaryLanguage"]["name"] if repo["primaryLanguage"] is not None else "[unknown]" ) licenseName = repo["licenseInfo"]["name"] if repo["licenseInfo"] is not None else "[unknown]" pushed = repo["pushedAt"] if "pushedAt" in repo else "[unknown]" printf.logPrint( f"{description}\nLanguage: {language}, License: {licenseName}, Last Pushed: {pushed}" ) printf.logPrint(f"Link: {repo['url']}")
def printRepo(repo: dict[Any, Any]): """Print repo function.""" if all(key in repo for key in ("archived", "name")): printf.logPrint( ("[\033[91mArchived\033[00m] " if repo["archived"] else "") + repo["name"], LogType.BOLD, ) else: return description = repo[ "description"] if "description" in repo else "[description]" language = repo["language"] if "language" in repo else "[unknown]" try: licenseName = repo["license"]["name"] except (KeyError, TypeError): licenseName = "[unknown]" updated = repo["updated_at"] if "updated_at" in repo else "[unknown]" printf.logPrint( f"{description}\nLanguage: {language}, License: {licenseName}, Last Updated: {updated}" ) printf.logPrint(f"Link: {repo['html_url']}")
"--orgs", action="append", nargs="+", help="add an org to get traffic for", ) parser.add_argument( "-u", "--user", action="store_true", help="return the list of user owned repos?", ) args = parser.parse_args() username = getUsername() if not args.orgs and not args.user: printf.logPrint("Pass at least 1 org or 1 user see --help for more info") sourceRepos = [] for organization in sum(args.orgs, []): sourceRepos += github_graph.getListOfRepos(organization, organization=True) if args.user: sourceRepos += github_graph.getListOfRepos(username) sortRepos = [] for repoData in sourceRepos: repositoryName = repoData["name"] sortRepos.append(( ("(Archived) " if repoData["isArchived"] else "") + repositoryName, repoData["owner"]["login"], repositoryName,
#!/usr/bin/env python3 """Use this program to get a list of forked repos that are active. """ from metprint import LogType from lib import github_graph from lib.utils import getUsernameAndLifespan, printf _username, death = getUsernameAndLifespan() author, repoName = input( "Enter the user and repo name in the form (user/repo - eg. fredhappyface/python.activegithub)\n>" ).split("/") """Is source repo alive? """ sourceRepo = github_graph.getRepo(author, repoName) if github_graph.sourceAlive(sourceRepo, death): printf.logPrint(f"Source repo is alive! Head to {sourceRepo['url']}", LogType.SUCCESS) """Get list of forked repos that are alive and newer than the source repo """ aliveRepos, forkedRepos = github_graph.getListOfAliveForks(sourceRepo, death) printf.logPrint( f"{len(aliveRepos)} out of {len(forkedRepos)} Forked repos are alive and newer than the source!", LogType.BOLD, ) for aliveRepo in aliveRepos: github_graph.printRepo(aliveRepo)
def printGist(gist: dict[Any, Any]): """Print gist function.""" printf.logPrint(gist["description"], LogType.BOLD) printf.logPrint(f"Files: {[gFile['name'] for gFile in gist['files']]}", LogType.BOLD) printf.logPrint(gist["url"])
def printUser(user: dict[Any, Any]): """Print user function.""" printf.logPrint(user["login"], LogType.BOLD) printf.logPrint(user["url"])
help="add an org to get traffic for", ) parser.add_argument( "-u", "--user", action="store_true", help="return the list of user owned repos?", ) args = parser.parse_args() username, lifespan = getUsernameAndLifespan() if args.orgs is None and not args.user: organization = input( "Set the organisation name (hit enter if not applicable)\n>") if len(organization) == 0: printf.logPrint("Organization name not set", LogType.WARNING) sourceRepos = github_graph.getListOfRepos(username) else: sourceRepos = github_graph.getListOfRepos(organization, organization=True) else: sourceRepos = [] for organization in sum(args.orgs, []): sourceRepos += github_graph.getListOfRepos(organization, organization=True) if args.user: sourceRepos += github_graph.getListOfRepos(username) sortRepos = [] cachedData = CachedData() for repoData in sourceRepos:
def printGist(gist: dict[Any, Any]): """Print gist function.""" printf.logPrint(gist["description"], LogType.BOLD) printf.logPrint(f"Files: {list(gist['files'].keys())}", LogType.BOLD) printf.logPrint(gist["html_url"])