Skip to content

Instantly share code, notes, and snippets.

@haideralipunjabi
Created April 16, 2017 11:18
Show Gist options
  • Save haideralipunjabi/5d54f6c75f23d211ca2a5e47f92b4443 to your computer and use it in GitHub Desktop.
Save haideralipunjabi/5d54f6c75f23d211ca2a5e47f92b4443 to your computer and use it in GitHub Desktop.
Simple Django View to return number of each language used by a user in their Github Repositories (pulic & private)
from rest_framework.views import APIView
from rest_framework.response import Response
import requests
import os
import simplejson
# Simple Django View to return number of each language used by a user in their Github Repositories (pulic & private)
# To use, add environment variables, GITHUB_USERNAME = <your-github-username> & GITHUB_PAT = <github-personal-access-token-with-repo-access>
class LanguageView(APIView):
def get(self, request):
response = requests.get('https://api.github.com/user/repos', auth=(os.environ.get('GITHUB_USERNAME'), os.environ.get('GITHUB_PAT')))
jsdata = simplejson.loads(response.text)
data = {}
for repo in jsdata:
langres = requests.get(str(repo['languages_url']), auth=(os.environ.get('GITHUB_USERNAME'), os.environ.get('GITHUB_PAT')))
langdata = simplejson.loads(langres.text)
for lang in langdata:
if lang in data:
data[lang] += 1
else:
data[lang] = 1
response = Response(data)
response["Access-Control-Allow-Origin"] = "*"
response["Access-Control-Allow-Methods"] = "GET, OPTIONS"
response["Access-Control-Max-Age"] = "1000"
response["Access-Control-Allow-Headers"] = "X-Requested-With, Content-Type"
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment