Spaces:
Running
Running
Monteeq CI commited on
Commit Β·
0f591cf
1
Parent(s): 02c1fe3
π Deploy from GitHub @ 7a8676e63a10d8145c0cf259b4530e89cdb31445
Browse filesTriggered by: Smasduq
Message: Merge pull request #18 from Monteeq/feat/async-upload-jobs-gunicorn
fix: sitemap crash + Google Video Indexing issues
- app/api/v1/endpoints/seo.py +68 -32
app/api/v1/endpoints/seo.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
from fastapi import APIRouter, Depends, Response
|
| 2 |
from sqlalchemy.orm import Session
|
|
|
|
| 3 |
from app.db.session import get_db
|
| 4 |
-
from app.
|
| 5 |
-
from app.core import config
|
| 6 |
import xml.etree.ElementTree as ET
|
| 7 |
from datetime import datetime
|
| 8 |
|
|
@@ -10,11 +10,32 @@ router = APIRouter()
|
|
| 10 |
|
| 11 |
BASE_URL = config.FRONTEND_URL.rstrip('/')
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
@router.get("/sitemap.xml")
|
| 14 |
async def get_sitemap(db: Session = Depends(get_db)):
|
| 15 |
-
"""Generates a standard XML sitemap for general pages."""
|
| 16 |
urlset = ET.Element("urlset", xmlns="http://www.sitemaps.org/schemas/sitemap/0.9")
|
| 17 |
-
|
| 18 |
# Static Public Pages
|
| 19 |
pages = [
|
| 20 |
{"path": "", "priority": "1.0"},
|
|
@@ -30,52 +51,67 @@ async def get_sitemap(db: Session = Depends(get_db)):
|
|
| 30 |
url_el = ET.SubElement(urlset, "url")
|
| 31 |
ET.SubElement(url_el, "loc").text = f"{BASE_URL}{page['path']}"
|
| 32 |
ET.SubElement(url_el, "changefreq").text = "daily"
|
| 33 |
-
ET.SubElement(url_el, "priority").text = page[
|
| 34 |
|
| 35 |
-
# Dynamic Video Pages
|
| 36 |
-
|
| 37 |
-
for
|
|
|
|
|
|
|
| 38 |
url_el = ET.SubElement(urlset, "url")
|
| 39 |
-
ET.SubElement(url_el, "loc").text = f"{BASE_URL}/watch/{
|
| 40 |
-
ET.SubElement(url_el, "lastmod").text =
|
| 41 |
ET.SubElement(url_el, "changefreq").text = "weekly"
|
| 42 |
ET.SubElement(url_el, "priority").text = "0.6"
|
| 43 |
|
| 44 |
xml_data = b'<?xml version="1.0" encoding="UTF-8"?>\n' + ET.tostring(urlset, encoding="utf-8", method="xml")
|
| 45 |
return Response(content=xml_data, media_type="application/xml")
|
| 46 |
|
|
|
|
| 47 |
@router.get("/video-sitemap.xml")
|
| 48 |
async def get_video_sitemap(db: Session = Depends(get_db)):
|
| 49 |
-
"""Generates a
|
| 50 |
urlset = ET.Element("urlset", {
|
| 51 |
"xmlns": "http://www.sitemaps.org/schemas/sitemap/0.9",
|
| 52 |
-
"xmlns:video": "http://www.google.com/schemas/sitemap-video/1.1"
|
| 53 |
})
|
| 54 |
-
|
| 55 |
-
videos = db.query(Video).filter(Video.status == "approved").all()
|
| 56 |
video_ns = "{http://www.google.com/schemas/sitemap-video/1.1}"
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
continue
|
| 62 |
-
|
| 63 |
url_el = ET.SubElement(urlset, "url")
|
| 64 |
-
ET.SubElement(url_el, "loc").text = f"{BASE_URL}/watch/{
|
| 65 |
-
|
| 66 |
video_el = ET.SubElement(url_el, f"{video_ns}video")
|
| 67 |
-
ET.SubElement(video_el, f"{video_ns}thumbnail_loc").text =
|
| 68 |
-
ET.SubElement(video_el, f"{video_ns}title").text =
|
| 69 |
-
ET.SubElement(video_el, f"{video_ns}description").text =
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
ET.SubElement(video_el, f"{video_ns}publication_date").text = pub_date
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
| 79 |
|
| 80 |
xml_data = b'<?xml version="1.0" encoding="UTF-8"?>\n' + ET.tostring(urlset, encoding="utf-8", method="xml")
|
| 81 |
return Response(content=xml_data, media_type="application/xml")
|
|
|
|
| 1 |
from fastapi import APIRouter, Depends, Response
|
| 2 |
from sqlalchemy.orm import Session
|
| 3 |
+
from sqlalchemy import text
|
| 4 |
from app.db.session import get_db
|
| 5 |
+
from app.core import config, storage
|
|
|
|
| 6 |
import xml.etree.ElementTree as ET
|
| 7 |
from datetime import datetime
|
| 8 |
|
|
|
|
| 10 |
|
| 11 |
BASE_URL = config.FRONTEND_URL.rstrip('/')
|
| 12 |
|
| 13 |
+
|
| 14 |
+
def _resolve_url(url):
|
| 15 |
+
"""Inline CDN resolver β avoids loading the full Video ORM (which may have columns not yet migrated)."""
|
| 16 |
+
if not url:
|
| 17 |
+
return url
|
| 18 |
+
if "amazonaws.com" in url or "monteeq.s3" in url or "cdn.monteeq.com" in url:
|
| 19 |
+
if ".com/" in url:
|
| 20 |
+
parts = url.split(".com/")
|
| 21 |
+
if len(parts) > 1:
|
| 22 |
+
return storage.get_url(parts[1])
|
| 23 |
+
return url
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
_VIDEOS_SQL = text("""
|
| 27 |
+
SELECT id, title, description, video_url, thumbnail_url, tags,
|
| 28 |
+
duration, created_at, owner_id, status
|
| 29 |
+
FROM videos
|
| 30 |
+
WHERE status = :status
|
| 31 |
+
""")
|
| 32 |
+
|
| 33 |
+
|
| 34 |
@router.get("/sitemap.xml")
|
| 35 |
async def get_sitemap(db: Session = Depends(get_db)):
|
| 36 |
+
"""Generates a standard XML sitemap for general pages + video watch pages."""
|
| 37 |
urlset = ET.Element("urlset", xmlns="http://www.sitemaps.org/schemas/sitemap/0.9")
|
| 38 |
+
|
| 39 |
# Static Public Pages
|
| 40 |
pages = [
|
| 41 |
{"path": "", "priority": "1.0"},
|
|
|
|
| 51 |
url_el = ET.SubElement(urlset, "url")
|
| 52 |
ET.SubElement(url_el, "loc").text = f"{BASE_URL}{page['path']}"
|
| 53 |
ET.SubElement(url_el, "changefreq").text = "daily"
|
| 54 |
+
ET.SubElement(url_el, "priority").text = page["priority"]
|
| 55 |
|
| 56 |
+
# Dynamic Video Pages β raw SQL to avoid ORM column-mismatch crashes
|
| 57 |
+
rows = db.execute(_VIDEOS_SQL, {"status": "approved"}).fetchall()
|
| 58 |
+
for row in rows:
|
| 59 |
+
if not row.created_at:
|
| 60 |
+
continue
|
| 61 |
url_el = ET.SubElement(urlset, "url")
|
| 62 |
+
ET.SubElement(url_el, "loc").text = f"{BASE_URL}/watch/{row.id}"
|
| 63 |
+
ET.SubElement(url_el, "lastmod").text = row.created_at.strftime("%Y-%m-%d")
|
| 64 |
ET.SubElement(url_el, "changefreq").text = "weekly"
|
| 65 |
ET.SubElement(url_el, "priority").text = "0.6"
|
| 66 |
|
| 67 |
xml_data = b'<?xml version="1.0" encoding="UTF-8"?>\n' + ET.tostring(urlset, encoding="utf-8", method="xml")
|
| 68 |
return Response(content=xml_data, media_type="application/xml")
|
| 69 |
|
| 70 |
+
|
| 71 |
@router.get("/video-sitemap.xml")
|
| 72 |
async def get_video_sitemap(db: Session = Depends(get_db)):
|
| 73 |
+
"""Generates a Google Video Sitemap for video indexing."""
|
| 74 |
urlset = ET.Element("urlset", {
|
| 75 |
"xmlns": "http://www.sitemaps.org/schemas/sitemap/0.9",
|
| 76 |
+
"xmlns:video": "http://www.google.com/schemas/sitemap-video/1.1",
|
| 77 |
})
|
|
|
|
|
|
|
| 78 |
video_ns = "{http://www.google.com/schemas/sitemap-video/1.1}"
|
| 79 |
+
|
| 80 |
+
rows = db.execute(_VIDEOS_SQL, {"status": "approved"}).fetchall()
|
| 81 |
+
|
| 82 |
+
for row in rows:
|
| 83 |
+
if not row.created_at or not row.video_url:
|
| 84 |
+
continue
|
| 85 |
+
|
| 86 |
+
thumbnail = _resolve_url(row.thumbnail_url) if row.thumbnail_url else ""
|
| 87 |
+
if not thumbnail:
|
| 88 |
continue
|
| 89 |
+
|
| 90 |
url_el = ET.SubElement(urlset, "url")
|
| 91 |
+
ET.SubElement(url_el, "loc").text = f"{BASE_URL}/watch/{row.id}"
|
| 92 |
+
|
| 93 |
video_el = ET.SubElement(url_el, f"{video_ns}video")
|
| 94 |
+
ET.SubElement(video_el, f"{video_ns}thumbnail_loc").text = thumbnail
|
| 95 |
+
ET.SubElement(video_el, f"{video_ns}title").text = row.title or ""
|
| 96 |
+
ET.SubElement(video_el, f"{video_ns}description").text = (
|
| 97 |
+
row.description or f"Watch {row.title} on Monteeq."
|
| 98 |
+
)
|
| 99 |
+
# player_loc: Google crawls this page which embeds the HLS player
|
| 100 |
+
ET.SubElement(video_el, f"{video_ns}player_loc").text = f"{BASE_URL}/watch/{row.id}"
|
| 101 |
+
|
| 102 |
+
# Duration β Google requires HH:MM:SS
|
| 103 |
+
dur = int(row.duration or 0)
|
| 104 |
+
hours, remainder = divmod(dur, 3600)
|
| 105 |
+
minutes, seconds = divmod(remainder, 60)
|
| 106 |
+
ET.SubElement(video_el, f"{video_ns}duration").text = f"{hours:02d}:{minutes:02d}:{seconds:02d}"
|
| 107 |
+
|
| 108 |
+
pub_date = row.created_at.strftime("%Y-%m-%d")
|
| 109 |
ET.SubElement(video_el, f"{video_ns}publication_date").text = pub_date
|
| 110 |
+
|
| 111 |
+
# Tags (comma-separated in DB)
|
| 112 |
+
if row.tags:
|
| 113 |
+
for tag in [t.strip() for t in row.tags.split(",") if t.strip()][:32]:
|
| 114 |
+
ET.SubElement(video_el, f"{video_ns}tag").text = tag
|
| 115 |
|
| 116 |
xml_data = b'<?xml version="1.0" encoding="UTF-8"?>\n' + ET.tostring(urlset, encoding="utf-8", method="xml")
|
| 117 |
return Response(content=xml_data, media_type="application/xml")
|