Not-Grim-Refer commited on
Commit
f1f576a
1 Parent(s): e113367

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -41
app.py CHANGED
@@ -1,75 +1,70 @@
1
- import os
2
  import streamlit as st
3
  from github import Github
4
- from github import GithubException
5
- from altair import Chart
6
 
7
- # Set up the Streamlit app
8
- st.set_page_config(page_title="GitHub File Search", page_icon=":mag_right:", theme="dark")
9
 
10
- # Retrieve the GitHub API token from the environment variable
11
- access_token = os.environ["GITHUB_TOKEN"]
12
 
13
- # Define a function to get all the repositories that contain a file with the given name
14
- def get_repositories_with_file(filename):
15
- # Create a GitHub client
16
- g = Github(access_token)
17
 
18
- # Search for the file on GitHub
19
- query = f"filename:{filename}"
20
- repositories = g.search_repositories(query=query, sort="stars", order="desc")
 
 
 
 
 
 
 
 
21
 
22
- # Initialize a list to store the repository information
 
 
23
  repo_info = []
24
 
25
- # Loop through the repositories
26
  for repo in repositories:
27
- # Get the contents of the repository
28
- contents = repo.get_contents("")
29
-
30
- # Check if the repository contains a file with the given name
31
- for content in contents:
32
- if content.name == filename:
33
  repo_info.append({
34
  "name": repo.name,
35
  "description": repo.description,
36
  "url": repo.html_url
37
  })
38
- break
39
-
40
- # Return the repository information
41
  return repo_info
42
 
43
- # Define the Streamlit app
44
  def app():
45
- # Set up the user interface
46
  st.title("GitHub File Search")
47
  st.write("Enter a file name to search for on GitHub:")
48
  filename = st.text_input("File name")
49
-
50
  if filename:
51
- # Get the repositories with the file
52
  repo_info = get_repositories_with_file(filename)
53
-
54
- # Display the repository information
55
  if len(repo_info) > 0:
56
  st.success(f"Found {len(repo_info)} repositories with the file '{filename}':")
57
  for repo in repo_info:
58
  st.write(f"- **{repo['name']}**: {repo['description']}")
59
  st.write(f" URL: [{repo['url']}]({repo['url']})")
60
  else:
 
61
  st.warning(f"No repositories found with the file '{filename}'.")
62
 
63
- # Create a chart to show the number of repositories found for each file name
64
- chart = Chart(data=repo_info).mark_bar().encode(
65
- x='name',
66
- y='count',
67
- color='name'
68
- )
 
69
 
70
- # Display the chart
71
- st.plotly_chart(chart)
72
 
73
- # Run the Streamlit app
74
  if __name__ == "__main__":
75
- app()
 
 
1
  import streamlit as st
2
  from github import Github
3
+ from github import UnknownObjectException
4
+ import logging
5
 
6
+ logging.basicConfig(level=logging.INFO)
7
+ logger = logging.getLogger(__name__)
8
 
9
+ st.set_page_config(page_title="GitHub File Search", page_icon=":mag_right:")
10
+ access_token = "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"
11
 
 
 
 
 
12
 
13
+ def find_file(repo, path, filename):
14
+ contents = repo.get_contents(path)
15
+ for content in contents:
16
+ if content.type == "dir":
17
+ result = find_file(repo, content.path, filename)
18
+ if result:
19
+ return result
20
+ elif content.name == filename:
21
+ return content
22
+ return None
23
+
24
 
25
+ def get_repositories_with_file(filename):
26
+ g = Github(access_token)
27
+ repositories = g.search_repositories(query=f"filename:{filename}", sort="stars", order="desc")
28
  repo_info = []
29
 
 
30
  for repo in repositories:
31
+ try:
32
+ content = find_file(repo, "", filename)
33
+ if content:
 
 
 
34
  repo_info.append({
35
  "name": repo.name,
36
  "description": repo.description,
37
  "url": repo.html_url
38
  })
39
+ except UnknownObjectException as e:
40
+ logger.warning(f"File '{filename}' not found in repository '{repo.full_name}'.")
41
+ st.warning(f"File '{filename}' not found in repository '{repo.full_name}'.")
42
  return repo_info
43
 
44
+
45
  def app():
 
46
  st.title("GitHub File Search")
47
  st.write("Enter a file name to search for on GitHub:")
48
  filename = st.text_input("File name")
 
49
  if filename:
 
50
  repo_info = get_repositories_with_file(filename)
 
 
51
  if len(repo_info) > 0:
52
  st.success(f"Found {len(repo_info)} repositories with the file '{filename}':")
53
  for repo in repo_info:
54
  st.write(f"- **{repo['name']}**: {repo['description']}")
55
  st.write(f" URL: [{repo['url']}]({repo['url']})")
56
  else:
57
+ logger.warning(f"No repositories found with the file '{filename}'.")
58
  st.warning(f"No repositories found with the file '{filename}'.")
59
 
60
+ st.sidebar.title("GitHub File Search Options")
61
+ show_logs = st.sidebar.checkbox("Show Logs")
62
+ if show_logs:
63
+ st.sidebar.subheader("Logs")
64
+ with open("app.log", "r") as log_file:
65
+ logs = log_file.read()
66
+ st.sidebar.text(logs)
67
 
 
 
68
 
 
69
  if __name__ == "__main__":
70
+ app()