Search code examples
htmlruby-on-railsrubygithubhtml-parsing

How can I add "current streak" of contributions from github to my blog?


I have a personal blog I built using rails. I want to add a section to my site that displays my current streak of github contributions. What would be the best way about doing this?

edit: for clarification, here is what I want:

enter image description here

just the number of days is all that is necessary for me.


Solution

  • Considering the GitHub API for Users doesn't yet expose that particular information (number of days for current stream of contributions), you might have to:

    • scrape it (extract it by reading the user's GitHub page)
      As klamping mentions in his answer (upvoted), the url to scrap would be:
      https://github.com/users/<username>/contributions_calendar_data
      https://github.com/users/<username>/contributions
      (for public repos only, though)

      SherlockStd has an updated (May 2017) parsing code below:

      https://github-stats.com/api/user/streak/current/:username
      
    • try projects which are using https://github.com/users/<username>/contributions_calendar_data (as listed in Marques Johansson's answer, upvoted)

    git-stats

    https://github.com/akerl/githubchart

    https://raw.github.com/k4rthik/git-cal/master/screenshots/img1.png

    git-cal is a simple script to view commits calendar (similar to GitHub contributions calendar) on command line.
    Each block in the graph corresponds to a day and is shaded with one of the 5 possible colors, each representing relative number of commits on that day.

    • or establish a service that will report, each day, any new commit for that given day to a Google Calendar (using the Google Calendar API through a project like nf/streak).
      You can then read that information and report it in your blog.

    Google Calendar streak


    You can find various example of scraping that information:

    As in:

    $.getJSON('https://github.com/users/' + location.pathname.replace(/\//g, '') + '/contributions_calendar_data', weekendWork);
    

    Like:

    leaderboard = members.map do |u|
      user_stats = get("https://github.com/users/#{u}/contributions_calendar_data")
      total = user_stats.map { |s| s[1] }.reduce(&:+)
      [u, total]
    end
    
    • ... (you get the idea)