Spaces:
Running
Running
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
import json
|
3 |
+
import requests
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
HEADERS = {
|
7 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36 Edg/132.0.0.0",
|
8 |
+
}
|
9 |
+
|
10 |
+
|
11 |
+
def is_valid_url(url):
|
12 |
+
# 定义 URL 的正则表达式
|
13 |
+
pattern = re.compile(
|
14 |
+
r"^(https?://)?" # 协议(http 或 https,可选)
|
15 |
+
r"([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}" # 域名
|
16 |
+
r"(:\d+)?" # 端口号(可选)
|
17 |
+
r"(/[^ ]*)?$" # 路径(可选)
|
18 |
+
)
|
19 |
+
# 使用正则表达式匹配 URL
|
20 |
+
return bool(pattern.match(url))
|
21 |
+
|
22 |
+
|
23 |
+
def noxlink(longUrl: str):
|
24 |
+
domain = "https://noxlink.net"
|
25 |
+
api = f"{domain}/zh-CN/shorten"
|
26 |
+
payload = {"longUrl": longUrl}
|
27 |
+
try:
|
28 |
+
response = requests.post(api, json=payload, headers=HEADERS)
|
29 |
+
if response.status_code == 200:
|
30 |
+
retcode = json.loads(response.text)
|
31 |
+
if retcode["success"]:
|
32 |
+
return f"{domain}/" + retcode["message"]
|
33 |
+
|
34 |
+
return response.text
|
35 |
+
|
36 |
+
except Exception as e:
|
37 |
+
return f"{e}"
|
38 |
+
|
39 |
+
|
40 |
+
def monojson(longUrl: str):
|
41 |
+
api = "https://monojson.com/api/short-link"
|
42 |
+
payload = {"url": longUrl}
|
43 |
+
try:
|
44 |
+
response = requests.post(api, json=payload, headers=HEADERS)
|
45 |
+
if response.status_code == 200:
|
46 |
+
return json.loads(response.text)["shortUrl"]
|
47 |
+
|
48 |
+
else:
|
49 |
+
return response.text
|
50 |
+
|
51 |
+
except Exception as e:
|
52 |
+
return f"{e}"
|
53 |
+
|
54 |
+
|
55 |
+
def infer(longUrl: str, tool: str):
|
56 |
+
shortUrl = ""
|
57 |
+
if tool == "monojson":
|
58 |
+
shortUrl = monojson(longUrl)
|
59 |
+
elif tool == "noxlink":
|
60 |
+
shortUrl = noxlink(longUrl)
|
61 |
+
else:
|
62 |
+
shortUrl = "Please select an API provider!"
|
63 |
+
|
64 |
+
if is_valid_url(shortUrl):
|
65 |
+
return f'<a href="{shortUrl}" target="_blank">{shortUrl}</a>'
|
66 |
+
|
67 |
+
return shortUrl
|
68 |
+
|
69 |
+
|
70 |
+
if __name__ == "__main__":
|
71 |
+
gr.Interface(
|
72 |
+
fn=infer,
|
73 |
+
inputs=[
|
74 |
+
gr.Textbox(
|
75 |
+
label="Input a long URL",
|
76 |
+
placeholder="Input a long URL",
|
77 |
+
show_copy_button=True,
|
78 |
+
),
|
79 |
+
gr.Dropdown(
|
80 |
+
choices=["noxlink", "monojson"],
|
81 |
+
label="Select an API provider",
|
82 |
+
value="noxlink",
|
83 |
+
),
|
84 |
+
],
|
85 |
+
outputs=[
|
86 |
+
gr.HTML(
|
87 |
+
container=True,
|
88 |
+
show_label=True,
|
89 |
+
label="Output short URL",
|
90 |
+
)
|
91 |
+
],
|
92 |
+
title="Short link generator",
|
93 |
+
description="Convert long urls into short, easy-to-share links",
|
94 |
+
flagging_mode="never",
|
95 |
+
examples=[
|
96 |
+
["https://www.bing.com", "noxlink"],
|
97 |
+
["https://www.baidu.com", "monojson"],
|
98 |
+
],
|
99 |
+
).launch()
|