added rewrite guid link
This commit is contained in:
@@ -3,9 +3,9 @@ import os
|
|||||||
import requests
|
import requests
|
||||||
import redis
|
import redis
|
||||||
import xml.etree.ElementTree as ET
|
import xml.etree.ElementTree as ET
|
||||||
|
import re
|
||||||
from flask import request, Response
|
from flask import request, Response
|
||||||
|
|
||||||
|
|
||||||
PROXY_URL = os.getenv("PROXY_URL")
|
PROXY_URL = os.getenv("PROXY_URL")
|
||||||
REDIS_URL = os.getenv("REDIS_URL", "redis://localhost:6379/0")
|
REDIS_URL = os.getenv("REDIS_URL", "redis://localhost:6379/0")
|
||||||
CACHE_TTL = int(os.getenv("CACHE_TTL", 3600))
|
CACHE_TTL = int(os.getenv("CACHE_TTL", 3600))
|
||||||
@@ -13,10 +13,16 @@ CACHE_TTL = int(os.getenv("CACHE_TTL", 3600))
|
|||||||
rdb = redis.from_url(REDIS_URL)
|
rdb = redis.from_url(REDIS_URL)
|
||||||
|
|
||||||
|
|
||||||
|
def extract_viewtopic_link(description):
|
||||||
|
"""Search viewtopic.php in description"""
|
||||||
|
match = re.search(r'href="(http://tapochek\.net/viewtopic\.php\?t=\d+)"', description)
|
||||||
|
return match.group(1) if match else None
|
||||||
|
|
||||||
|
|
||||||
def init_proxy(app):
|
def init_proxy(app):
|
||||||
@app.route("/proxy")
|
@app.route("/proxy")
|
||||||
def proxy():
|
def proxy():
|
||||||
"""Proxy RSS feed with per-item caching."""
|
"""Proxy RSS feed with per-item caching and GUID replacement."""
|
||||||
raw_query = request.query_string.decode()
|
raw_query = request.query_string.decode()
|
||||||
if raw_query.startswith("url="):
|
if raw_query.startswith("url="):
|
||||||
url = urllib.parse.unquote(raw_query[4:])
|
url = urllib.parse.unquote(raw_query[4:])
|
||||||
@@ -24,7 +30,6 @@ def init_proxy(app):
|
|||||||
return "Missing URL", 400
|
return "Missing URL", 400
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Получаем ленту
|
|
||||||
proxies = {"http": PROXY_URL, "https": PROXY_URL} if PROXY_URL else None
|
proxies = {"http": PROXY_URL, "https": PROXY_URL} if PROXY_URL else None
|
||||||
r = requests.get(url, timeout=10, proxies=proxies)
|
r = requests.get(url, timeout=10, proxies=proxies)
|
||||||
_encode = r.apparent_encoding.lower()
|
_encode = r.apparent_encoding.lower()
|
||||||
@@ -32,7 +37,6 @@ def init_proxy(app):
|
|||||||
xml_data = r.text.replace(f'<?xml version="1.0" encoding="{_encode}"?>',
|
xml_data = r.text.replace(f'<?xml version="1.0" encoding="{_encode}"?>',
|
||||||
'<?xml version="1.0" encoding="UTF-8"?>')
|
'<?xml version="1.0" encoding="UTF-8"?>')
|
||||||
|
|
||||||
# Разбираем XML
|
|
||||||
root = ET.fromstring(xml_data)
|
root = ET.fromstring(xml_data)
|
||||||
items = root.findall(".//item")
|
items = root.findall(".//item")
|
||||||
|
|
||||||
@@ -40,19 +44,26 @@ def init_proxy(app):
|
|||||||
new_items = []
|
new_items = []
|
||||||
|
|
||||||
for item in items:
|
for item in items:
|
||||||
guid = item.find("guid").text if item.find("guid") is not None else None
|
guid = item.find("guid")
|
||||||
if guid:
|
if guid is None or guid.get("isPermaLink") == "true":
|
||||||
cache_key = f"rss:item:{guid}"
|
continue
|
||||||
|
|
||||||
|
cache_key = f"rss:item:{guid.text}"
|
||||||
cached_item = rdb.get(cache_key)
|
cached_item = rdb.get(cache_key)
|
||||||
|
|
||||||
if cached_item:
|
if cached_item:
|
||||||
cached_items.append(cached_item.decode())
|
cached_items.append(cached_item.decode())
|
||||||
else:
|
else:
|
||||||
|
description = item.find("description")
|
||||||
|
if description is not None:
|
||||||
|
new_guid = extract_viewtopic_link(description.text)
|
||||||
|
if new_guid:
|
||||||
|
guid.text = new_guid
|
||||||
|
|
||||||
item_str = ET.tostring(item, encoding="unicode")
|
item_str = ET.tostring(item, encoding="unicode")
|
||||||
rdb.setex(cache_key, CACHE_TTL, item_str)
|
rdb.setex(cache_key, CACHE_TTL, item_str)
|
||||||
new_items.append(item_str)
|
new_items.append(item_str)
|
||||||
|
|
||||||
# Собираем финальный RSS
|
|
||||||
final_items = cached_items + new_items
|
final_items = cached_items + new_items
|
||||||
response_xml = f'<?xml version="1.0" encoding="UTF-8"?><rss><channel>{"".join(final_items)}</channel></rss>'
|
response_xml = f'<?xml version="1.0" encoding="UTF-8"?><rss><channel>{"".join(final_items)}</channel></rss>'
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user