-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
98 lines (76 loc) · 2.66 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Author: Lasith Manujitha
# Github: @z1nc0r3
# Description: A simple plugin to shorten URLs using tinyurl.com
# Date: 2024-02-04
import sys, os
parent_folder_path = os.path.abspath(os.path.dirname(__file__))
sys.path.append(parent_folder_path)
sys.path.append(os.path.join(parent_folder_path, "lib"))
sys.path.append(os.path.join(parent_folder_path, "plugin"))
from flowlauncher import FlowLauncher
import webbrowser
import requests
import pyperclip
import re
class Shortener(FlowLauncher):
def isValidURL(self, str):
regex = (
"((http|https)://)(www.)?"
+ "[a-zA-Z0-9@:%._\\+~#?&//=]"
+ "{1,256}\\.[a-z]"
+ "{2,6}\\b([-a-zA-Z0-9@:%"
+ "._\\+~#?&//=]*)"
)
p = re.compile(regex)
if re.search(p, str):
return True
else:
return False
def query(self, query):
output = []
if len(query.strip()) == 0:
output.append(
{"Title": "Enter a URL to shorten", "IcoPath": "Images/app.png"}
)
else:
if not (query.startswith("http://") or query.startswith("https://")):
query = f"https://{query}"
api_url = f"https://tinyurl.com/api-create.php?url={query}"
try:
if not self.isValidURL(query):
raise ValueError
response = requests.get(api_url)
tiny = response.text
if tiny == "Error":
raise Exception
except Exception:
output.append(
{
"Title": "Error: Enter a valid URL",
"IcoPath": "Images/broken.png",
}
)
return output
output.append(
{
"Title": "Click to copy",
"SubTitle": f"{tiny}",
"IcoPath": "Images/copy.png",
"JsonRPCAction": {"method": "copy", "parameters": [f"{tiny}"]},
}
)
output.append(
{
"Title": "Click to open in browser",
"SubTitle": f"{tiny}",
"IcoPath": "Images/open.png",
"JsonRPCAction": {"method": "open_url", "parameters": [f"{tiny}"]},
}
)
return output
def copy(self, tiny):
pyperclip.copy(tiny)
def open_url(self, url):
webbrowser.open(url)
if __name__ == "__main__":
Shortener()