jpjp9292 commited on
Commit
70f9375
Β·
verified Β·
1 Parent(s): 681df96

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +157 -8
app.py CHANGED
@@ -1,3 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import yt_dlp
3
  import os
@@ -40,7 +184,7 @@ def download_video(url, progress_bar, status_text):
40
  'user_agent': random.choice(user_agents),
41
  'referer': 'https://www.youtube.com/',
42
  'http_chunk_size': random.randint(10000000, 15000000),
43
- 'retries': 1,
44
  'sleep_interval': 3,
45
  'max_sleep_interval': 8,
46
  'headers': {
@@ -63,7 +207,7 @@ def download_video(url, progress_bar, status_text):
63
 
64
  ydl_opts['progress_hooks'] = [progress_hook]
65
 
66
- for attempt in range(3):
67
  try:
68
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
69
  info = ydl.extract_info(url, download=True)
@@ -71,16 +215,22 @@ def download_video(url, progress_bar, status_text):
71
  return filename
72
  except yt_dlp.utils.ExtractorError as e:
73
  if "Sign in to confirm you're not a bot" in str(e):
74
- status_text.text(f"μž¬μ‹œλ„ 쀑... ({attempt + 1}/3)")
75
  time.sleep(random.uniform(3, 5))
76
  continue
77
- raise e
 
 
 
78
  except Exception as e:
79
- if attempt < 2:
80
- status_text.text(f"μž¬μ‹œλ„ 쀑... ({attempt + 1}/3)")
81
  time.sleep(2)
82
  continue
83
- raise e
 
 
 
84
 
85
  except Exception as e:
86
  st.error(f"였λ₯˜ λ°œμƒ: {str(e)}")
@@ -137,4 +287,3 @@ if download_button:
137
  if reset_button:
138
  st.session_state.video_url = ""
139
  st.rerun()
140
-
 
1
+ # import streamlit as st
2
+ # import yt_dlp
3
+ # import os
4
+ # from pathlib import Path
5
+ # import random
6
+ # import time
7
+
8
+ # # νŽ˜μ΄μ§€ μ„€μ •
9
+ # st.set_page_config(page_title="Simple YouTube Downloader", page_icon="πŸ“Ί")
10
+ # st.title("Simple YouTube Downloader πŸ“Ί")
11
+
12
+ # # μŠ€νƒ€μΌ μ„€μ •
13
+ # st.markdown("""
14
+ # <style>
15
+ # .stButton > button {
16
+ # width: 100%;
17
+ # height: 60px;
18
+ # }
19
+ # </style>
20
+ # """, unsafe_allow_html=True)
21
+
22
+ # # λ‹€μš΄λ‘œλ“œ 폴더 생성
23
+ # output_dir = Path("downloads")
24
+ # output_dir.mkdir(exist_ok=True)
25
+
26
+ # def download_video(url, progress_bar, status_text):
27
+ # try:
28
+ # user_agents = [
29
+ # 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
30
+ # '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',
31
+ # 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0'
32
+ # ]
33
+
34
+ # ydl_opts = {
35
+ # 'format': 'best',
36
+ # 'outtmpl': str(output_dir / '%(title)s.%(ext)s'),
37
+ # 'quiet': True,
38
+ # 'no_warnings': True,
39
+ # 'cookiefile': 'youtube.com_cookies.txt',
40
+ # 'user_agent': random.choice(user_agents),
41
+ # 'referer': 'https://www.youtube.com/',
42
+ # 'http_chunk_size': random.randint(10000000, 15000000),
43
+ # 'retries': 3,
44
+ # 'sleep_interval': 3,
45
+ # 'max_sleep_interval': 8,
46
+ # 'headers': {
47
+ # 'Accept-Language': 'en-US,en;q=0.9',
48
+ # 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
49
+ # },
50
+ # }
51
+
52
+ # def progress_hook(d):
53
+ # if d['status'] == 'downloading':
54
+ # try:
55
+ # progress = d['downloaded_bytes'] / d['total_bytes']
56
+ # progress_bar.progress(progress)
57
+ # status_text.text(f"λ‹€μš΄λ‘œλ“œ 쀑: {progress:.1%}")
58
+ # except:
59
+ # status_text.text("λ‹€μš΄λ‘œλ“œ 쀑...")
60
+ # elif d['status'] == 'finished':
61
+ # progress_bar.progress(1.0)
62
+ # status_text.text("처리 μ™„λ£Œ!")
63
+
64
+ # ydl_opts['progress_hooks'] = [progress_hook]
65
+
66
+ # for attempt in range(3):
67
+ # try:
68
+ # with yt_dlp.YoutubeDL(ydl_opts) as ydl:
69
+ # info = ydl.extract_info(url, download=True)
70
+ # filename = ydl.prepare_filename(info)
71
+ # return filename
72
+ # except yt_dlp.utils.ExtractorError as e:
73
+ # if "Sign in to confirm you're not a bot" in str(e):
74
+ # status_text.text(f"μž¬μ‹œλ„ 쀑... ({attempt + 1}/3)")
75
+ # time.sleep(random.uniform(3, 5))
76
+ # continue
77
+ # raise e
78
+ # except Exception as e:
79
+ # if attempt < 2:
80
+ # status_text.text(f"μž¬μ‹œλ„ 쀑... ({attempt + 1}/3)")
81
+ # time.sleep(2)
82
+ # continue
83
+ # raise e
84
+
85
+ # except Exception as e:
86
+ # st.error(f"였λ₯˜ λ°œμƒ: {str(e)}")
87
+ # return None
88
+
89
+ # # μΏ ν‚€ 파일 체크
90
+ # if not os.path.exists('youtube.com_cookies.txt'):
91
+ # st.warning("⚠️ 'youtube.com_cookies.txt' 파일이 ν•„μš”ν•©λ‹ˆλ‹€. YouTube에 둜그인된 μΏ ν‚€ νŒŒμΌμ„ μ—…λ‘œλ“œν•΄μ£Όμ„Έμš”.")
92
+ # uploaded_file = st.file_uploader("μΏ ν‚€ 파일 μ—…λ‘œλ“œ", type=['txt'])
93
+ # if uploaded_file is not None:
94
+ # with open('youtube.com_cookies.txt', 'wb') as f:
95
+ # f.write(uploaded_file.getvalue())
96
+ # st.success("βœ… μΏ ν‚€ 파일이 μ—…λ‘œλ“œλ˜μ—ˆμŠ΅λ‹ˆλ‹€!")
97
+ # st.rerun()
98
+
99
+ # # 메인 UI
100
+ # if 'video_url' not in st.session_state:
101
+ # st.session_state.video_url = ""
102
+
103
+ # video_url = st.text_input("YouTube URL μž…λ ₯:",
104
+ # value=st.session_state.video_url,
105
+ # placeholder="https://www.youtube.com/watch?v=...")
106
+
107
+ # # λ²„νŠΌμ„ 같은 쀄에 배치
108
+ # col1, col2 = st.columns(2)
109
+
110
+ # with col1:
111
+ # download_button = st.button("λ‹€μš΄λ‘œλ“œ", type="primary", key="download_btn", use_container_width=True)
112
+
113
+ # with col2:
114
+ # reset_button = st.button("μ΄ˆκΈ°ν™”", key="reset_btn", use_container_width=True)
115
+
116
+ # # λ‹€μš΄λ‘œλ“œ 둜직
117
+ # if download_button:
118
+ # if video_url:
119
+ # progress_bar = st.progress(0)
120
+ # status_text = st.empty()
121
+
122
+ # downloaded_file = download_video(video_url, progress_bar, status_text)
123
+
124
+ # if downloaded_file and os.path.exists(downloaded_file):
125
+ # with open(downloaded_file, 'rb') as file:
126
+ # st.download_button(
127
+ # label="⬇️ 파일 λ‹€μš΄λ‘œλ“œ",
128
+ # data=file,
129
+ # file_name=os.path.basename(downloaded_file),
130
+ # mime="video/mp4",
131
+ # use_container_width=True
132
+ # )
133
+ # else:
134
+ # st.warning("⚠️ YouTube URL을 μž…λ ₯ν•΄μ£Όμ„Έμš”.")
135
+
136
+ # # μ΄ˆκΈ°ν™” 둜직
137
+ # if reset_button:
138
+ # st.session_state.video_url = ""
139
+ # st.rerun()
140
+
141
+
142
+
143
+
144
+
145
  import streamlit as st
146
  import yt_dlp
147
  import os
 
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': {
 
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)
 
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)}")
 
287
  if reset_button:
288
  st.session_state.video_url = ""
289
  st.rerun()