Spaces:
Running
Running
fabiogra
commited on
Commit
·
85a7085
1
Parent(s):
9a79bec
fix: url_is_valid, add wget --spider
Browse files- app/helpers.py +17 -0
app/helpers.py
CHANGED
@@ -1,4 +1,6 @@
|
|
1 |
import json
|
|
|
|
|
2 |
import random
|
3 |
from base64 import b64encode
|
4 |
from io import BytesIO
|
@@ -17,6 +19,11 @@ from streamlit_player import st_player
|
|
17 |
extensions = ["mp3", "wav", "ogg", "flac"] # we will look for all those file types.
|
18 |
|
19 |
|
|
|
|
|
|
|
|
|
|
|
20 |
@st.cache_data(show_spinner=False)
|
21 |
def url_is_valid(url):
|
22 |
if url.startswith("http") is False:
|
@@ -29,6 +36,16 @@ def url_is_valid(url):
|
|
29 |
r = requests.get(url)
|
30 |
r.raise_for_status()
|
31 |
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
except Exception:
|
33 |
st.error("URL is not valid.")
|
34 |
return False
|
|
|
1 |
import json
|
2 |
+
import logging
|
3 |
+
import os
|
4 |
import random
|
5 |
from base64 import b64encode
|
6 |
from io import BytesIO
|
|
|
19 |
extensions = ["mp3", "wav", "ogg", "flac"] # we will look for all those file types.
|
20 |
|
21 |
|
22 |
+
def check_file_availability(url):
|
23 |
+
exit_status = os.system(f"wget --spider {url}")
|
24 |
+
return exit_status == 0
|
25 |
+
|
26 |
+
|
27 |
@st.cache_data(show_spinner=False)
|
28 |
def url_is_valid(url):
|
29 |
if url.startswith("http") is False:
|
|
|
36 |
r = requests.get(url)
|
37 |
r.raise_for_status()
|
38 |
return True
|
39 |
+
except requests.exceptions.HTTPError as err:
|
40 |
+
msg = (
|
41 |
+
"requests get failed with status code "
|
42 |
+
+ str(err.response.status_code)
|
43 |
+
+ " for url "
|
44 |
+
+ url
|
45 |
+
+ ". Try wget spider."
|
46 |
+
)
|
47 |
+
logging.error(msg)
|
48 |
+
return check_file_availability(url)
|
49 |
except Exception:
|
50 |
st.error("URL is not valid.")
|
51 |
return False
|