Skip to content

Instantly share code, notes, and snippets.

@Rarst
Last active July 28, 2021 10:27
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rarst/5407059 to your computer and use it in GitHub Desktop.
Save Rarst/5407059 to your computer and use it in GitHub Desktop.
Build Gist comments feed for specific user, using GitHub API.
<?php
namespace Rarst\GitHub\Gist;
use GuzzleHttp\Client;
use GuzzleHttp\Command\Guzzle\Description;
use GuzzleHttp\Command\Guzzle\GuzzleClient;
/**
* Build Gist comments feed for specific user, using GitHub API.
*
* Example use:
*
* $feed = new CommentsFeed('Rarst');
* echo $feed->fetch()->render();
*
* Passing own instances of Guzzle and Mustache set up for authorization and caching recommended.
*/
class CommentsFeed
{
const DATE_GITHUB = 'Y-m-d\TH:i:s\Z';
protected $user;
protected $since;
protected $comments = [ ];
public function __construct($user, $since = '-1 month')
{
$this->user = $user;
$this->since = $since;
}
public function fetch(Client $client = null)
{
if (empty( $client )) {
$client = new Client;
}
$description = new Description($this->getServiceDescription());
$guzzleClient = new GuzzleClient($client, $description);
$command = $guzzleClient->getCommand('GetGists', [
'user' => $this->user,
'since' => gmdate(self::DATE_GITHUB, strtotime($this->since)),
]);
$gists = $guzzleClient->execute($command);
foreach ($gists as $gist) {
if (0 === $gist['comments']) {
continue;
}
$command = $guzzleClient->getCommand('GetComments', [ 'id' => $gist['id'] ]);
$comments = $guzzleClient->execute($command);
foreach ($comments as $comment) {
if (0 === strcasecmp($this->user, $comment['user']['login'])) {
continue;
}
$comment['gist_id'] = $gist['id'];
$comment['gist_author'] = $gist['user']['login'];
$filenames = array_keys($gist['files']);
$comment['gist_filename'] = array_shift($filenames);
$comment['updated_at'] = date(DATE_RSS, strtotime($comment['updated_at']));
$this->comments[] = $comment;
}
}
return $this;
}
public function getServiceDescription()
{
return [
'name' => 'Gist',
'apiVersion' => 'v3',
'baseUrl' => 'https://api.github.com/',
'description' => 'Gists and comments access',
'operations' => [
'GetGists' => [
'responseModel' => 'getResponse',
'httpMethod' => 'GET',
'uri' => '/users/{user}/gists',
'parameters' => [
'user' => [
'location' => 'uri',
'required' => true,
],
'since' => [
'location' => 'query',
],
],
],
'GetComments' => [
'responseModel' => 'getResponse',
'httpMethod' => 'GET',
'uri' => '/gists/{id}/comments',
'parameters' => [
'id' => [
'location' => 'uri',
'required' => true,
],
],
],
],
'models' => [
'getResponse' => [
'type' => 'object',
'additionalProperties' => [
'location' => 'json'
]
]
]
];
}
public function render(\Mustache_Engine $mustache = null)
{
if (empty( $mustache )) {
$mustache = new \Mustache_Engine();
}
return $mustache->render(
$this->getRssTemplate(),
[
'user' => $this->user,
'comments' => $this->comments,
]
);
}
public function getRssTemplate()
{
$template = <<<RSS
<?xml version = '1.0' ?>
<rss version="2.0">
<channel>
<title>Gist comments for {{user}}</title>
{{#comments}}
<item>
<title>Comment by {{user.login}} on {{gist_filename}}</title>
<description>{{body}}</description>
<link>https://gist.github.com/{{gist_author}}/{{gist_id}}/#comment-{{id}}</link>
<guid isPermaLink="true">https://gist.github.com/{{gist_author}}/{{gist_id}}/#comment-{{id}}</guid>
<pubDate>{{updated_at}}</pubDate>
</item>
{{/comments}}
</channel>
</rss>
RSS;
return $template;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment