Created
August 31, 2014 21:21
-
-
Save purushothblog/271cf2af5a1ec4737f14 to your computer and use it in GitHub Desktop.
This is just a test for Github API. It retrieves Authors, Repos and Commit of the repos.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request, redirect | |
import requests | |
import MySQLdb as mdb | |
from github import Github | |
client_id = '8d4c7fafa31f1f8abf3a' | |
client_secret = 'f42dba103d8ff88399dceb77a3f7a3d9cdb359c8' | |
redirect_url = "https://localhost:5000/home" | |
authorization_base_url = 'https://github.com/login/oauth/authorize' | |
token_url = 'https://github.com/login/oauth/access_token' | |
con = mdb.connect("localhost", "root", "Localhost123", "testapi") | |
app = Flask(__name__) | |
@app.route('/') | |
def hello(): | |
auth_url = "%s?client_id=%s&redirect_url=%s&scope=user,user:email,repo" \ | |
% (authorization_base_url, client_id, redirect_url) | |
return redirect(auth_url) | |
@app.route('/home') | |
def demo(): | |
code = request.args.get('code') | |
print code | |
headers = {'Accept': 'application/json'} | |
payload = { | |
'code': code, 'client_id': client_id, | |
'client_secret': client_secret} | |
post_response = requests.post(token_url, data=payload, headers=headers) | |
acces = post_response.json() | |
print acces['access_token'] | |
ack = acces['access_token'] | |
g = Github(ack) | |
print "Username -> " + g.get_user().name | |
print "Location -> " + g.get_user().location | |
print "Email -> " + g.get_user().email | |
author = g.get_user().email | |
print "List of Repositaries" | |
for repo in g.get_user().get_repos(): | |
print " ---->" + repo.name | |
repos = repo.name | |
data = (author, repos) | |
cur = con.cursor() | |
inst_st = ("INSERT INTO wrepos( Author, RepoName) \ | |
VALUES(%s, %s) ") | |
cur.execute(inst_st, data) | |
con.commit() | |
inst_rp = ("INSERT INTO wlrep(Repos) \ | |
VALUES(%s) ") | |
data = [repos] | |
cur.execute(inst_rp, data) | |
con.commit() | |
print "**************************" | |
for commit in repo.get_commits(): | |
print type(commit) | |
print commit.commit.message | |
comm = commit.commit.message.encode('utf-32') | |
data = [comm] | |
inst_com = "INSERT INTO wlcommits(message) \ | |
VALUES(%s)" | |
cur.execute(inst_com, data) | |
con.commit() | |
return "Success" | |
# select_stmt = "SELECT * FROM repos;" | |
# cur.execute(select_stmt % ('RepoName',)) | |
# select_stmt = "SELECT * FROM repos;" | |
# cur.execute(select_stmt % ('commmits',)) | |
if __name__ == '__main__': | |
app.debug = True | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment