broadfield-dev commited on
Commit
f1d73d7
·
verified ·
1 Parent(s): e33cca8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -17
app.py CHANGED
@@ -4,24 +4,20 @@ import base64
4
  import markdown
5
  from bs4 import BeautifulSoup
6
  import os
 
7
 
8
  app = Flask(__name__)
9
 
10
- # GitHub API base URL
11
  GITHUB_API = "https://api.github.com/repos/"
12
 
13
  def get_repo_contents(repo_url):
14
  """Extract contents from GitHub repo URL"""
15
  try:
16
- # Extract owner and repo name from URL
17
  parts = repo_url.rstrip('/').split('/')
18
  owner, repo = parts[-2], parts[-1]
19
-
20
- # Get repository contents
21
  api_url = f"{GITHUB_API}{owner}/{repo}/contents"
22
  response = requests.get(api_url)
23
  response.raise_for_status()
24
-
25
  return owner, repo, response.json()
26
  except Exception as e:
27
  return None, None, str(e)
@@ -32,15 +28,24 @@ def process_file_content(file_info, owner, repo):
32
  file_path = file_info['path']
33
 
34
  if file_info['type'] == 'file':
35
- # Get file content
36
  file_url = f"{GITHUB_API}{owner}/{repo}/contents/{file_path}"
37
  file_response = requests.get(file_url)
38
  file_data = file_response.json()
39
 
40
  if 'content' in file_data:
41
- # Decode base64 content
42
- decoded_content = base64.b64decode(file_data['content']).decode('utf-8')
43
- content = f"### File: {file_path}\n```{(file_path.split('.')[-1] if '.' in file_path else 'text')}\n{decoded_content}\n```\n\n"
 
 
 
 
 
 
 
 
 
 
44
 
45
  return content
46
 
@@ -54,7 +59,6 @@ def create_markdown_document(repo_url):
54
  markdown_content = f"# Repository: {owner}/{repo}\n\n"
55
  markdown_content += "Below are the contents of all files in the repository:\n\n"
56
 
57
- # Process each file recursively
58
  for item in contents:
59
  markdown_content += process_file_content(item, owner, repo)
60
 
@@ -62,19 +66,15 @@ def create_markdown_document(repo_url):
62
 
63
  @app.route('/')
64
  def index():
65
- """Render the main page"""
66
  return render_template('index.html')
67
 
68
  @app.route('/process', methods=['POST'])
69
  def process_repo():
70
- """Process the repository URL and return markdown"""
71
  repo_url = request.json.get('repo_url')
72
  if not repo_url:
73
  return jsonify({'error': 'Please provide a repository URL'}), 400
74
 
75
  markdown_content = create_markdown_document(repo_url)
76
-
77
- # Convert markdown to HTML for display
78
  html_content = markdown.markdown(markdown_content)
79
 
80
  return jsonify({
@@ -82,7 +82,6 @@ def process_repo():
82
  'html': html_content
83
  })
84
 
85
- # HTML template
86
  html_template = """
87
  <!DOCTYPE html>
88
  <html>
@@ -162,11 +161,10 @@ html_template = """
162
  </html>
163
  """
164
 
165
- # Create templates directory and write the HTML file
166
  if not os.path.exists('templates'):
167
  os.makedirs('templates')
168
  with open('templates/index.html', 'w') as f:
169
  f.write(html_template)
170
 
171
  if __name__ == '__main__':
172
- app.run(host="0.0.0.0", port=7860,debug=True)
 
4
  import markdown
5
  from bs4 import BeautifulSoup
6
  import os
7
+ import mimetypes
8
 
9
  app = Flask(__name__)
10
 
 
11
  GITHUB_API = "https://api.github.com/repos/"
12
 
13
  def get_repo_contents(repo_url):
14
  """Extract contents from GitHub repo URL"""
15
  try:
 
16
  parts = repo_url.rstrip('/').split('/')
17
  owner, repo = parts[-2], parts[-1]
 
 
18
  api_url = f"{GITHUB_API}{owner}/{repo}/contents"
19
  response = requests.get(api_url)
20
  response.raise_for_status()
 
21
  return owner, repo, response.json()
22
  except Exception as e:
23
  return None, None, str(e)
 
28
  file_path = file_info['path']
29
 
30
  if file_info['type'] == 'file':
 
31
  file_url = f"{GITHUB_API}{owner}/{repo}/contents/{file_path}"
32
  file_response = requests.get(file_url)
33
  file_data = file_response.json()
34
 
35
  if 'content' in file_data:
36
+ # Guess the file type
37
+ file_extension = file_path.split('.')[-1] if '.' in file_path else ''
38
+ mime_type, _ = mimetypes.guess_type(file_path)
39
+ is_text = mime_type and mime_type.startswith('text') or file_extension in ['py', 'md', 'txt', 'js', 'html', 'css']
40
+
41
+ if is_text:
42
+ try:
43
+ decoded_content = base64.b64decode(file_data['content']).decode('utf-8')
44
+ content = f"### File: {file_path}\n```{(file_extension if file_extension else 'text')}\n{decoded_content}\n```\n\n"
45
+ except UnicodeDecodeError:
46
+ content = f"### File: {file_path}\n[Text content could not be decoded - possibly corrupted or non-UTF-8 text]\n\n"
47
+ else:
48
+ content = f"### File: {file_path}\n[Binary file - {file_data['size']} bytes]\n\n"
49
 
50
  return content
51
 
 
59
  markdown_content = f"# Repository: {owner}/{repo}\n\n"
60
  markdown_content += "Below are the contents of all files in the repository:\n\n"
61
 
 
62
  for item in contents:
63
  markdown_content += process_file_content(item, owner, repo)
64
 
 
66
 
67
  @app.route('/')
68
  def index():
 
69
  return render_template('index.html')
70
 
71
  @app.route('/process', methods=['POST'])
72
  def process_repo():
 
73
  repo_url = request.json.get('repo_url')
74
  if not repo_url:
75
  return jsonify({'error': 'Please provide a repository URL'}), 400
76
 
77
  markdown_content = create_markdown_document(repo_url)
 
 
78
  html_content = markdown.markdown(markdown_content)
79
 
80
  return jsonify({
 
82
  'html': html_content
83
  })
84
 
 
85
  html_template = """
86
  <!DOCTYPE html>
87
  <html>
 
161
  </html>
162
  """
163
 
 
164
  if not os.path.exists('templates'):
165
  os.makedirs('templates')
166
  with open('templates/index.html', 'w') as f:
167
  f.write(html_template)
168
 
169
  if __name__ == '__main__':
170
+ app.run(debug=True)