github.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /*
  3. This file is part of Resumer
  4. Copyright (C) 2016 Sylvia van Os
  5. Resumer is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU Affero General Public License as
  7. published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Affero General Public License for more details.
  13. You should have received a copy of the GNU Affero General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. declare(strict_types=1);
  17. require_once 'abstract.php';
  18. class GitHubRepoParser extends RepoParser
  19. {
  20. private $repoData;
  21. private static $opts = array(
  22. 'http' => array(
  23. 'method' => "GET",
  24. 'header' => "User-Agent: TheLastProject-Resumer\r\n"
  25. )
  26. );
  27. public function __construct(string $username) {
  28. $this->repoData = json_decode(parent::callApi(sprintf("https://api.github.com/users/%s/repos", $username), self::$opts), true);
  29. // Also grab organisation repositories
  30. $orgs = json_decode(parent::callApi(sprintf("https://api.github.com/users/%s/orgs", $username), self::$opts), true);
  31. foreach ($orgs as $org)
  32. {
  33. $this->repoData = array_merge($this->repoData, json_decode(parent::callApi($org["repos_url"], self::$opts), true));
  34. }
  35. }
  36. public function getIdentifier() : string {
  37. return "GitHub";
  38. }
  39. public function getProfileUrl(string $username) : string {
  40. return sprintf("https://github.com/users/%s", $username);
  41. }
  42. public function getRepos() : array {
  43. return $this->repoData;
  44. }
  45. public function getName(int $repo) : string {
  46. return $this->repoData[$repo]["name"];
  47. }
  48. public function getHomepage(int $repo) : string {
  49. return $this->repoData[$repo]["homepage"] ?? "";
  50. }
  51. public function getRepositoryUrl(int $repo) : string {
  52. return $this->repoData[$repo]["html_url"] ?? "";
  53. }
  54. public function getDescription(int $repo) : string {
  55. return $this->repoData[$repo]["description"] ?? "No description";
  56. }
  57. public function getLanguage(int $repo) : string {
  58. return $this->repoData[$repo]["language"] ?? "Unknown";
  59. }
  60. public function getStarCount(int $repo) : int {
  61. return $this->repoData[$repo]["stargazers_count"];
  62. }
  63. public function getWatcherCount(int $repo) : int {
  64. return $this->repoData[$repo]["watchers_count"];
  65. }
  66. public function getForkCount(int $repo) : int {
  67. return $this->repoData[$repo]["forks_count"];
  68. }
  69. public function getCreationDate(int $repo) : array {
  70. return date_parse($this->repoData[$repo]["created_at"]);
  71. }
  72. public function getLastUpdatedDate(int $repo) : array {
  73. return date_parse($this->repoData[$repo]["updated_at"]);
  74. }
  75. }