-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
421 lines (365 loc) · 16.4 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import streamlit as st
from pydantic import BaseModel, Field
from typing import List, Optional
import pandas as pd
from scrapegraph_py import SyncClient
import json
import logging
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# Define Pydantic models for GitHub trending developers
class Developer(BaseModel):
username: str = Field(description="Developer's GitHub username")
full_name: Optional[str] = Field(description="Developer's full name")
popular_repo: Optional[str] = Field(description="Name of developer's popular repository")
repo_description: Optional[str] = Field(description="Description of the popular repository")
company: Optional[str] = Field(description="Company the developer works for")
class TrendingDevelopers(BaseModel):
developers: List[Developer] = Field(description="List of trending developers from GitHub")
class Repository(BaseModel):
name: str = Field(description="Repository name")
description: Optional[str] = Field(description="Repository description")
stars: int = Field(description="Number of stars")
language: Optional[str] = Field(description="Primary programming language")
def fetch_trending_developers(api_key):
# Initialize the client
sgai_client = SyncClient(api_key=api_key)
try:
# SmartScraper request with output schema
response = sgai_client.smartscraper(
website_url="https://github.com/trending/developers",
user_prompt="Extract information about trending developers including their username, full name, popular repository, repository description, and company. Parse the data from the articles with class 'Box-row'. Return the data in a copiable JSON format.",
)
sgai_client.close()
logger.info("Successfully fetched trending developers data")
# Add formatted JSON output and download button
if response and 'result' in response:
json_str = json.dumps(response['result'], indent=2)
st.download_button(
label="Download Raw JSON",
data=json_str,
file_name="trending_developers_raw.json",
mime="application/json"
)
st.code(json_str, language='json')
return response['result']
except Exception as e:
logger.error(f"Error fetching trending developers data: {str(e)}")
if 'sgai_client' in locals():
sgai_client.close()
return None
def fetch_github_topics(api_key):
sgai_client = SyncClient(api_key=api_key)
try:
response = sgai_client.smartscraper(
website_url="https://github.com/topics",
user_prompt="Extract featured topics including name and description",
)
sgai_client.close()
return response['result']
except Exception as e:
st.error(f"Error fetching topics: {str(e)}")
if 'sgai_client' in locals():
sgai_client.close()
return None
def fetch_github_explore(api_key):
sgai_client = SyncClient(api_key=api_key)
try:
response = sgai_client.smartscraper(
website_url="https://github.com/explore",
user_prompt="Extract trending repositories including name, description, stars, and programming language. Return the data in a copiable JSON format.",
)
logger.info("Successfully fetched explore data")
# Add formatted JSON output and download button
if response and 'result' in response:
json_str = json.dumps(response['result'], indent=2)
st.download_button(
label="Download Raw JSON",
data=json_str,
file_name="trending_repos_raw.json",
mime="application/json"
)
st.code(json_str, language='json')
sgai_client.close()
return response['result']
except Exception as e:
logger.error(f"Error fetching explore data: {str(e)}")
if 'sgai_client' in locals():
sgai_client.close()
return None
def fetch_github_collections(api_key):
sgai_client = SyncClient(api_key=api_key)
try:
response = sgai_client.smartscraper(
website_url="https://github.com/collections",
user_prompt="Extract collections including title and description",
)
sgai_client.close()
return response['result']
except Exception as e:
st.error(f"Error fetching collections: {str(e)}")
if 'sgai_client' in locals():
sgai_client.close()
return None
def main():
st.set_page_config(
page_title="GitHub Insights",
page_icon="👨💻",
layout="wide"
)
st.title("🌟 GitHub Insights")
st.markdown("---")
# API Key input
api_key = st.sidebar.text_input("Enter Scrapegraph API Key", type="password")
# Add ScrapeGraphAI logo and reference in sidebar
st.sidebar.image("assets/scrapegraphai_logo.png", width=100, use_column_width=True)
st.sidebar.markdown("<div style='text-align: center'><a href='https://scrapegraphai.com/'>Powered by ScrapeGraphAI</a></div><div style='text-align: center'><a href='https://scrapegraphai.com/'>Buy credits</a></div>", unsafe_allow_html=True)
if not api_key:
st.warning("Please enter your Scrapegraph API key in the sidebar to continue. You can buy here the credits: [https://scrapegraphai.com/](https://scrapegraphai.com/)")
return
# Create tabs
tab1, tab2, tab3, tab4 = st.tabs([
"Trending Developers",
"Trending Repos",
"Topics",
"Collections"
])
# Tab 1: Trending Developers (existing functionality)
with tab1:
if st.button("Refresh Developers Data"):
st.session_state.trending_data = fetch_trending_developers(api_key)
if 'trending_data' not in st.session_state:
st.session_state.trending_data = fetch_trending_developers(api_key)
if st.session_state.trending_data:
# Debug: Print the structure of the data
st.write("Raw data structure:", st.session_state.trending_data)
# Convert to DataFrame for better display
developers_data = []
# Try to parse if it's a string
if isinstance(st.session_state.trending_data, str):
try:
raw_developers = json.loads(st.session_state.trending_data)
except json.JSONDecodeError:
st.error("Invalid JSON data received")
raw_developers = []
else:
raw_developers = st.session_state.trending_data
# Ensure we're working with a list
if not isinstance(raw_developers, list):
raw_developers = [raw_developers]
for dev in raw_developers:
if isinstance(dev, str):
try:
dev = json.loads(dev)
except json.JSONDecodeError:
continue
developers_data.append({
"Username": dev['username'] if isinstance(dev, dict) and 'username' in dev else 'N/A',
"Full Name": dev['full_name'] if isinstance(dev, dict) and 'full_name' in dev else 'N/A',
"Popular Repository": dev['popular_repo'] if isinstance(dev, dict) and 'popular_repo' in dev else 'N/A',
"Repository Description": dev['repo_description'] if isinstance(dev, dict) and 'repo_description' in dev else 'N/A',
"Company": dev['company'] if isinstance(dev, dict) and 'company' in dev else 'N/A'
})
df = pd.DataFrame(developers_data)
# Display data
st.dataframe(
df,
use_container_width=True,
hide_index=True
)
col1, col2 = st.columns(2)
# CSV download button
with col1:
csv = df.to_csv(index=False)
st.download_button(
label="Download as CSV",
data=csv,
file_name="github_trending_developers.csv",
mime="text/csv"
)
# JSON download button
with col2:
# Convert the data to JSON format
json_data = json.dumps(st.session_state.trending_data, indent=2)
st.download_button(
label="Download as JSON",
data=json_data,
file_name="github_trending_developers.json",
mime="application/json"
)
else:
st.warning("No data available. Please try refreshing.")
# Tab 2: Explore
with tab2:
if st.button("Refresh Explore Data"):
data = fetch_github_explore(api_key)
logger.info("Refreshing explore data")
logger.debug(f"Fetched explore data: {data}")
st.session_state.explore_data = data
if 'explore_data' not in st.session_state:
st.session_state.explore_data = fetch_github_explore(api_key)
if st.session_state.explore_data:
# Debug: Print the structure of the data
st.write("Raw explore data:", st.session_state.explore_data)
# Convert explore data to DataFrame
repos_data = []
# Handle string data
if isinstance(st.session_state.explore_data, str):
try:
raw_repos = json.loads(st.session_state.explore_data)
except json.JSONDecodeError:
st.error("Invalid JSON data received")
raw_repos = []
else:
raw_repos = st.session_state.explore_data
# Ensure we're working with a list
if not isinstance(raw_repos, list):
raw_repos = [raw_repos]
for repo in raw_repos:
if isinstance(repo, str):
try:
repo = json.loads(repo)
except json.JSONDecodeError:
continue
repos_data.append({
"Repository": repo.get('name', 'N/A'),
"Description": repo.get('description', 'N/A'),
"Stars": repo.get('stars', 0),
"Language": repo.get('language', 'Unknown')
})
explore_df = pd.DataFrame(repos_data)
# Display the DataFrame
st.dataframe(
explore_df,
use_container_width=True,
hide_index=True,
column_config={
"Stars": st.column_config.NumberColumn(
"Stars",
help="Number of GitHub stars",
format="%d ⭐"
),
"Language": st.column_config.TextColumn(
"Language",
help="Primary programming language"
)
}
)
# Add download buttons for Explore data
col1, col2 = st.columns(2)
with col1:
# CSV download
csv = explore_df.to_csv(index=False)
st.download_button(
label="Download Repositories as CSV",
data=csv,
file_name="github_trending_repos.csv",
mime="text/csv"
)
with col2:
# JSON download
json_data = json.dumps(st.session_state.explore_data, indent=2)
st.download_button(
label="Download Repositories as JSON",
data=json_data,
file_name="github_trending_repos.json",
mime="application/json"
)
else:
st.warning("No explore data available. Please try refreshing.")
# Tab 3: Topics (moved from tab2)
with tab3:
if st.button("Refresh Topics Data"):
st.session_state.topics_data = fetch_github_topics(api_key)
if 'topics_data' not in st.session_state:
st.session_state.topics_data = fetch_github_topics(api_key)
if st.session_state.topics_data:
# Convert topics data to DataFrame
if 'featured_topics' in st.session_state.topics_data:
topics_df = pd.DataFrame(st.session_state.topics_data['featured_topics'])
# Display the DataFrame
st.dataframe(
topics_df,
use_container_width=True,
hide_index=True,
column_config={
"name": "Topic Name",
"description": "Description"
}
)
# Add download buttons for Topics data
col1, col2 = st.columns(2)
with col1:
# CSV download
csv = topics_df.to_csv(index=False)
st.download_button(
label="Download Topics as CSV",
data=csv,
file_name="github_topics.csv",
mime="text/csv"
)
with col2:
# JSON download
st.download_button(
label="Download Topics as JSON",
data=json.dumps(st.session_state.topics_data, indent=2),
file_name="github_topics.json",
mime="application/json"
)
else:
st.error("Invalid topics data format received")
else:
st.warning("No topics data available. Please try refreshing.")
# Tab 4: Collections
with tab4:
if st.button("Refresh Collections Data"):
st.session_state.collections_data = fetch_github_collections(api_key)
if 'collections_data' not in st.session_state:
st.session_state.collections_data = fetch_github_collections(api_key)
if st.session_state.collections_data:
# Convert collections data to DataFrame
if 'collections' in st.session_state.collections_data:
collections_df = pd.DataFrame(st.session_state.collections_data['collections'])
# Display the DataFrame
st.dataframe(
collections_df,
use_container_width=True,
hide_index=True,
column_config={
"title": "Collection",
"description": st.column_config.TextColumn(
"Description",
width="large",
help="Collection description"
)
}
)
# Add download buttons for Collections data
col1, col2 = st.columns(2)
with col1:
# CSV download
csv = collections_df.to_csv(index=False)
st.download_button(
label="Download Collections as CSV",
data=csv,
file_name="github_collections.csv",
mime="text/csv"
)
with col2:
# JSON download
st.download_button(
label="Download Collections as JSON",
data=json.dumps(st.session_state.collections_data, indent=2),
file_name="github_collections.json",
mime="application/json"
)
else:
st.error("Invalid collections data format received")
else:
st.warning("No collections data available. Please try refreshing.")
if __name__ == "__main__":
main()