import streamlit as st from github import Github from github import UnknownObjectException import logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) st.set_page_config(page_title="GitHub File Search", page_icon=":mag_right:") access_token = "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" def find_file(repo, filename): contents = repo.get_contents("", ref=repo.default_branch) while contents: content = contents.pop(0) if content.type == "file" and content.name == filename: return content elif content.type == "dir": contents.extend(repo.get_contents(content.path)) def get_repositories_with_file(filename): g = Github(access_token) repositories = g.search_repositories(query=f"filename:{filename}", sort="stars", order="desc") repo_info = [] for repo in repositories: try: content = find_file(repo, filename) if content: repo_info.append({ "name": repo.name, "description": repo.description, "url": repo.html_url }) except UnknownObjectException as e: logger.warning(f"File '{filename}' not found in repository '{repo.full_name}'.") st.warning(f"File '{filename}' not found in repository '{repo.full_name}'.") return repo_info def app(): st.title("GitHub File Search") st.write("Enter a file name to search for on GitHub:") filename = st.text_input("File name") if filename: repo_info = get_repositories_with_file(filename) if len(repo_info) > 0: st.success(f"Found {len(repo_info)} repositories with the file '{filename}':") for repo in repo_info: st.write(f"- **{repo['name']}**: {repo['description']}") st.write(f" URL: [{repo['url']}]({repo['url']})") else: logger.warning(f"No repositories found with the file '{filename}'.") st.warning(f"No repositories found with the file '{filename}'.") st.sidebar.title("GitHub File Search Options") show_logs = st.sidebar.checkbox("Show Logs") if show_logs: st.sidebar.subheader("Logs") with open("app.log", "r") as log_file: logs = log_file.read() st.sidebar.text(logs) if __name__ == "__main__": app()