Skip to content
This repository has been archived by the owner on Jan 14, 2019. It is now read-only.

Commit

Permalink
Merge pull request #26 from abahlo/master
Browse files Browse the repository at this point in the history
Add GitHub Plugin
  • Loading branch information
bastianallgeier committed Dec 19, 2012
2 parents a47cc6a + 6c6a41e commit 7e91f41
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
39 changes: 39 additions & 0 deletions plugins/github/README.md
@@ -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
5 changes: 5 additions & 0 deletions plugins/github/changelog.md
@@ -0,0 +1,5 @@
# Changelog

## v1.0

* Initial release
81 changes: 81 additions & 0 deletions plugins/github/github.php
@@ -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);
}
}

?>
9 changes: 9 additions & 0 deletions plugins/github/package.json
@@ -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"
}

0 comments on commit 7e91f41

Please sign in to comment.