izuemon commited on
Commit
26fb022
·
verified ·
1 Parent(s): ccf19c1

Update watcher.py

Browse files
Files changed (1) hide show
  1. watcher.py +19 -47
watcher.py CHANGED
@@ -5,8 +5,6 @@ import json
5
  import requests
6
  from datetime import datetime, timezone
7
  from bs4 import BeautifulSoup
8
- import ffmpeg
9
- from tflink import TFLinkClient
10
 
11
  # ===== Channel.io 設定 =====
12
  GET_URL = "https://desk-api.channel.io/desk/channels/200605/groups/519217/messages"
@@ -90,27 +88,23 @@ def fetch_download_links(youtube_url):
90
 
91
  return results
92
 
93
- def download_file(url, filename):
94
- with requests.get(url, stream=True) as r:
95
- r.raise_for_status()
96
- with open(filename, 'wb') as f:
97
- for chunk in r.iter_content(chunk_size=8192):
98
- f.write(chunk)
99
- return filename
100
-
101
- def merge_video_audio(video_file, audio_file, output_file):
102
- (
103
- ffmpeg
104
- .input(video_file)
105
- .output(audio_file, output_file, vcodec='copy', acodec='aac', strict='experimental')
106
- .run(overwrite_output=True)
107
- )
108
- return output_file
109
 
110
- def upload_to_tflink(file_path):
111
- client = TFLinkClient()
112
- result = client.upload(file_path)
113
- return result.download_link
114
 
115
  def send_to_channel(text):
116
  payload = {
@@ -136,6 +130,7 @@ def send_to_channel(text):
136
  )
137
  res.raise_for_status()
138
 
 
139
  def main():
140
  while True:
141
  try:
@@ -184,32 +179,9 @@ def main():
184
  time.sleep(10)
185
  continue
186
 
187
- # ===== 最も高画質の動画と音声を選択 =====
188
- video_items = [i for i in items if i["has_audio"] == "false"]
189
- audio_items = [i for i in items if i["has_audio"] == "true" and i["quality"] == "audio"]
190
-
191
- if not video_items or not audio_items:
192
- print("動画または音声が見つかりません")
193
- time.sleep(10)
194
- continue
195
-
196
- # 解像度順にソートして最初を取得
197
- video_items.sort(key=lambda x: int(re.sub(r"\D", "", x["quality"] or "0")), reverse=True)
198
- video_url = video_items[0]["url"]
199
- audio_url = audio_items[0]["url"]
200
-
201
- video_file = download_file(video_url, "video.mp4")
202
- audio_file = download_file(audio_url, "audio.m4a")
203
- output_file = "merged.mp4"
204
-
205
- merge_video_audio(video_file, audio_file, output_file)
206
-
207
- # tflink にアップロード
208
- tflink_url = upload_to_tflink(output_file)
209
-
210
- message_text = f"結合動画のダウンロード: {tflink_url}"
211
  send_to_channel(message_text)
212
- print("送信完了:", tflink_url)
213
 
214
  except Exception as e:
215
  print("エラー:", e)
 
5
  import requests
6
  from datetime import datetime, timezone
7
  from bs4 import BeautifulSoup
 
 
8
 
9
  # ===== Channel.io 設定 =====
10
  GET_URL = "https://desk-api.channel.io/desk/channels/200605/groups/519217/messages"
 
88
 
89
  return results
90
 
91
+ def build_links(items):
92
+ lines = []
93
+ for item in items:
94
+ url = item["url"]
95
+ quality = item["quality"]
96
+ has_audio = item["has_audio"]
97
+
98
+ audio_label = ""
99
+ if has_audio == "false":
100
+ audio_label = "(映像のみ)"
101
+ elif has_audio == "true":
102
+ audio_label = "(音声付き)"
103
+
104
+ line = f'<link type="url" value="{url}"> {quality} {audio_label}</link>'
105
+ lines.append(line)
 
106
 
107
+ return "\n".join(lines)
 
 
 
108
 
109
  def send_to_channel(text):
110
  payload = {
 
130
  )
131
  res.raise_for_status()
132
 
133
+ # ===== Main =====
134
  def main():
135
  while True:
136
  try:
 
179
  time.sleep(10)
180
  continue
181
 
182
+ message_text = build_links(items)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
  send_to_channel(message_text)
184
+ print("送信完了")
185
 
186
  except Exception as e:
187
  print("エラー:", e)