def main(args): output = [] g = Github(args.login, args.password) repositories = g.search_repositories(query='topic:machine-learning') prefix = 'https://github.com/' for i, repo in enumerate(repositories): #reponame = re.sub(prefix, '', repo) try: reponame = repo.full_name gs = GitHubStargazer(reponame) stargazers = gs.get_all_stargazers() for stargazer in stargazers: user = g.get_user(stargazer) if args.email_required and not user.email: continue if args.company_required and not user.company: continue if args.location_required and not user.location: continue output.append({ 'name': user.name, 'email': user.email, 'company': user.company, 'location': user.location, 'repo': reponame }) pd.DataFrame.from_records(output).to_csv(args.output_file, sep='\t', index=False) print(f'Repo {reponame} --> {len(stargazers)} stargazers found.') except RateLimitExceededException: time.sleep(1000) except Exception: pass
def test_get_all_stargazers_sorts_stargazers(url_page_content_1: str, ok_status_code: int) -> None: responses.add(responses.GET, "https://github.com/foo/bar/stargazers?page=1", body=url_page_content_1, status=ok_status_code) assert GitHub("foo/bar").get_all_stargazers() == sorted(['foo', 'bar'])
def test_get_all_stargazers_returns_empty_on_page_without_stargazers( url_page_content_without_stargazers: str, ok_status_code: int) -> None: responses.add(responses.GET, "https://github.com/foo/bar/stargazers?page=1", body=url_page_content_without_stargazers, status=ok_status_code) assert GitHub("foo/bar").get_all_stargazers() == []
def test_provided_user_is_not_stargazer_on_page_without_stargazers( url_page_content_without_stargazers: str, ok_status_code: int) -> None: responses.add(responses.GET, "https://github.com/foo/bar/stargazers?page=1", body=url_page_content_without_stargazers, status=ok_status_code) assert not GitHub("foo/bar").is_stargazer("another_foo")
def __get_github(self) -> typing.Optional[GitHub]: try: github = GitHub(self.__username_and_repository) except (UsernameRepositoryError, UrlNotFoundError) as exception_message: Halo().fail(exception_message) return None return github
def test_get_all_stargazers_on_too_many_requests_raises( url_page_content_1: str, too_many_requests_status_code: int) -> None: responses.add(responses.GET, "https://github.com/foo/bar/stargazers?page=1", body=url_page_content_1, status=too_many_requests_status_code) with pytest.raises(TooManyRequestsHttpError): GitHub("foo/bar").get_all_stargazers()
def test_wrong_href_content_raises(wrong_href_content: str, ok_status_code: int) -> None: responses.add(responses.GET, "https://github.com/foo/bar/stargazers?page=1", body=get_page_content_with_href(wrong_href_content), status=ok_status_code) with pytest.raises(HrefContentError): GitHub("foo/bar").is_stargazer("foo")
def test_provided_user_with_missing_href_attribute( url_page_content_no_href: str, ok_status_code: int) -> None: responses.add(responses.GET, "https://github.com/foo/bar/stargazers?page=1", body=url_page_content_no_href, status=ok_status_code) with pytest.raises(MissingHrefAttributeError): GitHub("foo/bar").is_stargazer("foo")
def test_provided_user_with_missing_hyperlink_tag( url_page_content_no_hyperlink: str, ok_status_code: int) -> None: responses.add(responses.GET, "https://github.com/foo/bar/stargazers?page=1", body=url_page_content_no_hyperlink, status=ok_status_code) with pytest.raises(MissingHyperlinkTagError): GitHub("foo/bar").is_stargazer("foo")
def test_provided_user_on_invalid_page(url_page_content_1: str, not_found_status_code: int) -> None: responses.add(responses.GET, "https://github.com/foo/bar/stargazers?page=1", body=url_page_content_1, status=not_found_status_code) with pytest.raises(UrlNotFoundError): GitHub("foo/bar").is_stargazer("foo")
def test_get_all_stargazers_on_invalid_user_repo_raises( url_page_content_1: str, invalid_user_and_repo: str, not_found_status_code: int) -> None: responses.add(responses.GET, "https://github.com/" + invalid_user_and_repo + "/stargazers?page=1", body=url_page_content_1, status=not_found_status_code) with pytest.raises(UrlNotFoundError): GitHub(invalid_user_and_repo).get_all_stargazers()
def test_provided_user_is_stargazer_on_last_page(url_page_content_1: str, url_page_content_2: str, ok_status_code: int) -> None: responses.add(responses.GET, "https://github.com/foo/bar/stargazers?page=1", body=url_page_content_1, status=ok_status_code) responses.add(responses.GET, "https://github.com/foo/bar/stargazers?page=2", body=url_page_content_2, status=ok_status_code) assert GitHub("foo/bar").is_stargazer("bar2")
def test_wrong_argument_raises() -> None: wrong_arguments: typing.List[str] = ["foo", "foo/", "/bar", "/", "//", ""] for wrong_argument in wrong_arguments: with pytest.raises(UsernameRepositoryError): GitHub(wrong_argument)