-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
110 lines (83 loc) · 3.75 KB
/
main.py
File metadata and controls
110 lines (83 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import logging
import aiohttp
from bs4 import BeautifulSoup, SoupStrainer
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse, RedirectResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
app = FastAPI(
title="Fx dcinside",
summary="Fix dcinside link preview on Discord.",
docs_url=None,
redoc_url=None,
)
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")
only_meta_tags = SoupStrainer("meta")
async def fetch_og_tags(url: str) -> dict[str, str]:
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36"
}
buf = b""
try:
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=headers) as resp:
resp.raise_for_status()
async for chunk in resp.content.iter_chunked(1024 * 16):
buf += chunk
if b"</head>" in buf.lower():
break
soup = BeautifulSoup(buf.decode(errors="ignore"), "html.parser", parse_only=only_meta_tags)
return {
"title": soup.find("meta", property="og:title")["content"],
"description": soup.find("meta", property="og:description")["content"],
#"image": soup.find("meta", property="og:image")["content"],
"image": "...",
}
except Exception as e:
logging.exception(e)
return {
"title": "We are with you all the way! IT is Life! 디시인사이드 입니다.",
"description": "접속불가",
"image": "/static/descrip_img.png",
}
async def template_or_redirect(
request: Request,
id: str,
no: int,
base_url: str = "https://gall.dcinside.com",
infix: str = "",
):
url = f"{base_url}{infix}/{id}/{no}"
ua = (request.headers.get("user-agent") or "").lower()
if "discordbot" in ua:
og = await fetch_og_tags(url)
return templates.TemplateResponse(
"index.html", {"request": request, "og": og, "path": request['path']}
)
else:
return RedirectResponse(url)
@app.get("/", include_in_schema=False)
async def root():
return RedirectResponse("https://github.com/KOZ39/fxdcinside.com")
@app.get("/board/view/", response_class=HTMLResponse)
async def gallery(id: str, no: int, request: Request):
return await template_or_redirect(request, id, no)
@app.get("/m/{id}/{no}", response_class=HTMLResponse)
@app.get("/mgallery/board/view/", response_class=HTMLResponse)
async def minor_gallery(id: str, no: int, request: Request):
return await template_or_redirect(request, id, no, infix="/m")
@app.get("/mini/{id}/{no}", response_class=HTMLResponse)
@app.get("/mini/board/view/", response_class=HTMLResponse)
async def mini_gallery(id: str, no: int, request: Request):
return await template_or_redirect(request, id, no, infix="/mini")
@app.get("/person/{id}/{no}", response_class=HTMLResponse)
@app.get("/person/board/view/", response_class=HTMLResponse)
async def person_gallery(id: str, no: int, request: Request):
return await template_or_redirect(request, id, no, infix="/person")
# HACK: 마이너 갤러리 단축 주소의 301 리다이렉션 판단 불가
# Ref: https://github.com/KOZ39/fxdcinside.com/issues/1
@app.get("/board/{id}/{no}", response_class=HTMLResponse) # Mobile
@app.get("/{id}/{no}", response_class=HTMLResponse) # Short
async def short_or_mobile(id: str, no: int, request: Request):
return await template_or_redirect(request, id, no, base_url="https://m.dcinside.com", infix="/board")