jpjp9292 commited on
Commit
d299e32
β€’
1 Parent(s): 93d97ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +164 -164
app.py CHANGED
@@ -142,164 +142,12 @@
142
 
143
 
144
 
145
- # import streamlit as st
146
- # import yt_dlp
147
- # import os
148
- # from pathlib import Path
149
- # import random
150
- # import time
151
-
152
- # # νŽ˜μ΄μ§€ μ„€μ •
153
- # st.set_page_config(page_title="Simple YouTube Downloader", page_icon="πŸ“Ί")
154
- # st.title("Simple YouTube Downloader πŸ“Ί")
155
-
156
- # # μŠ€νƒ€μΌ μ„€μ •
157
- # st.markdown("""
158
- # <style>
159
- # .stButton > button {
160
- # width: 100%;
161
- # height: 60px;
162
- # }
163
- # </style>
164
- # """, unsafe_allow_html=True)
165
-
166
- # # λ‹€μš΄λ‘œλ“œ 폴더 생성
167
- # output_dir = Path("downloads")
168
- # output_dir.mkdir(exist_ok=True)
169
-
170
- # def download_video(url, progress_bar, status_text):
171
- # try:
172
- # user_agents = [
173
- # 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
174
- # 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2.1 Safari/605.1.15',
175
- # 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0'
176
- # ]
177
-
178
- # ydl_opts = {
179
- # 'format': 'best',
180
- # 'outtmpl': str(output_dir / '%(title)s.%(ext)s'),
181
- # 'quiet': True,
182
- # 'no_warnings': True,
183
- # 'cookiefile': 'youtube.com_cookies.txt',
184
- # 'user_agent': random.choice(user_agents),
185
- # 'referer': 'https://www.youtube.com/',
186
- # 'http_chunk_size': random.randint(10000000, 15000000),
187
- # 'retries': 5, # μž¬μ‹œλ„ 횟수λ₯Ό 5둜 늘림
188
- # 'sleep_interval': 3,
189
- # 'max_sleep_interval': 8,
190
- # 'headers': {
191
- # 'Accept-Language': 'en-US,en;q=0.9',
192
- # 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
193
- # },
194
- # }
195
-
196
- # def progress_hook(d):
197
- # if d['status'] == 'downloading':
198
- # try:
199
- # progress = d['downloaded_bytes'] / d['total_bytes']
200
- # progress_bar.progress(progress)
201
- # status_text.text(f"λ‹€μš΄λ‘œλ“œ 쀑: {progress:.1%}")
202
- # except:
203
- # status_text.text("λ‹€μš΄λ‘œλ“œ 쀑...")
204
- # elif d['status'] == 'finished':
205
- # progress_bar.progress(1.0)
206
- # status_text.text("처리 μ™„λ£Œ!")
207
-
208
- # ydl_opts['progress_hooks'] = [progress_hook]
209
-
210
- # for attempt in range(5): # μž¬μ‹œλ„ 횟수λ₯Ό 5둜 μ„€μ •
211
- # try:
212
- # with yt_dlp.YoutubeDL(ydl_opts) as ydl:
213
- # info = ydl.extract_info(url, download=True)
214
- # filename = ydl.prepare_filename(info)
215
- # return filename
216
- # except yt_dlp.utils.ExtractorError as e:
217
- # if "Sign in to confirm you're not a bot" in str(e):
218
- # status_text.text(f"μž¬μ‹œλ„ 쀑... ({attempt + 1}/5)")
219
- # time.sleep(random.uniform(3, 5))
220
- # continue
221
- # status_text.text(f"였λ₯˜ λ°œμƒ: {str(e)}")
222
- # st.error(f"λ‹€μš΄λ‘œλ“œ μ‹€νŒ¨: {str(e)}") # μ‹€νŒ¨ λ©”μ‹œμ§€ ν‘œμ‹œ
223
- # st.info("λ¬Έμ œκ°€ 지속될 경우 URL이 μ˜¬λ°”λ₯Έμ§€ ν™•μΈν•˜κ³  λ‹€μ‹œ μ‹œλ„ν•˜μ„Έμš”.") # μΆ”κ°€ λ©”λͺ¨
224
- # return None
225
- # except Exception as e:
226
- # if attempt < 4:
227
- # status_text.text(f"μž¬μ‹œλ„ 쀑... ({attempt + 1}/5)")
228
- # time.sleep(2)
229
- # continue
230
- # status_text.text(f"였λ₯˜ λ°œμƒ: {str(e)}")
231
- # st.error(f"λ‹€μš΄λ‘œλ“œ μ‹€νŒ¨: {str(e)}") # μ‹€νŒ¨ λ©”μ‹œμ§€ ν‘œμ‹œ
232
- # st.info("λ¬Έμ œκ°€ 지속될 경우 URL이 μ˜¬λ°”λ₯Έμ§€ ν™•μΈν•˜κ³  λ‹€μ‹œ μ‹œλ„ν•˜μ„Έμš”.") # μΆ”κ°€ λ©”λͺ¨
233
- # return None
234
-
235
- # except Exception as e:
236
- # st.error(f"였λ₯˜ λ°œμƒ: {str(e)}")
237
- # return None
238
-
239
- # # μΏ ν‚€ 파일 체크
240
- # if not os.path.exists('youtube.com_cookies.txt'):
241
- # st.warning("⚠️ 'youtube.com_cookies.txt' 파일이 ν•„μš”ν•©λ‹ˆλ‹€. YouTube에 둜그인된 μΏ ν‚€ νŒŒμΌμ„ μ—…λ‘œλ“œν•΄μ£Όμ„Έμš”.")
242
- # uploaded_file = st.file_uploader("μΏ ν‚€ 파일 μ—…λ‘œλ“œ", type=['txt'])
243
- # if uploaded_file is not None:
244
- # with open('youtube.com_cookies.txt', 'wb') as f:
245
- # f.write(uploaded_file.getvalue())
246
- # st.success("βœ… μΏ ν‚€ 파일이 μ—…λ‘œλ“œλ˜μ—ˆμŠ΅λ‹ˆλ‹€!")
247
- # st.rerun()
248
-
249
- # # 메인 UI
250
- # if 'video_url' not in st.session_state:
251
- # st.session_state.video_url = ""
252
-
253
- # video_url = st.text_input("YouTube URL μž…λ ₯:",
254
- # value=st.session_state.video_url,
255
- # placeholder="https://www.youtube.com/watch?v=...")
256
-
257
- # # λ²„νŠΌμ„ 같은 쀄에 배치
258
- # col1, col2 = st.columns(2)
259
-
260
- # with col1:
261
- # download_button = st.button("λ‹€μš΄λ‘œλ“œ", type="primary", key="download_btn", use_container_width=True)
262
-
263
- # with col2:
264
- # reset_button = st.button("μ΄ˆκΈ°ν™”", key="reset_btn", use_container_width=True)
265
-
266
- # # λ‹€μš΄λ‘œλ“œ 둜직
267
- # if download_button:
268
- # if video_url:
269
- # progress_bar = st.progress(0)
270
- # status_text = st.empty()
271
-
272
- # downloaded_file = download_video(video_url, progress_bar, status_text)
273
-
274
- # if downloaded_file and os.path.exists(downloaded_file):
275
- # with open(downloaded_file, 'rb') as file:
276
- # st.download_button(
277
- # label="⬇️ 파일 λ‹€μš΄λ‘œλ“œ",
278
- # data=file,
279
- # file_name=os.path.basename(downloaded_file),
280
- # mime="video/mp4",
281
- # use_container_width=True
282
- # )
283
- # else:
284
- # st.warning("⚠️ YouTube URL을 μž…λ ₯ν•΄μ£Όμ„Έμš”.")
285
-
286
- # # μ΄ˆκΈ°ν™” 둜직
287
- # if reset_button:
288
- # st.session_state.video_url = ""
289
- # st.rerun()
290
-
291
-
292
-
293
  import streamlit as st
294
  import yt_dlp
295
  import os
296
  from pathlib import Path
297
  import random
298
  import time
299
- import logging
300
-
301
- # λ‘œκΉ… μ„€μ •
302
- logging.basicConfig(level=logging.ERROR, filename='error.log')
303
 
304
  # νŽ˜μ΄μ§€ μ„€μ •
305
  st.set_page_config(page_title="Simple YouTube Downloader", page_icon="πŸ“Ί")
@@ -335,10 +183,10 @@ def download_video(url, progress_bar, status_text):
335
  'cookiefile': 'youtube.com_cookies.txt',
336
  'user_agent': random.choice(user_agents),
337
  'referer': 'https://www.youtube.com/',
338
- 'http_chunk_size': random.randint(100000000, 150000000),
339
- 'retries': 5, # μž¬μ‹œλ„ 횟수
340
  'sleep_interval': 3,
341
- 'max_sleep_interval': 10,
342
  'headers': {
343
  'Accept-Language': 'en-US,en;q=0.9',
344
  'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
@@ -351,8 +199,7 @@ def download_video(url, progress_bar, status_text):
351
  progress = d['downloaded_bytes'] / d['total_bytes']
352
  progress_bar.progress(progress)
353
  status_text.text(f"λ‹€μš΄λ‘œλ“œ 쀑: {progress:.1%}")
354
- except Exception as e:
355
- logging.error(f"Progress Hook Error: {e}")
356
  status_text.text("λ‹€μš΄λ‘œλ“œ 쀑...")
357
  elif d['status'] == 'finished':
358
  progress_bar.progress(1.0)
@@ -371,21 +218,22 @@ def download_video(url, progress_bar, status_text):
371
  status_text.text(f"μž¬μ‹œλ„ 쀑... ({attempt + 1}/5)")
372
  time.sleep(random.uniform(3, 5))
373
  continue
374
- logging.error(f"Extractor Error: {str(e)}")
375
- st.info("λ¬Έμ œκ°€ 지속될 경우 URL이 μ˜¬λ°”λ₯Έμ§€ ν™•μΈν•˜κ³  λ‹€μ‹œ μ‹œλ„ν•˜μ„Έμš”.")
 
376
  return None
377
  except Exception as e:
378
- logging.error(f"Download Error: {str(e)}")
379
  if attempt < 4:
380
  status_text.text(f"μž¬μ‹œλ„ 쀑... ({attempt + 1}/5)")
381
  time.sleep(2)
382
  continue
383
- st.info("λ¬Έμ œκ°€ 지속될 경우 λ‹€λ₯Έ URL을 μ‹œλ„ν•΄μ£Όμ„Έμš”.")
 
 
384
  return None
385
 
386
  except Exception as e:
387
- logging.error(f"General Error: {str(e)}")
388
- st.info("λ¬Έμ œκ°€ 지속될 경우 λ‹€λ₯Έ URL을 μ‹œλ„ν•΄μ£Όμ„Έμš”.")
389
  return None
390
 
391
  # μΏ ν‚€ 파일 체크
@@ -438,5 +286,157 @@ if download_button:
438
  # μ΄ˆκΈ°ν™” 둜직
439
  if reset_button:
440
  st.session_state.video_url = ""
441
- st.rerun() # st.experimental_rerun()을 st.rerun()으둜 λ³€κ²½ν–ˆμŠ΅λ‹ˆλ‹€.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
442
 
 
142
 
143
 
144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
  import streamlit as st
146
  import yt_dlp
147
  import os
148
  from pathlib import Path
149
  import random
150
  import time
 
 
 
 
151
 
152
  # νŽ˜μ΄μ§€ μ„€μ •
153
  st.set_page_config(page_title="Simple YouTube Downloader", page_icon="πŸ“Ί")
 
183
  'cookiefile': 'youtube.com_cookies.txt',
184
  'user_agent': random.choice(user_agents),
185
  'referer': 'https://www.youtube.com/',
186
+ 'http_chunk_size': random.randint(10000000, 15000000),
187
+ 'retries': 5, # μž¬μ‹œλ„ 횟수λ₯Ό 5둜 늘림
188
  'sleep_interval': 3,
189
+ 'max_sleep_interval': 8,
190
  'headers': {
191
  'Accept-Language': 'en-US,en;q=0.9',
192
  'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
 
199
  progress = d['downloaded_bytes'] / d['total_bytes']
200
  progress_bar.progress(progress)
201
  status_text.text(f"λ‹€μš΄λ‘œλ“œ 쀑: {progress:.1%}")
202
+ except:
 
203
  status_text.text("λ‹€μš΄λ‘œλ“œ 쀑...")
204
  elif d['status'] == 'finished':
205
  progress_bar.progress(1.0)
 
218
  status_text.text(f"μž¬μ‹œλ„ 쀑... ({attempt + 1}/5)")
219
  time.sleep(random.uniform(3, 5))
220
  continue
221
+ status_text.text(f"였λ₯˜ λ°œμƒ: {str(e)}")
222
+ st.error(f"λ‹€μš΄λ‘œλ“œ μ‹€νŒ¨: {str(e)}") # μ‹€νŒ¨ λ©”μ‹œμ§€ ν‘œμ‹œ
223
+ st.info("λ¬Έμ œκ°€ 지속될 경우 URL이 μ˜¬λ°”λ₯Έμ§€ ν™•μΈν•˜κ³  λ‹€μ‹œ μ‹œλ„ν•˜μ„Έμš”.") # μΆ”κ°€ λ©”λͺ¨
224
  return None
225
  except Exception as e:
 
226
  if attempt < 4:
227
  status_text.text(f"μž¬μ‹œλ„ 쀑... ({attempt + 1}/5)")
228
  time.sleep(2)
229
  continue
230
+ status_text.text(f"였λ₯˜ λ°œμƒ: {str(e)}")
231
+ st.error(f"λ‹€μš΄λ‘œλ“œ μ‹€νŒ¨: {str(e)}") # μ‹€νŒ¨ λ©”μ‹œμ§€ ν‘œμ‹œ
232
+ st.info("λ¬Έμ œκ°€ 지속될 경우 URL이 μ˜¬λ°”λ₯Έμ§€ ν™•μΈν•˜κ³  λ‹€μ‹œ μ‹œλ„ν•˜μ„Έμš”.") # μΆ”κ°€ λ©”λͺ¨
233
  return None
234
 
235
  except Exception as e:
236
+ st.error(f"였λ₯˜ λ°œμƒ: {str(e)}")
 
237
  return None
238
 
239
  # μΏ ν‚€ 파일 체크
 
286
  # μ΄ˆκΈ°ν™” 둜직
287
  if reset_button:
288
  st.session_state.video_url = ""
289
+ st.rerun()
290
+
291
+
292
+
293
+ # import streamlit as st
294
+ # import yt_dlp
295
+ # import os
296
+ # from pathlib import Path
297
+ # import random
298
+ # import time
299
+ # import logging
300
+
301
+ # # λ‘œκΉ… μ„€μ •
302
+ # logging.basicConfig(level=logging.ERROR, filename='error.log')
303
+
304
+ # # νŽ˜μ΄μ§€ μ„€μ •
305
+ # st.set_page_config(page_title="Simple YouTube Downloader", page_icon="πŸ“Ί")
306
+ # st.title("Simple YouTube Downloader πŸ“Ί")
307
+
308
+ # # μŠ€νƒ€μΌ μ„€μ •
309
+ # st.markdown("""
310
+ # <style>
311
+ # .stButton > button {
312
+ # width: 100%;
313
+ # height: 60px;
314
+ # }
315
+ # </style>
316
+ # """, unsafe_allow_html=True)
317
+
318
+ # # λ‹€μš΄λ‘œλ“œ 폴더 생성
319
+ # output_dir = Path("downloads")
320
+ # output_dir.mkdir(exist_ok=True)
321
+
322
+ # def download_video(url, progress_bar, status_text):
323
+ # try:
324
+ # user_agents = [
325
+ # 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
326
+ # 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2.1 Safari/605.1.15',
327
+ # 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0'
328
+ # ]
329
+
330
+ # ydl_opts = {
331
+ # 'format': 'best',
332
+ # 'outtmpl': str(output_dir / '%(title)s.%(ext)s'),
333
+ # 'quiet': True,
334
+ # 'no_warnings': True,
335
+ # 'cookiefile': 'youtube.com_cookies.txt',
336
+ # 'user_agent': random.choice(user_agents),
337
+ # 'referer': 'https://www.youtube.com/',
338
+ # 'http_chunk_size': random.randint(100000000, 150000000),
339
+ # 'retries': 5, # μž¬μ‹œλ„ 횟수
340
+ # 'sleep_interval': 3,
341
+ # 'max_sleep_interval': 10,
342
+ # 'headers': {
343
+ # 'Accept-Language': 'en-US,en;q=0.9',
344
+ # 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
345
+ # },
346
+ # }
347
+
348
+ # def progress_hook(d):
349
+ # if d['status'] == 'downloading':
350
+ # try:
351
+ # progress = d['downloaded_bytes'] / d['total_bytes']
352
+ # progress_bar.progress(progress)
353
+ # status_text.text(f"λ‹€μš΄λ‘œλ“œ 쀑: {progress:.1%}")
354
+ # except Exception as e:
355
+ # logging.error(f"Progress Hook Error: {e}")
356
+ # status_text.text("λ‹€μš΄λ‘œλ“œ 쀑...")
357
+ # elif d['status'] == 'finished':
358
+ # progress_bar.progress(1.0)
359
+ # status_text.text("처리 μ™„λ£Œ!")
360
+
361
+ # ydl_opts['progress_hooks'] = [progress_hook]
362
+
363
+ # for attempt in range(5): # μž¬μ‹œλ„ 횟수λ₯Ό 5둜 μ„€μ •
364
+ # try:
365
+ # with yt_dlp.YoutubeDL(ydl_opts) as ydl:
366
+ # info = ydl.extract_info(url, download=True)
367
+ # filename = ydl.prepare_filename(info)
368
+ # return filename
369
+ # except yt_dlp.utils.ExtractorError as e:
370
+ # if "Sign in to confirm you're not a bot" in str(e):
371
+ # status_text.text(f"μž¬μ‹œλ„ 쀑... ({attempt + 1}/5)")
372
+ # time.sleep(random.uniform(3, 5))
373
+ # continue
374
+ # logging.error(f"Extractor Error: {str(e)}")
375
+ # st.info("λ¬Έμ œκ°€ 지속될 경우 URL이 μ˜¬λ°”λ₯Έμ§€ ν™•μΈν•˜κ³  λ‹€μ‹œ μ‹œλ„ν•˜μ„Έμš”.")
376
+ # return None
377
+ # except Exception as e:
378
+ # logging.error(f"Download Error: {str(e)}")
379
+ # if attempt < 4:
380
+ # status_text.text(f"μž¬μ‹œλ„ 쀑... ({attempt + 1}/5)")
381
+ # time.sleep(2)
382
+ # continue
383
+ # st.info("λ¬Έμ œκ°€ 지속될 경우 λ‹€λ₯Έ URL을 μ‹œλ„ν•΄μ£Όμ„Έμš”.")
384
+ # return None
385
+
386
+ # except Exception as e:
387
+ # logging.error(f"General Error: {str(e)}")
388
+ # st.info("λ¬Έμ œκ°€ 지속될 경우 λ‹€λ₯Έ URL을 μ‹œλ„ν•΄μ£Όμ„Έμš”.")
389
+ # return None
390
+
391
+ # # μΏ ν‚€ 파일 체크
392
+ # if not os.path.exists('youtube.com_cookies.txt'):
393
+ # st.warning("⚠️ 'youtube.com_cookies.txt' 파일이 ν•„μš”ν•©λ‹ˆλ‹€. YouTube에 둜그인된 μΏ ν‚€ νŒŒμΌμ„ μ—…λ‘œλ“œν•΄μ£Όμ„Έμš”.")
394
+ # uploaded_file = st.file_uploader("μΏ ν‚€ 파일 μ—…λ‘œλ“œ", type=['txt'])
395
+ # if uploaded_file is not None:
396
+ # with open('youtube.com_cookies.txt', 'wb') as f:
397
+ # f.write(uploaded_file.getvalue())
398
+ # st.success("βœ… μΏ ν‚€ 파일이 μ—…λ‘œλ“œλ˜μ—ˆμŠ΅λ‹ˆλ‹€!")
399
+ # st.rerun()
400
+
401
+ # # 메인 UI
402
+ # if 'video_url' not in st.session_state:
403
+ # st.session_state.video_url = ""
404
+
405
+ # video_url = st.text_input("YouTube URL μž…λ ₯:",
406
+ # value=st.session_state.video_url,
407
+ # placeholder="https://www.youtube.com/watch?v=...")
408
+
409
+ # # λ²„νŠΌμ„ 같은 쀄에 배치
410
+ # col1, col2 = st.columns(2)
411
+
412
+ # with col1:
413
+ # download_button = st.button("λ‹€μš΄λ‘œλ“œ", type="primary", key="download_btn", use_container_width=True)
414
+
415
+ # with col2:
416
+ # reset_button = st.button("μ΄ˆκΈ°ν™”", key="reset_btn", use_container_width=True)
417
+
418
+ # # λ‹€μš΄λ‘œλ“œ 둜직
419
+ # if download_button:
420
+ # if video_url:
421
+ # progress_bar = st.progress(0)
422
+ # status_text = st.empty()
423
+
424
+ # downloaded_file = download_video(video_url, progress_bar, status_text)
425
+
426
+ # if downloaded_file and os.path.exists(downloaded_file):
427
+ # with open(downloaded_file, 'rb') as file:
428
+ # st.download_button(
429
+ # label="⬇️ 파일 λ‹€μš΄λ‘œλ“œ",
430
+ # data=file,
431
+ # file_name=os.path.basename(downloaded_file),
432
+ # mime="video/mp4",
433
+ # use_container_width=True
434
+ # )
435
+ # else:
436
+ # st.warning("⚠️ YouTube URL을 μž…λ ₯ν•΄μ£Όμ„Έμš”.")
437
+
438
+ # # μ΄ˆκΈ°ν™” 둜직
439
+ # if reset_button:
440
+ # st.session_state.video_url = ""
441
+ # st.rerun() # st.experimental_rerun()을 st.rerun()으둜 λ³€κ²½ν–ˆμŠ΅λ‹ˆλ‹€.
442