Pranesh64 commited on
Commit
8daa0a9
Β·
verified Β·
1 Parent(s): 1cc8888

Fixed oauth never called

Browse files
Files changed (1) hide show
  1. app.py +74 -51
app.py CHANGED
@@ -1,7 +1,6 @@
1
  import gradio as gr
2
  import requests
3
  import os
4
- from datetime import datetime
5
  from dotenv import load_dotenv
6
 
7
  load_dotenv()
@@ -11,15 +10,38 @@ GITHUB_CLIENT_SECRET = os.getenv("GITHUB_CLIENT_SECRET")
11
 
12
  GITHUB_GRAPHQL = "https://api.github.com/graphql"
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  def github_graphql(query, token):
15
  headers = {
16
  "Authorization": f"Bearer {token}",
17
- "Content-Type": "application/json"
18
  }
19
- r = requests.post(GITHUB_GRAPHQL, json={"query": query}, headers=headers)
20
  r.raise_for_status()
21
  return r.json()
22
 
 
23
  def fetch_github_wrapped(token):
24
  query = """
25
  query {
@@ -29,49 +51,48 @@ def fetch_github_wrapped(token):
29
  from: "2025-01-01T00:00:00Z",
30
  to: "2025-12-31T23:59:59Z"
31
  ) {
32
- contributionCalendar {
33
- totalContributions
34
- }
35
  totalCommitContributions
36
  totalPullRequestContributions
37
  totalIssueContributions
38
  }
39
- repositories(first: 100, ownerAffiliations: OWNER, orderBy: {field: STARGAZERS, direction: DESC}) {
 
 
 
 
40
  nodes {
41
  name
42
  stargazerCount
43
- primaryLanguage {
44
- name
45
- }
46
  }
47
  }
48
  }
49
  }
50
  """
51
 
52
- data = github_graphql(query, token)["data"]["viewer"]
 
53
 
54
- repos = data["repositories"]["nodes"]
55
  languages = {}
56
-
57
  for r in repos:
58
  if r["primaryLanguage"]:
59
  lang = r["primaryLanguage"]["name"]
60
  languages[lang] = languages.get(lang, 0) + 1
61
 
62
- top_language = max(languages, key=languages.get) if languages else "Unknown"
63
- top_repo = repos[0]["name"] if repos else "None"
64
-
65
  return {
66
- "username": data["login"],
67
- "commits": data["contributionsCollection"]["totalCommitContributions"],
68
- "prs": data["contributionsCollection"]["totalPullRequestContributions"],
69
- "issues": data["contributionsCollection"]["totalIssueContributions"],
70
- "total": data["contributionsCollection"]["contributionCalendar"]["totalContributions"],
71
- "top_language": top_language,
72
- "top_repo": top_repo
73
  }
74
 
 
 
 
75
  def render_wrapped(stats):
76
  return f"""
77
  <div style="
@@ -79,14 +100,14 @@ def render_wrapped(stats):
79
  padding: 40px;
80
  background: linear-gradient(135deg,#0f2027,#203a43,#2c5364);
81
  color: white;
82
- border-radius: 20px;
83
- max-width: 800px;
84
  margin: auto;
85
  ">
86
- <h1 style="font-size: 3em; text-align:center;">πŸŽ‰ GitHub Wrapped 2025</h1>
87
  <h2 style="text-align:center;">@{stats['username']}</h2>
88
 
89
- <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(200px,1fr));gap:20px;margin-top:40px;">
90
  <div class="card">πŸ”₯ {stats['total']} Contributions</div>
91
  <div class="card">πŸ’» {stats['commits']} Commits</div>
92
  <div class="card">πŸ” {stats['prs']} PRs</div>
@@ -94,9 +115,9 @@ def render_wrapped(stats):
94
  </div>
95
 
96
  <div style="margin-top:40px;">
97
- <h3>✨ Your Coding Personality</h3>
98
  <p><b>Favorite Language:</b> {stats['top_language']}</p>
99
- <p><b>Most Starred Repo:</b> {stats['top_repo']}</p>
100
  </div>
101
 
102
  <p style="text-align:center;margin-top:40px;">
@@ -107,39 +128,41 @@ def render_wrapped(stats):
107
  <style>
108
  .card {{
109
  background: rgba(255,255,255,0.15);
110
- padding: 25px;
111
- border-radius: 15px;
112
  font-size: 1.4em;
113
- text-align: center;
114
  font-weight: 600;
 
115
  backdrop-filter: blur(8px);
116
  }}
117
  </style>
118
  """
119
 
120
- def wrapped_with_token(token):
121
- stats = fetch_github_wrapped(token)
122
- return render_wrapped(stats)
123
 
124
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
125
- gr.Markdown("## 🎁 GitHub Wrapped 2025")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
 
127
- gr.Markdown("""
128
- πŸ‘‰ Login with GitHub to generate your personalized Wrapped.
129
- """)
130
 
131
- token_input = gr.Textbox(
132
- label="GitHub OAuth Token",
133
- placeholder="Token is received automatically after OAuth",
134
- visible=False
135
- )
136
 
 
137
  output = gr.HTML()
138
-
139
- gr.Button("Generate My GitHub Wrapped πŸš€").click(
140
- wrapped_with_token,
141
- inputs=token_input,
142
- outputs=output
143
- )
144
 
145
  demo.launch()
 
1
  import gradio as gr
2
  import requests
3
  import os
 
4
  from dotenv import load_dotenv
5
 
6
  load_dotenv()
 
10
 
11
  GITHUB_GRAPHQL = "https://api.github.com/graphql"
12
 
13
+
14
+ # ---------------- OAuth helpers ----------------
15
+
16
+ def exchange_code_for_token(code: str) -> str:
17
+ url = "https://github.com/login/oauth/access_token"
18
+ headers = {"Accept": "application/json"}
19
+ data = {
20
+ "client_id": GITHUB_CLIENT_ID,
21
+ "client_secret": GITHUB_CLIENT_SECRET,
22
+ "code": code,
23
+ }
24
+ r = requests.post(url, headers=headers, data=data, timeout=20)
25
+ r.raise_for_status()
26
+
27
+ token = r.json().get("access_token")
28
+ if not token:
29
+ raise RuntimeError("GitHub OAuth failed: no access token returned")
30
+ return token
31
+
32
+
33
+ # ---------------- GitHub API ----------------
34
+
35
  def github_graphql(query, token):
36
  headers = {
37
  "Authorization": f"Bearer {token}",
38
+ "Content-Type": "application/json",
39
  }
40
+ r = requests.post(GITHUB_GRAPHQL, json={"query": query}, headers=headers, timeout=30)
41
  r.raise_for_status()
42
  return r.json()
43
 
44
+
45
  def fetch_github_wrapped(token):
46
  query = """
47
  query {
 
51
  from: "2025-01-01T00:00:00Z",
52
  to: "2025-12-31T23:59:59Z"
53
  ) {
54
+ contributionCalendar { totalContributions }
 
 
55
  totalCommitContributions
56
  totalPullRequestContributions
57
  totalIssueContributions
58
  }
59
+ repositories(
60
+ first: 100,
61
+ ownerAffiliations: OWNER,
62
+ orderBy: { field: STARGAZERS, direction: DESC }
63
+ ) {
64
  nodes {
65
  name
66
  stargazerCount
67
+ primaryLanguage { name }
 
 
68
  }
69
  }
70
  }
71
  }
72
  """
73
 
74
+ viewer = github_graphql(query, token)["data"]["viewer"]
75
+ repos = viewer["repositories"]["nodes"]
76
 
 
77
  languages = {}
 
78
  for r in repos:
79
  if r["primaryLanguage"]:
80
  lang = r["primaryLanguage"]["name"]
81
  languages[lang] = languages.get(lang, 0) + 1
82
 
 
 
 
83
  return {
84
+ "username": viewer["login"],
85
+ "total": viewer["contributionsCollection"]["contributionCalendar"]["totalContributions"],
86
+ "commits": viewer["contributionsCollection"]["totalCommitContributions"],
87
+ "prs": viewer["contributionsCollection"]["totalPullRequestContributions"],
88
+ "issues": viewer["contributionsCollection"]["totalIssueContributions"],
89
+ "top_language": max(languages, key=languages.get) if languages else "Unknown",
90
+ "top_repo": repos[0]["name"] if repos else "None",
91
  }
92
 
93
+
94
+ # ---------------- UI ----------------
95
+
96
  def render_wrapped(stats):
97
  return f"""
98
  <div style="
 
100
  padding: 40px;
101
  background: linear-gradient(135deg,#0f2027,#203a43,#2c5364);
102
  color: white;
103
+ border-radius: 24px;
104
+ max-width: 900px;
105
  margin: auto;
106
  ">
107
+ <h1 style="text-align:center;font-size:3em;">πŸŽ‰ GitHub Wrapped 2025</h1>
108
  <h2 style="text-align:center;">@{stats['username']}</h2>
109
 
110
+ <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(220px,1fr));gap:20px;margin-top:40px;">
111
  <div class="card">πŸ”₯ {stats['total']} Contributions</div>
112
  <div class="card">πŸ’» {stats['commits']} Commits</div>
113
  <div class="card">πŸ” {stats['prs']} PRs</div>
 
115
  </div>
116
 
117
  <div style="margin-top:40px;">
118
+ <h3>✨ Your Coding Style</h3>
119
  <p><b>Favorite Language:</b> {stats['top_language']}</p>
120
+ <p><b>Top Starred Repo:</b> {stats['top_repo']}</p>
121
  </div>
122
 
123
  <p style="text-align:center;margin-top:40px;">
 
128
  <style>
129
  .card {{
130
  background: rgba(255,255,255,0.15);
131
+ padding: 26px;
132
+ border-radius: 18px;
133
  font-size: 1.4em;
 
134
  font-weight: 600;
135
+ text-align: center;
136
  backdrop-filter: blur(8px);
137
  }}
138
  </style>
139
  """
140
 
 
 
 
141
 
142
+ def router(request: gr.Request):
143
+ code = request.query_params.get("code")
144
+
145
+ if not code:
146
+ return f"""
147
+ <div style="text-align:center;padding:40px;">
148
+ <h1>🎁 GitHub Wrapped 2025</h1>
149
+ <a href="https://github.com/login/oauth/authorize
150
+ ?client_id={GITHUB_CLIENT_ID}
151
+ &scope=read:user repo">
152
+ <button style="font-size:18px;padding:14px 24px;border-radius:10px;">
153
+ Login with GitHub
154
+ </button>
155
+ </a>
156
+ </div>
157
+ """
158
 
159
+ token = exchange_code_for_token(code)
160
+ stats = fetch_github_wrapped(token)
161
+ return render_wrapped(stats)
162
 
 
 
 
 
 
163
 
164
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
165
  output = gr.HTML()
166
+ demo.load(router, None, output)
 
 
 
 
 
167
 
168
  demo.launch()