Agent-Logs-Url: https://github.com/wasi-master/13ft/sessions/3b6e6061-c4d2-4aa1-879b-8526ccd3b1db Co-authored-by: wasi-master <63045920+wasi-master@users.noreply.github.com>
302 lines
8.3 KiB
Python
302 lines
8.3 KiB
Python
import json
|
|
import os
|
|
import re
|
|
|
|
import flask
|
|
import requests
|
|
from flask import request
|
|
from bs4 import BeautifulSoup
|
|
from urllib.parse import urlparse, urljoin
|
|
|
|
app = flask.Flask(__name__)
|
|
|
|
_DEFAULT_STRINGS = {
|
|
"heading": "Enter Website Link",
|
|
"label": "Link of the website you want to remove paywall for:",
|
|
"submit": "Submit",
|
|
"toggle_dark_mode": "Toggle Dark Mode",
|
|
"invalid_url": "Invalid URL",
|
|
}
|
|
|
|
|
|
def load_strings():
|
|
"""Load UI strings from a locale JSON file selected by the LOCALE env var.
|
|
|
|
Falls back to built-in English strings when the locale file is missing or
|
|
cannot be parsed. Only alphanumeric characters, hyphens, and underscores
|
|
are accepted as locale names to prevent path traversal.
|
|
"""
|
|
locale = os.environ.get("LOCALE", "en")
|
|
if not re.match(r'^[a-zA-Z0-9_-]+$', locale):
|
|
locale = "en"
|
|
|
|
if locale == "en":
|
|
return dict(_DEFAULT_STRINGS)
|
|
|
|
locale_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "locales")
|
|
locale_file = os.path.join(locale_dir, f"{locale}.json")
|
|
|
|
try:
|
|
with open(locale_file, "r", encoding="utf-8") as f:
|
|
loaded = json.load(f)
|
|
return {**_DEFAULT_STRINGS, **loaded}
|
|
except (FileNotFoundError, json.JSONDecodeError, OSError):
|
|
return dict(_DEFAULT_STRINGS)
|
|
|
|
|
|
strings = load_strings()
|
|
|
|
googlebot_headers = {
|
|
"User-Agent": "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.6533.119 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
|
|
}
|
|
html = """
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
<title>13ft Ladder</title>
|
|
<link href="https://fonts.googleapis.com/css2?family=Open+Sans&display=swap" rel="stylesheet" async>
|
|
<style>
|
|
body {
|
|
font-family: 'Open Sans', sans-serif;
|
|
background-color: #FFF;
|
|
margin: 0;
|
|
padding: 0;
|
|
display: flex;
|
|
justify-content: center;
|
|
align-items: center;
|
|
height: 90vh;
|
|
transition: background-color 0.3s, color 0.3s;
|
|
}
|
|
|
|
h1 {
|
|
font-size: 1.5rem;
|
|
margin-bottom: 20px;
|
|
text-align: center;
|
|
color: #333;
|
|
}
|
|
|
|
label {
|
|
display: block;
|
|
margin-bottom: 10px;
|
|
font-weight: bold;
|
|
}
|
|
|
|
input[type=text] {
|
|
padding: 10px;
|
|
margin-bottom: 10px;
|
|
border: 1px solid #ccc;
|
|
border-radius: 5px;
|
|
width: 100%;
|
|
font-size: 1rem;
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
input[type="submit"] {
|
|
padding: 10px;
|
|
background-color: #6a0dad;
|
|
color: #fff;
|
|
border: none;
|
|
border-radius: 5px;
|
|
width: 100%;
|
|
text-transform: uppercase;
|
|
font-weight: 600;
|
|
cursor: pointer;
|
|
transition: background-color 0.3s ease;
|
|
}
|
|
|
|
input[type="submit"]:hover {
|
|
background-color: #4e0875;
|
|
}
|
|
|
|
/* Toggle switch styles */
|
|
.dark-mode-toggle {
|
|
position: absolute;
|
|
top: 10px;
|
|
right: 10px;
|
|
}
|
|
|
|
.dark-mode-toggle input {
|
|
display: none;
|
|
}
|
|
|
|
.dark-mode-toggle label {
|
|
cursor: pointer;
|
|
text-indent: -9999px;
|
|
width: 52px;
|
|
height: 27px;
|
|
background: grey;
|
|
display: block;
|
|
border-radius: 100px;
|
|
position: relative;
|
|
}
|
|
|
|
.dark-mode-toggle label:after {
|
|
content: '';
|
|
position: absolute;
|
|
top: 2px;
|
|
left: 2px;
|
|
width: 23px;
|
|
height: 23px;
|
|
background: #fff;
|
|
border-radius: 90px;
|
|
transition: 0.3s;
|
|
}
|
|
|
|
.dark-mode-toggle input:checked+label {
|
|
background: #6a0dad;
|
|
}
|
|
|
|
.dark-mode-toggle input:checked+label:after {
|
|
left: calc(100% - 2px);
|
|
transform: translateX(-100%);
|
|
}
|
|
|
|
/* Responsive adjustments */
|
|
@media only screen and (max-width: 600px) {
|
|
form {
|
|
padding: 10px;
|
|
}
|
|
|
|
h1 {
|
|
font-size: 1.2rem;
|
|
}
|
|
}
|
|
|
|
/* Dark mode styles */
|
|
body.dark-mode {
|
|
background-color: #333;
|
|
color: #FFF;
|
|
}
|
|
|
|
body.dark-mode h1 {
|
|
color: #FFF;
|
|
}
|
|
|
|
body.dark-mode input[type=text] {
|
|
background-color: #555;
|
|
border: 1px solid #777;
|
|
color: #FFF;
|
|
}
|
|
|
|
body.dark-mode input[type="submit"] {
|
|
background-color: #9b30ff;
|
|
}
|
|
|
|
body.dark-mode input[type="submit"]:hover {
|
|
background-color: #7a1bb5;
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body>
|
|
<div class="dark-mode-toggle">
|
|
<input type="checkbox" id="dark-mode-toggle">
|
|
<label for="dark-mode-toggle" title="{{ toggle_dark_mode }}"></label>
|
|
</div>
|
|
<form action="/article" method="post">
|
|
<h1>{{ heading }}</h1>
|
|
<label for="link">{{ label }}</label>
|
|
<input type="text" id="link" name="link" required autofocus>
|
|
<input type="submit" value="{{ submit }}">
|
|
</form>
|
|
|
|
<script>
|
|
const toggleSwitch = document.getElementById('dark-mode-toggle');
|
|
const currentTheme = localStorage.getItem('theme') || (window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light");
|
|
|
|
if (currentTheme === "dark") {
|
|
document.body.classList.add("dark-mode");
|
|
toggleSwitch.checked = true;
|
|
}
|
|
|
|
toggleSwitch.addEventListener('change', function () {
|
|
if (this.checked) {
|
|
document.body.classList.add("dark-mode");
|
|
localStorage.setItem('theme', 'dark');
|
|
} else {
|
|
document.body.classList.remove("dark-mode");
|
|
localStorage.setItem('theme', 'light');
|
|
}
|
|
});
|
|
</script>
|
|
</body>
|
|
|
|
</html>
|
|
"""
|
|
|
|
def add_base_tag(html_content, original_url):
|
|
soup = BeautifulSoup(html_content, 'html.parser')
|
|
parsed_url = urlparse(original_url)
|
|
base_url = f"{parsed_url.scheme}://{parsed_url.netloc}/"
|
|
|
|
# Handle paths that are not root, e.g., "https://x.com/some/path/w.html"
|
|
if parsed_url.path and not parsed_url.path.endswith('/'):
|
|
base_url = urljoin(base_url, parsed_url.path.rsplit('/', 1)[0] + '/')
|
|
base_tag = soup.find('base')
|
|
|
|
print(base_url)
|
|
if not base_tag:
|
|
new_base_tag = soup.new_tag('base', href=base_url)
|
|
if soup.head:
|
|
soup.head.insert(0, new_base_tag)
|
|
else:
|
|
head_tag = soup.new_tag('head')
|
|
head_tag.insert(0, new_base_tag)
|
|
soup.insert(0, head_tag)
|
|
|
|
return str(soup)
|
|
|
|
def bypass_paywall(url):
|
|
"""
|
|
Bypass paywall for a given url
|
|
"""
|
|
if url.startswith("http"):
|
|
response = requests.get(url, headers=googlebot_headers)
|
|
response.encoding = response.apparent_encoding
|
|
return add_base_tag(response.text, response.url)
|
|
|
|
try:
|
|
return bypass_paywall("https://" + url)
|
|
except requests.exceptions.RequestException as e:
|
|
return bypass_paywall("http://" + url)
|
|
|
|
|
|
@app.route("/")
|
|
def main_page():
|
|
return flask.render_template_string(html, **strings)
|
|
|
|
|
|
@app.route("/article", methods=["POST"])
|
|
def show_article():
|
|
link = flask.request.form["link"]
|
|
try:
|
|
return bypass_paywall(link)
|
|
except requests.exceptions.RequestException as e:
|
|
return str(e), 400
|
|
except Exception as e:
|
|
raise e
|
|
|
|
|
|
@app.route("/", defaults={"path": ""})
|
|
@app.route("/<path:path>", methods=["GET"])
|
|
def get_article(path):
|
|
full_url = request.url
|
|
parts = full_url.split("/", 4)
|
|
if len(parts) >= 5:
|
|
actual_url = "https://" + parts[4].lstrip("/")
|
|
try:
|
|
return bypass_paywall(actual_url)
|
|
except requests.exceptions.RequestException as e:
|
|
return str(e), 400
|
|
except Exception as e:
|
|
raise e
|
|
else:
|
|
return strings["invalid_url"], 400
|
|
|
|
|
|
app.run(host="0.0.0.0", port=5000, debug=False)
|