This repository has been archived by the owner on Jan 14, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from abahlo/master
Add GitHub Plugin
- Loading branch information
Showing
4 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
GitHub Plugin for Kirby | ||
======================= | ||
|
||
With that plugin you can implement a users GitHub information or list your repos easily in Kirby. | ||
|
||
Usage example for repos: | ||
======================== | ||
```php | ||
<?php | ||
$github = new github('abahlo', 10); | ||
|
||
foreach($github->repos() as $repo): | ||
?> | ||
<li> | ||
<h1><a href="<?php echo $repo->url ?>"><?php echo $repo->name ?></a></h1> | ||
<p><?php echo $repo->description ?></p> | ||
</li> | ||
<?php endforeach ?> | ||
``` | ||
|
||
Attributes for repo() | ||
===================== | ||
- `$repo->name` the name of the repository | ||
- `$repo->description` the description | ||
- `$repo->url` the url to the repository | ||
- `$repo->last_update` the timestamp to the last update | ||
- `$repo->forkcount` the number of forks | ||
- `$repo->watchers` the number of watchers | ||
|
||
Attributes for user() | ||
===================== | ||
- `$user->username` the GitHub username | ||
- `$user->name` the real name | ||
- `$user->email` the email, if given | ||
- `$user->followers` the count of followers | ||
- `$user->following` the count of following | ||
- `$user->url` the GitHub-url | ||
- `$user->gravatar_id` the gravatar-id | ||
- `$user->repos_url` the url to the repos |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Changelog | ||
|
||
## v1.0 | ||
|
||
* Initial release |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
/* | ||
* With that plugin you can implement your GitHub information or list your repos easily in Kirby. | ||
* Copyright (c) 2012 by Arne Bahlo | ||
* | ||
*/ | ||
class github { | ||
// Constructor | ||
function __construct($username = null, $count = 0) { | ||
if(is_null($username)) return false; | ||
$this->username = $username; | ||
$this->count = $count; | ||
} | ||
|
||
// Get repos and sort them | ||
public function repos($sorted = false) { | ||
if($this->count <= 0) return false; | ||
$url = 'https://api.github.com/users/' . $this->username . '/repos'; | ||
$data = $this->get_data($url); | ||
|
||
$this->repos = array(); | ||
|
||
foreach ($data as $key => $obj) { | ||
if($key >= $this->count) break; | ||
|
||
$this->repos[$key] = new stdClass; | ||
|
||
$this->repos[$key]->name = $obj->name; | ||
$this->repos[$key]->description = $obj->description; | ||
$this->repos[$key]->url = $obj->html_url; | ||
$this->repos[$key]->last_update = strtotime($obj->updated_at); | ||
$this->repos[$key]->forkount = $obj->forks_count; | ||
$this->repos[$key]->watchers = $obj->watchers_count; | ||
} | ||
|
||
if($sorted) { | ||
function cmp($a, $b) { | ||
if ($b->last_update == $a->last_update) return 0; | ||
return ($b->last_update < $a->last_update) ? -1 : 1; | ||
} | ||
usort($this->repos, 'cmp'); | ||
} | ||
|
||
return $this->repos; | ||
} | ||
|
||
// Get user | ||
public function user() { | ||
$url = 'https://api.github.com/users/' . $this->username; | ||
$data = $this->get_data($url); | ||
|
||
$this->user = new stdClass; | ||
|
||
$this->user->username = $data->login; | ||
$this->user->name = $data->name; | ||
$this->user->email = $data->email; | ||
$this->user->followers = $data->followers; | ||
$this->user->following = $data->following; | ||
$this->user->url = 'https://github.com/' . $data->login; | ||
$this->user->gravatar_id = $data->gravatar_id; | ||
$this->user->repos_url = $data->repos_url; | ||
|
||
return $this->user; | ||
} | ||
|
||
// Fetch data | ||
protected function get_data($url) { | ||
if($url == '') return false; | ||
$handler = curl_init(); | ||
|
||
curl_setopt($handler, CURLOPT_URL, $url); | ||
curl_setopt($handler, CURLOPT_RETURNTRANSFER, 1); | ||
|
||
$data = curl_exec($handler); | ||
curl_close($handler); | ||
|
||
return json_decode($data); | ||
} | ||
} | ||
|
||
?> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "github", | ||
"readable": "GitHub", | ||
"version": "1.0", | ||
"stable": true, | ||
"description": "Fetch GitHub user or repo", | ||
"author": "Arne Bahlo", | ||
"url": "http://arne.me" | ||
} |