Refactor app.py to use Jinja2 templates for rendering index.html and pass social media data as JSON
Browse files- app.py +18 -11
- templates/index.html +1 -22
- utils.py +5 -0
app.py
CHANGED
@@ -1,19 +1,26 @@
|
|
1 |
-
|
2 |
-
from fastapi
|
3 |
-
from fastapi.
|
4 |
-
from
|
5 |
|
6 |
app = FastAPI()
|
7 |
|
8 |
-
|
9 |
-
app.mount("/static", StaticFiles(directory="static"), name="static")
|
10 |
|
11 |
socials = get_socials()
|
12 |
|
13 |
@app.get('/')
|
14 |
-
|
15 |
try:
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
except Exception as e:
|
18 |
return str(e)
|
19 |
|
@@ -24,11 +31,11 @@ async def check_social_media_handle(platform: str, username: str):
|
|
24 |
return {
|
25 |
"message": f'❌ The platform "{platform}" is not supported'
|
26 |
}
|
27 |
-
return await _resolve(username, **social)
|
28 |
|
29 |
-
async def _resolve(platform: str, username: str, *, validate, resolve, message = None):
|
30 |
if not validate(username):
|
31 |
-
raise Exception(f'"{username}" is not a valid {platform}
|
32 |
logs, logger = get_logger()
|
33 |
response = await availability_response(
|
34 |
resolve = resolve(username, logger),
|
|
|
1 |
+
import json
|
2 |
+
from fastapi import FastAPI, Request
|
3 |
+
from fastapi.templating import Jinja2Templates
|
4 |
+
from utils import get_socials, get_logger, availability_response
|
5 |
|
6 |
app = FastAPI()
|
7 |
|
8 |
+
templates = Jinja2Templates(directory="templates")
|
|
|
9 |
|
10 |
socials = get_socials()
|
11 |
|
12 |
@app.get('/')
|
13 |
+
def index(request: Request):
|
14 |
try:
|
15 |
+
json_data = json.dumps([
|
16 |
+
{ "id": i.get('id'), "name": i.get('name'), "img": i.get('img'), }
|
17 |
+
for i
|
18 |
+
in socials
|
19 |
+
])
|
20 |
+
return templates.TemplateResponse("index.html", {
|
21 |
+
"request": request,
|
22 |
+
"json_data": json_data
|
23 |
+
})
|
24 |
except Exception as e:
|
25 |
return str(e)
|
26 |
|
|
|
31 |
return {
|
32 |
"message": f'❌ The platform "{platform}" is not supported'
|
33 |
}
|
34 |
+
return await _resolve(platform, username, **social)
|
35 |
|
36 |
+
async def _resolve(platform: str, username: str, *, validate, resolve, message = None, **_):
|
37 |
if not validate(username):
|
38 |
+
raise Exception(f'"{username}" is not a valid {platform} username')
|
39 |
logs, logger = get_logger()
|
40 |
response = await availability_response(
|
41 |
resolve = resolve(username, logger),
|
templates/index.html
CHANGED
@@ -64,28 +64,7 @@
|
|
64 |
|
65 |
<script>
|
66 |
const platformContainer = document.querySelector("#platform-container")
|
67 |
-
const platforms =
|
68 |
-
{
|
69 |
-
id: "instagram",
|
70 |
-
name: "Instagram",
|
71 |
-
img: "https://cdn.jsdelivr.net/npm/simple-icons@v6/icons/instagram.svg"
|
72 |
-
},
|
73 |
-
{
|
74 |
-
id: "x",
|
75 |
-
name: "X (formerly Twitter)",
|
76 |
-
img: "https://cdn.jsdelivr.net/npm/simple-icons@v6/icons/twitter.svg"
|
77 |
-
},
|
78 |
-
{
|
79 |
-
id: "linkedin-user",
|
80 |
-
name: "LinkedIn User",
|
81 |
-
img: "https://cdn.jsdelivr.net/npm/simple-icons@v6/icons/linkedin.svg"
|
82 |
-
},
|
83 |
-
{
|
84 |
-
id: "linkedin-page",
|
85 |
-
name: "LinkedIn Company",
|
86 |
-
img: "https://cdn.jsdelivr.net/npm/simple-icons@v6/icons/linkedin.svg"
|
87 |
-
}
|
88 |
-
];
|
89 |
|
90 |
for (const platform of platforms) {
|
91 |
platformContainer.innerHTML +=
|
|
|
64 |
|
65 |
<script>
|
66 |
const platformContainer = document.querySelector("#platform-container")
|
67 |
+
const platforms = {{ json_data | safe }};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
for (const platform of platforms) {
|
70 |
platformContainer.innerHTML +=
|
utils.py
CHANGED
@@ -105,6 +105,11 @@ def resolve_instagram_username(
|
|
105 |
def resolve() -> bool:
|
106 |
profile_uri = f"https://www.instagram.com/{username}/"
|
107 |
profile_response = requests.get(profile_uri, allow_redirects = False)
|
|
|
|
|
|
|
|
|
|
|
108 |
profile_response_username = get_json_value(profile_response.text, "username", "\w+") or ""
|
109 |
logger("profile_response_username", profile_response_username)
|
110 |
_return_result = lambda is_available: (username, is_available, profile_uri)
|
|
|
105 |
def resolve() -> bool:
|
106 |
profile_uri = f"https://www.instagram.com/{username}/"
|
107 |
profile_response = requests.get(profile_uri, allow_redirects = False)
|
108 |
+
logger(
|
109 |
+
"profile_response_status",
|
110 |
+
f"{profile_response.status_code} {profile_response.headers.get('Location')}" \
|
111 |
+
if profile_response.status_code in [301, 302] \
|
112 |
+
else profile_response.status_code)
|
113 |
profile_response_username = get_json_value(profile_response.text, "username", "\w+") or ""
|
114 |
logger("profile_response_username", profile_response_username)
|
115 |
_return_result = lambda is_available: (username, is_available, profile_uri)
|