Skip to content

Commit

Permalink
update(feat): profile page with md
Browse files Browse the repository at this point in the history
  • Loading branch information
Rooyca committed Jun 6, 2024
1 parent 6182fa7 commit 7858d75
Show file tree
Hide file tree
Showing 9 changed files with 574 additions and 34 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ fly deploy
- [GitHub API](https://docs.github.com/en/rest)
- [Font Awesome](https://fontawesome.com/)

## 📝 TODO

- [ ] Add nix support

## 📄 License

[MIT](LICENSE)
30 changes: 30 additions & 0 deletions app/data/rooyca.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!-- Last updated: 2024-06-06 -->

<!-- Profile Picture -->
- profile-picture: https://avatars.githubusercontent.com/u/42101257?v=4

<!-- Name -->
# rooyca

<!-- Short Bio -->
| Pronouns: he/him
| Occupation: Studend
| Location: Colombia

--- Bio ---
<!-- MAX 300 characters -->

Crazy? I Was Crazy Once. They Locked Me In A Room. A Rubber Room. A Rubber Room With Rats. And Rats Make Me Crazy.

--- Profile Items ---
<!-- Available profile items: Twitter, Facebook, Instagram, Unsplash, Dribbble, YouTube, Dev.to, Linkedin, Github, Buy Me a Coffee -->

- [Twitter](https://twitter.com/rooycaa)
- [Dev.to](https://dev.to/rooyca)
- [Linkedin](https://linkedin.com/rooyca)
- [Github](https://github.com/rooyca)
- [Buy Me a Coffee](https://buymeacoffee.com/rooyca)


<!-- Don't change anything after this line -->
--- END ---
2 changes: 1 addition & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ async def add_process_time_header(request: Request, call_next):
if __name__ == "__main__":
import uvicorn

uvicorn.run(app, host="127.0.0.1", port=8000)
uvicorn.run(app, host="0.0.0.0", port=8000)
47 changes: 47 additions & 0 deletions app/parse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

def parse_markdown(data: str):
"""
Parse markdown data and return a dictionary
"""
data = data.split("\n")
user_data_final = {}
for line in data:
if line.startswith("<!--"):
continue
if line.startswith("#"):
key = line.replace("#", "").strip().lower()
user_data_final["username"] = key
if line.startswith("- profile-picture:"):
user_data_final["profile_picture"] = line.replace("- profile-picture:", "").strip()
if line.startswith("|"):
key, value = line.replace("|", "").split(":")
user_data_final[key.strip()] = value.strip()
if line == "--- Bio ---":
user_data_final["bio"] = ""
for line in data[data.index("--- Bio ---") + 1:]:
if line.startswith("<!--"):
continue
if line.startswith("--- "):
break
user_data_final["bio"] += line
# check if bio is larger than 300 characters
if len(user_data_final["bio"]) > 300:
user_data_final["bio"] = user_data_final["bio"][:300] + "..."
break
if line == "--- Profile Items ---":
user_data_final["profile_items"] = []
for line in data[data.index("--- Profile Items ---") + 1:]:
if line.startswith("--- "):
break
if line == "":
continue
if line.startswith("<!--"):
continue
line_parsed = line.split("](")
desc = line_parsed[0].replace("- [", "").replace("* [", "").strip()
url = line_parsed[1].replace(")", "").strip()
user_data_final["profile_items"].append({"desc": desc, "url": url})
if line == data[-1]:
break

return user_data_final
31 changes: 30 additions & 1 deletion app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
from fastapi.responses import RedirectResponse
from fastapi.templating import Jinja2Templates

import requests, json
import requests, json, os

from app.config import Settings
from app.parse import parse_markdown

settings = Settings()
templates = Jinja2Templates(directory=settings.TEMPLATE_DIR)
Expand All @@ -21,6 +22,34 @@ def index(request: Request):

return templates.TemplateResponse("main.html", {"request": request, "page_title": "GHTree | Search"})

@router.get("/v2/{username}")
def get_user_markdown(request: Request, username: str):
# check if a file with username exists in data folder
try:
print(username)
with open(f"app/data/{username}.md", "r") as f:
data = f.read()
except FileNotFoundError:
return templates.TemplateResponse("404.html", {
"request": request,
"page_title": "GHTree | Not Found",
"status_code": 404,
"text": "NOPE",
})
parsed_data = parse_markdown(data)

return templates.TemplateResponse("v2_user.html", {
"request": request,
"profile_picture": parsed_data.get("profile_picture"),
"username": parsed_data.get("username"),
"pronouns": parsed_data.get("Pronouns"),
"occupation": parsed_data.get("Occupation"),
"location": parsed_data.get("Location"),
"bio": parsed_data.get("bio"),
"profile_items": parsed_data.get("profile_items"),
"page_title": f"GHTree | {parsed_data.get('username')}",
})

@router.get("/{username}")
def index(request: Request, username: str):
url = f"https://api.github.com/users/{username}"
Expand Down
Loading

0 comments on commit 7858d75

Please sign in to comment.