lexicalspace commited on
Commit
dab8e09
·
verified ·
1 Parent(s): 9e6a33f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -31
app.py CHANGED
@@ -1,9 +1,6 @@
1
  import streamlit as st
2
  import requests
3
  import json
4
- import time
5
- from PIL import Image
6
- import io
7
  import re
8
  from bs4 import BeautifulSoup
9
  import markdown
@@ -16,7 +13,7 @@ st.set_page_config(
16
  initial_sidebar_state="expanded"
17
  )
18
 
19
- # --- SIDEBAR NAVIGATION ---
20
  st.sidebar.title("Navigation")
21
  query_params = st.query_params
22
  default_index = 1 if query_params.get("mode") == "downloader" else 0
@@ -27,27 +24,40 @@ app_mode = st.sidebar.radio(
27
  index=default_index
28
  )
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  # ==========================================
31
- # VIEW 1: YOUTUBE DOWNLOADER (API Version)
32
  # ==========================================
33
  if app_mode == "YouTube Downloader":
34
  st.title("🎥 YouTube Media Extractor")
35
- st.info("High-Speed Downloader (Powered by Cobalt API)")
36
 
37
  url = st.text_input("Paste YouTube URL", placeholder="https://youtube.com/watch?v=...")
38
 
39
  col1, col2 = st.columns(2)
40
  with col1:
41
- # Cobalt supports various qualities. We'll map them simple.
42
  format_type = st.radio("Format", ["Video (MP4)", "Audio Only (MP3)"])
43
 
44
  if url and st.button("🚀 Process Media"):
45
  status_area = st.empty()
46
- status_area.info("⏳ Contacting processing server...")
47
 
48
  try:
49
- # Cobalt API Configuration
50
- api_url = "https://api.cobalt.tools/api/json"
51
  headers = {
52
  "Accept": "application/json",
53
  "Content-Type": "application/json",
@@ -56,37 +66,44 @@ if app_mode == "YouTube Downloader":
56
 
57
  payload = {
58
  "url": url,
59
- "vCodec": "h264",
60
- "vQuality": "1080",
61
- "aFormat": "mp3",
62
- "isAudioOnly": True if "Audio" in format_type else False
63
  }
64
 
65
- # Send Request
66
- response = requests.post(api_url, headers=headers, json=payload)
67
- data = response.json()
 
 
 
 
 
68
 
 
69
  if "url" in data:
70
  download_link = data["url"]
71
  status_area.success("✅ Media Ready!")
72
 
73
- # Show a Direct Download Link (Fastest method)
74
  st.link_button(f"⬇️ Click to Download {format_type}", download_link)
75
 
76
- # Optional: Preview
77
  if "Audio" not in format_type:
78
  st.video(download_link)
79
  else:
80
  st.audio(download_link)
81
-
 
82
  elif "text" in data:
83
- # API returned an error message
84
- st.error(f"Server Error: {data['text']}")
85
  else:
86
- st.error("Unknown error from processing server.")
87
 
88
  except Exception as e:
89
- st.error(f"Connection Error: {str(e)}")
 
90
 
91
  # ==========================================
92
  # VIEW 2: WEBMASTER TOOLKIT (Existing)
@@ -96,21 +113,17 @@ elif app_mode == "Webmaster Toolkit":
96
 
97
  tab1, tab2 = st.tabs(["📝 Text Cleaner & SEO", "🖼️ Media Optimizer"])
98
 
99
- # --- TEXT CLEANER TAB ---
100
  with tab1:
101
  st.write("Paste your raw text or messy HTML below.")
102
  raw_text = st.text_area("Input Text", height=200)
103
-
104
  c1, c2, c3 = st.columns(3)
105
  processed_text = ""
106
 
107
  if c1.button("Strip HTML"):
108
  soup = BeautifulSoup(raw_text, "html.parser")
109
  processed_text = soup.get_text(separator=" ")
110
-
111
  if c2.button("Fix Spacing"):
112
  processed_text = re.sub(r'\s+', ' ', raw_text).strip()
113
-
114
  if c3.button("MD to HTML"):
115
  processed_text = markdown.markdown(raw_text)
116
 
@@ -118,14 +131,16 @@ elif app_mode == "Webmaster Toolkit":
118
  st.success("Done!")
119
  st.text_area("Result", value=processed_text, height=200)
120
 
121
- # --- MEDIA OPTIMIZER TAB ---
122
  with tab2:
123
  st.write("Convert heavy images to WebP.")
124
- uploaded_file = st.file_uploader("Upload Image", type=['png', 'jpg', 'jpeg'])
 
 
125
 
 
126
  if uploaded_file:
127
  original_image = Image.open(uploaded_file)
128
- st.image(original_image, caption="Original", use_column_width=True)
129
 
130
  if st.button("Convert to WebP"):
131
  buffer = io.BytesIO()
 
1
  import streamlit as st
2
  import requests
3
  import json
 
 
 
4
  import re
5
  from bs4 import BeautifulSoup
6
  import markdown
 
13
  initial_sidebar_state="expanded"
14
  )
15
 
16
+ # --- SIDEBAR & SERVER SELECTOR ---
17
  st.sidebar.title("Navigation")
18
  query_params = st.query_params
19
  default_index = 1 if query_params.get("mode") == "downloader" else 0
 
24
  index=default_index
25
  )
26
 
27
+ # List of Public Cobalt v10 Instances (Community Hosted)
28
+ # If one fails, the user can switch to another.
29
+ SERVERS = {
30
+ "Auto (US)": "https://co.wuk.sh",
31
+ "Backup (EU)": "https://cobalt.api.timelessnesses.me",
32
+ "Backup (Asia)": "https://api.cobalt.750.moe",
33
+ "Backup (US-East)": "https://cobalt-api.hyper.lol"
34
+ }
35
+
36
+ if app_mode == "YouTube Downloader":
37
+ st.sidebar.markdown("---")
38
+ st.sidebar.subheader("⚙️ Server Settings")
39
+ selected_server_name = st.sidebar.selectbox("Processing Server", list(SERVERS.keys()))
40
+ api_base_url = SERVERS[selected_server_name]
41
+
42
  # ==========================================
43
+ # VIEW 1: YOUTUBE DOWNLOADER (v10 API)
44
  # ==========================================
45
  if app_mode == "YouTube Downloader":
46
  st.title("🎥 YouTube Media Extractor")
47
+ st.caption(f"Powered by Cobalt v10 • Server: {selected_server_name}")
48
 
49
  url = st.text_input("Paste YouTube URL", placeholder="https://youtube.com/watch?v=...")
50
 
51
  col1, col2 = st.columns(2)
52
  with col1:
 
53
  format_type = st.radio("Format", ["Video (MP4)", "Audio Only (MP3)"])
54
 
55
  if url and st.button("🚀 Process Media"):
56
  status_area = st.empty()
57
+ status_area.info("⏳ Contacting remote server...")
58
 
59
  try:
60
+ # v10 API Payload Structure
 
61
  headers = {
62
  "Accept": "application/json",
63
  "Content-Type": "application/json",
 
66
 
67
  payload = {
68
  "url": url,
69
+ # v10 uses 'downloadMode' instead of 'isAudioOnly'
70
+ "downloadMode": "audio" if "Audio" in format_type else "auto",
71
+ "videoQuality": "1080",
72
+ "filenameStyle": "basic"
73
  }
74
 
75
+ # Send Request to selected instance
76
+ response = requests.post(f"{api_base_url}", headers=headers, json=payload)
77
+
78
+ try:
79
+ data = response.json()
80
+ except:
81
+ st.error(f"Server Error: {response.text}")
82
+ st.stop()
83
 
84
+ # Handle Success
85
  if "url" in data:
86
  download_link = data["url"]
87
  status_area.success("✅ Media Ready!")
88
 
89
+ # Direct Stream Link (No server storage used)
90
  st.link_button(f"⬇️ Click to Download {format_type}", download_link)
91
 
92
+ # Preview
93
  if "Audio" not in format_type:
94
  st.video(download_link)
95
  else:
96
  st.audio(download_link)
97
+
98
+ # Handle API Specific Errors
99
  elif "text" in data:
100
+ st.error(f"API Error: {data['text']}")
 
101
  else:
102
+ st.error("Unknown error. Try switching the server in the sidebar.")
103
 
104
  except Exception as e:
105
+ st.error(f"Connection Failed: {str(e)}")
106
+ st.info("💡 Tip: Try selecting a different server in the Sidebar.")
107
 
108
  # ==========================================
109
  # VIEW 2: WEBMASTER TOOLKIT (Existing)
 
113
 
114
  tab1, tab2 = st.tabs(["📝 Text Cleaner & SEO", "🖼️ Media Optimizer"])
115
 
 
116
  with tab1:
117
  st.write("Paste your raw text or messy HTML below.")
118
  raw_text = st.text_area("Input Text", height=200)
 
119
  c1, c2, c3 = st.columns(3)
120
  processed_text = ""
121
 
122
  if c1.button("Strip HTML"):
123
  soup = BeautifulSoup(raw_text, "html.parser")
124
  processed_text = soup.get_text(separator=" ")
 
125
  if c2.button("Fix Spacing"):
126
  processed_text = re.sub(r'\s+', ' ', raw_text).strip()
 
127
  if c3.button("MD to HTML"):
128
  processed_text = markdown.markdown(raw_text)
129
 
 
131
  st.success("Done!")
132
  st.text_area("Result", value=processed_text, height=200)
133
 
 
134
  with tab2:
135
  st.write("Convert heavy images to WebP.")
136
+ # Need to import PIL and io only here to save memory
137
+ from PIL import Image
138
+ import io
139
 
140
+ uploaded_file = st.file_uploader("Upload Image", type=['png', 'jpg', 'jpeg'])
141
  if uploaded_file:
142
  original_image = Image.open(uploaded_file)
143
+ st.image(original_image, caption="Original", use_container_width=True) # Updated for Streamlit 1.39+
144
 
145
  if st.button("Convert to WebP"):
146
  buffer = io.BytesIO()