admin commited on
Commit
96c94a7
·
1 Parent(s): 8599419
Files changed (7) hide show
  1. README.md +3 -3
  2. app.py +53 -22
  3. modules/qr.py +0 -47
  4. modules/smtp.py +0 -66
  5. modules/trans.py +0 -69
  6. modules/url.py +0 -58
  7. utils.py +1 -44
README.md CHANGED
@@ -1,6 +1,6 @@
1
  ---
2
- title: Online Tools II
3
- emoji: 🛠️
4
  colorFrom: blue
5
  colorTo: yellow
6
  sdk: gradio
@@ -8,5 +8,5 @@ sdk_version: 5.22.0
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
11
- short_description: Online tool collection 2
12
  ---
 
1
  ---
2
+ title: SMTP Tester
3
+ emoji: 📧
4
  colorFrom: blue
5
  colorTo: yellow
6
  sdk: gradio
 
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
11
+ short_description: Test SMTP
12
  ---
app.py CHANGED
@@ -1,16 +1,20 @@
 
1
  import gradio as gr
2
- from modules.qr import qrcode
3
- from modules.smtp import smtp_tester
4
- from modules.trans import translator
5
- from modules.url import url_shortner
6
- from utils import EN_US
7
 
8
  ZH2EN = {
9
- "# 在线工具合集2": "# Online Tools Collection II",
10
- "二维码生成": "QR Code",
11
- "SMTP 测试": "SMTP Test",
12
- "翻译器": "Translator",
13
- "短链接生成": "URL Shortner",
 
 
 
 
 
 
 
14
  }
15
 
16
 
@@ -18,19 +22,46 @@ def _L(zh_txt: str):
18
  return ZH2EN[zh_txt] if EN_US else zh_txt
19
 
20
 
21
- if __name__ == "__main__":
22
- with gr.Blocks() as demo:
23
- gr.Markdown(_L("# 在线工具合集2"))
24
- with gr.Tab(_L("SMTP 测试")):
25
- smtp_tester()
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
- with gr.Tab(_L("二维码生成")):
28
- qrcode()
29
 
30
- with gr.Tab(_L("翻译器")):
31
- translator()
32
 
33
- with gr.Tab(_L("短链接生成")):
34
- url_shortner()
35
 
36
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
  import gradio as gr
3
+ from utils import API_SMTP, EN_US
 
 
 
 
4
 
5
  ZH2EN = {
6
+ "收信人邮箱": "To email",
7
+ "标题": "Title",
8
+ "测试标题": "Test title",
9
+ "正文": "Content",
10
+ "SMTP 在线测试工具": "SMTP online tester",
11
+ "发信人昵称": "Sender name",
12
+ "测试昵称": "Test nickname",
13
+ "发信人邮箱": "From email",
14
+ "应用密钥": "API key",
15
+ "SMTP 服务器": "SMTP host",
16
+ "端口": "Port",
17
+ "发送状态": "Status",
18
  }
19
 
20
 
 
22
  return ZH2EN[zh_txt] if EN_US else zh_txt
23
 
24
 
25
+ def infer(target, title, content, name, email, password, host, port):
26
+ try:
27
+ response = requests.get(
28
+ API_SMTP,
29
+ params={
30
+ "host": host,
31
+ "Port": port,
32
+ "key": password, # apikey
33
+ "email": email, # from
34
+ "mail": target, # to
35
+ "title": title, # subject
36
+ "name": name, # nickname
37
+ "text": content, # content
38
+ },
39
+ )
40
+ if response.status_code == 200:
41
+ result: dict = response.json()
42
+ return result.get("status")
43
 
44
+ else:
45
+ raise ConnectionError(f"{response.status_code}")
46
 
47
+ except Exception as e:
48
+ return f"{e}"
49
 
 
 
50
 
51
+ if __name__ == "__main__":
52
+ gr.Interface(
53
+ fn=infer,
54
+ inputs=[
55
+ gr.Textbox(label=_L("收信人邮箱"), placeholder="Recipient"),
56
+ gr.Textbox(label=_L("标题"), value=_L("测试标题")),
57
+ gr.TextArea(label=_L("正文"), value=_L("SMTP 在线测试工具")),
58
+ gr.Textbox(label=_L("发信人昵称"), value=_L("测试昵称")),
59
+ gr.Textbox(label=_L("发信人邮箱"), placeholder="Sender"),
60
+ gr.Textbox(label=_L("应用密钥"), placeholder="SMTP password"),
61
+ gr.Textbox(label=_L("SMTP 服务器"), value="smtp.163.com"),
62
+ gr.Slider(label=_L("端口"), minimum=0,
63
+ maximum=65535, step=1, value=25),
64
+ ],
65
+ outputs=gr.TextArea(label=_L("发送状态"), show_copy_button=True),
66
+ flagging_mode="never",
67
+ ).launch()
modules/qr.py DELETED
@@ -1,47 +0,0 @@
1
- import gradio as gr
2
- from utils import download_file, EN_US, API_QR, TMP_DIR
3
-
4
- ZH2EN = {
5
- "二维码输出尺寸": "Image size",
6
- "输入文本": "Input text",
7
- "输出二维码": "QR code",
8
- "输入文字在线生成二维码": "Enter text to generate a QR code.",
9
- "状态栏": "Status",
10
- }
11
-
12
-
13
- def _L(zh_txt: str):
14
- return ZH2EN[zh_txt] if EN_US else zh_txt
15
-
16
-
17
- def infer(img_size: int, input_txt: str):
18
- status = "Success"
19
- img = None
20
- try:
21
- if (not input_txt) or input_txt == "0":
22
- raise ValueError("Please input valid text!")
23
-
24
- img = download_file(
25
- f"{API_QR}/?size={img_size}x{img_size}&data={input_txt}",
26
- f"{TMP_DIR}/qrcode.jpg",
27
- )
28
-
29
- except Exception as e:
30
- status = f"{e}"
31
-
32
- return status, img
33
-
34
-
35
- def qrcode():
36
- return gr.Interface(
37
- fn=infer,
38
- inputs=[
39
- gr.Slider(35, 1000, 217, label=_L("二维码输出尺寸")),
40
- gr.Textbox(label=_L("输入文本"), placeholder=_L("输入文字在线生成二维码")),
41
- ],
42
- outputs=[
43
- gr.Textbox(label=_L("状态栏"), show_copy_button=True),
44
- gr.Image(label=_L("输出二维码"), show_share_button=False),
45
- ],
46
- flagging_mode="never",
47
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
modules/smtp.py DELETED
@@ -1,66 +0,0 @@
1
- import requests
2
- import gradio as gr
3
- from utils import API_SMTP, EN_US
4
-
5
- ZH2EN = {
6
- "收信人邮箱": "To email",
7
- "标题": "Title",
8
- "测试标题": "Test title",
9
- "正文": "Content",
10
- "SMTP 在线测试工具": "SMTP online tester",
11
- "发信人昵称": "Sender name",
12
- "测试昵称": "Test nickname",
13
- "发信人邮箱": "From email",
14
- "应用密钥": "API key",
15
- "SMTP 服务器": "SMTP host",
16
- "端口": "Port",
17
- "发送状态": "Status",
18
- }
19
-
20
-
21
- def _L(zh_txt: str):
22
- return ZH2EN[zh_txt] if EN_US else zh_txt
23
-
24
-
25
- def infer(target, title, content, name, email, password, host, port):
26
- try:
27
- response = requests.get(
28
- API_SMTP,
29
- params={
30
- "host": host,
31
- "Port": port,
32
- "key": password, # apikey
33
- "email": email, # from
34
- "mail": target, # to
35
- "title": title, # subject
36
- "name": name, # nickname
37
- "text": content, # content
38
- },
39
- )
40
- if response.status_code == 200:
41
- result: dict = response.json()
42
- return result.get("status")
43
-
44
- else:
45
- raise ConnectionError(f"{response.status_code}")
46
-
47
- except Exception as e:
48
- return f"{e}"
49
-
50
-
51
- def smtp_tester():
52
- return gr.Interface(
53
- fn=infer,
54
- inputs=[
55
- gr.Textbox(label=_L("收信人邮箱"), placeholder="Recipient"),
56
- gr.Textbox(label=_L("标题"), value=_L("测试标题")),
57
- gr.TextArea(label=_L("正文"), value=_L("SMTP 在线测试工具")),
58
- gr.Textbox(label=_L("发信人昵称"), value=_L("测试昵称")),
59
- gr.Textbox(label=_L("发信人邮箱"), placeholder="Sender"),
60
- gr.Textbox(label=_L("应用密钥"), placeholder="SMTP password"),
61
- gr.Textbox(label=_L("SMTP 服务器"), value="smtp.163.com"),
62
- gr.Slider(label=_L("端口"), minimum=0, maximum=65535, step=1, value=25),
63
- ],
64
- outputs=gr.TextArea(label=_L("发送状态"), show_copy_button=True),
65
- flagging_mode="never",
66
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
modules/trans.py DELETED
@@ -1,69 +0,0 @@
1
- import json
2
- import requests
3
- import gradio as gr
4
- from utils import API_TRANS, KEY_TRANS, EN_US
5
-
6
- ZH2EN = {
7
- "输入文本区域": "Input text area",
8
- "在这里输入文本...": "Type the text here...",
9
- "模式": "Mode",
10
- "翻译结果": "Translation results",
11
- "状态栏": "Status",
12
- }
13
-
14
-
15
- def _L(zh_txt: str):
16
- return ZH2EN[zh_txt] if EN_US else zh_txt
17
-
18
-
19
- def infer(source, direction):
20
- status = "Success"
21
- result = None
22
- try:
23
- if not source or not direction:
24
- raise ValueError("请输入有效文本!")
25
-
26
- response = requests.request(
27
- "POST",
28
- API_TRANS,
29
- data=json.dumps(
30
- {
31
- "source": source,
32
- "trans_type": direction,
33
- "request_id": "demo",
34
- "detect": True,
35
- }
36
- ),
37
- headers={
38
- "content-type": "application/json",
39
- "x-authorization": f"token {KEY_TRANS}",
40
- },
41
- )
42
-
43
- result = json.loads(response.text)["target"]
44
-
45
- except Exception as e:
46
- status = f"{e}"
47
-
48
- return status, result
49
-
50
-
51
- def translator():
52
- return gr.Interface(
53
- fn=infer,
54
- inputs=[
55
- gr.TextArea(label=_L("输入文本区域"), placeholder=_L("在这里输入文本...")),
56
- gr.Textbox(label=_L("模式"), value="auto2en"),
57
- ],
58
- outputs=[
59
- gr.Textbox(label=_L("状态栏"), show_copy_button=True),
60
- gr.TextArea(label=_L("翻译结果"), show_copy_button=True),
61
- ],
62
- flagging_mode="never",
63
- examples=[
64
- ["这是最好的翻译服务。", "auto2ja"],
65
- ["これは最高の翻訳サービスです。", "auto2en"],
66
- ["This is the best translation service.", "auto2zh"],
67
- ],
68
- cache_examples=False,
69
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
modules/url.py DELETED
@@ -1,58 +0,0 @@
1
- import json
2
- import requests
3
- import gradio as gr
4
- from utils import is_valid_url, HEADER, EN_US, API_URL
5
-
6
- ZH2EN = {
7
- "输入长链接": "Input a long URL",
8
- "输出短链接": "Output short URL",
9
- "预览短链接": "Preview short URL",
10
- "将长链接转换为短的、易于共享的链接": "Convert long urls into short, easy-to-share links",
11
- "状态栏": "Status",
12
- }
13
-
14
-
15
- def _L(zh_txt: str):
16
- return ZH2EN[zh_txt] if EN_US else zh_txt
17
-
18
-
19
- # outer func
20
- def infer(longUrl: str):
21
- status = "Success"
22
- shortUrl = preview = None
23
- try:
24
- response = requests.post(API_URL, json={"url": longUrl}, headers=HEADER)
25
- if response.status_code == 200:
26
- shortUrl = json.loads(response.text)["shortUrl"]
27
- else:
28
- raise ConnectionError(response.text)
29
-
30
- if is_valid_url(shortUrl):
31
- preview = f'<a href="{shortUrl}" target="_blank">{shortUrl}</a>'
32
-
33
- except Exception as e:
34
- status = f"{e}"
35
-
36
- return status, shortUrl, preview
37
-
38
-
39
- def url_shortner():
40
- return gr.Interface(
41
- fn=infer,
42
- inputs=gr.Textbox(
43
- label=_L("输入长链接"),
44
- placeholder=_L("将长链接转换为短的、易于共享的链接"),
45
- ),
46
- outputs=[
47
- gr.Textbox(label=_L("状态栏"), show_copy_button=True),
48
- gr.Textbox(label=_L("输出短链接"), show_copy_button=True),
49
- gr.HTML(
50
- container=True,
51
- show_label=True,
52
- label=_L("预览短链接"),
53
- ),
54
- ],
55
- flagging_mode="never",
56
- examples=["https://www.bing.com", "https://www.baidu.com"],
57
- cache_examples=False,
58
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
utils.py CHANGED
@@ -1,50 +1,7 @@
1
  import os
2
- import re
3
- import shutil
4
- import requests
5
 
6
  EN_US = os.getenv("LANG") != "zh_CN.UTF-8"
7
- API_QR = os.getenv("api_qr")
8
- API_URL = os.getenv("api_url")
9
  API_SMTP = os.getenv("api_smtp")
10
- API_TRANS = os.getenv("api_caiyun")
11
- KEY_TRANS = os.getenv("apikey_caiyun")
12
- if not (API_SMTP and API_TRANS and KEY_TRANS):
13
  print("请检查环境变量")
14
  exit()
15
-
16
-
17
- TMP_DIR = "./__pycache__"
18
- HEADER = {
19
- "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",
20
- }
21
-
22
-
23
- def clean_dir(dir_path: str):
24
- if os.path.exists(dir_path):
25
- shutil.rmtree(dir_path)
26
-
27
- os.makedirs(dir_path)
28
-
29
-
30
- def is_valid_url(url):
31
- # 定义 URL 的正则表达式
32
- pattern = re.compile(
33
- r"^(https?://)?" # 协议(http 或 https,可选)
34
- r"([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}" # 域名
35
- r"(:\d+)?" # 端口号(可选)
36
- r"(/[^ ]*)?$" # 路径(可选)
37
- )
38
- # 使用正则表达式匹配 URL
39
- return bool(pattern.match(url))
40
-
41
-
42
- def download_file(url, local_filename):
43
- clean_dir(os.path.dirname(local_filename))
44
- response = requests.get(url, stream=True)
45
- response.raise_for_status()
46
- with open(local_filename, "wb") as f:
47
- for chunk in response.iter_content(chunk_size=8192):
48
- f.write(chunk)
49
-
50
- return local_filename
 
1
  import os
 
 
 
2
 
3
  EN_US = os.getenv("LANG") != "zh_CN.UTF-8"
 
 
4
  API_SMTP = os.getenv("api_smtp")
5
+ if not API_SMTP:
 
 
6
  print("请检查环境变量")
7
  exit()