Skip to content

Instantly share code, notes, and snippets.

@prahladyeri
Created June 22, 2019 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prahladyeri/3cf6dfa96accea7f964d26af3ddc6724 to your computer and use it in GitHub Desktop.
Save prahladyeri/3cf6dfa96accea7f964d26af3ddc6724 to your computer and use it in GitHub Desktop.
Pelican Plugin to fetch a given user's github repos in order of last commits
##
# Repo Puller
#
# @description A plugin to pull your github repositories
# @author Prahlad Yeri<prahladyeri@yahoo.com>
# @license MIT
# @date 2019-06-20
#
# @modified:
# 2019-06-22: Cleaned up source, removed comments.
#
import json, requests
from datetime import datetime
from pelican import signals
"""
***
IMPORTANT: In pelicanconf.py, please set the GITHUB_USERNAME config variable (example: prahladyeri)
***
"""
def parse_date(dt):
format = "%Y-%m-%dT%H:%M:%S"
if dt[-6] in ('+', '-'):
return datetime.strptime(dt, format + '%z')
elif dt[-1] == 'Z':
return datetime.strptime(dt, format + 'Z')
return datetime.strptime(dt, format)
class RepoPuller():
def __init__(self, generator):
url = "https://api.github.com/users/%s/repos" % generator.settings['GITHUB_USERNAME']
resp = requests.get(url, headers={'Accept':'application/vnd.github.inertia-preview+json'})
repos = json.loads(resp.text)
filtered = []
for repo in repos:
if repo['fork']: continue
#if repo['name'] == 'prahladyeri.com': continue
dt = parse_date(repo['pushed_at'])
delta = datetime.now() - dt
# if repo['full_name'] =='prahladyeri/VisualAlchemist':
# print('last pushed at: ', repo['pushed_at'])
# print('dt: ', dt)
# print('delta: ', delta)
if delta.days <= 100:
filtered.append({
'name': repo['name'],
'full_name': repo['full_name'],
'html_url': repo['html_url'],
'description': repo['description'],
'since_last_commit': delta.days,
})
self.repos = sorted(filtered, key=lambda k: k['since_last_commit']) #reverse=True
def fetch(self):
return self.repos
def fetch_github_activity(gen, metadata):
if 'GITHUB_USERNAME' in gen.settings.keys():
gen.context['github_repos'] = gen.plugin_instance.fetch()
def feed_parser_initialization(generator):
print("repo_puller: init")
generator.plugin_instance = RepoPuller(generator)
def register():
print("repo_puller: register")
signals.article_generator_init.connect(feed_parser_initialization)
signals.article_generator_context.connect(fetch_github_activity)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment