Skip to content

Commit be5828a

Browse files
committed
Properly handle invalid URLs #79
1 parent ffe21dd commit be5828a

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

templates/message.html

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{% extends 'base.html' %}
2+
3+
{% block head %}
4+
<meta content="{{ appname }}" property="og:title" />
5+
<meta content="{{ message }}" property="og:description" />
6+
<meta content="{{ color }}" name="theme-color" />
7+
{% endblock %}
8+
9+
{% block body %}
10+
<div class="container">
11+
<div class="row">
12+
<div class="col-md-12">
13+
<h1>{{ message }}</h1>
14+
</div>
15+
</div>
16+
</div>
17+
{% endblock %}

vxtiktok.py

+24-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import io
1111
import base64
1212
import slideshowBuilder
13-
13+
import html
1414
app = Flask(__name__)
1515
CORS(app)
1616

@@ -29,6 +29,12 @@
2929
"Mozilla/5.0 (compatible; January/1.0; +https://gitlab.insrt.uk/revolt/january)",
3030
"test"]
3131

32+
def message(text):
33+
return render_template(
34+
'message.html',
35+
message = text,
36+
appname=config.currentConfig["MAIN"]["appName"])
37+
3238
def findApiFormat(videoInfo):
3339
for format in videoInfo['formats']:
3440
if format['format_id'] == 'download_addr-0':
@@ -100,16 +106,22 @@ def build_stats_line(videoInfo):
100106
return ""
101107

102108
def getVideoDataFromCacheOrDl(post_link):
103-
cachedItem = cache.getFromCache(post_link)
104-
if cachedItem != None:
105-
videoInfo = cachedItem
106-
else:
107-
videoInfo = getVideoFromPostURL(post_link)
108-
cache.addToCache(post_link, videoInfo)
109-
return videoInfo
109+
try:
110+
cachedItem = cache.getFromCache(post_link)
111+
if cachedItem != None:
112+
videoInfo = cachedItem
113+
else:
114+
videoInfo = getVideoFromPostURL(post_link)
115+
cache.addToCache(post_link, videoInfo)
116+
return videoInfo
117+
except Exception as e:
118+
print(e)
119+
return None
110120

111121
def embed_tiktok(post_link):
112122
videoInfo = getVideoDataFromCacheOrDl(post_link)
123+
if videoInfo == None:
124+
return message("Failed to get video data from TikTok")
113125
if "slideshowData" not in videoInfo or videoInfo["slideshowData"] == None:
114126
vFormat = findApiFormat(videoInfo)
115127
directURL = vFormat['url']
@@ -168,7 +180,10 @@ def embedTiktok(sub_path):
168180
# subdomain can be "vm.vxtiktok.com", "id.vxtiktok.com", "en.vxtiktok.com", etc.
169181
# get main domain from subdomain
170182
subdomain = (request.headers['Host']).split(".")[0]
171-
url = f"https://{subdomain}.tiktok.com{request.path}"
183+
if subdomain != "vxtiktok":
184+
url = f"https://{subdomain}.tiktok.com{request.path}"
185+
else:
186+
url = f"https://tiktok.com{request.path}"
172187
# make a request and get the long URL for yt-dlp
173188
r = requests.get(url, allow_redirects=False)
174189
baseURL = r.headers['location']

0 commit comments

Comments
 (0)