dragonxd1 commited on
Commit
13b6bf7
·
verified ·
1 Parent(s): 37f4023

Update DragMusic/utils/thumbnails.py

Browse files
Files changed (1) hide show
  1. DragMusic/utils/thumbnails.py +17 -12
DragMusic/utils/thumbnails.py CHANGED
@@ -7,6 +7,8 @@ import os
7
  import re
8
  import aiofiles
9
  import aiohttp
 
 
10
  from PIL import Image, ImageDraw, ImageEnhance, ImageFilter, ImageFont
11
  from youtubesearchpython.__future__ import VideosSearch
12
 
@@ -109,8 +111,13 @@ def draw_text_with_shadow(background, draw, position, text, font, fill, shadow_o
109
 
110
  async def gen_thumb(videoid: str):
111
  try:
112
- if os.path.isfile(f"cache/{videoid}_v4.png"):
113
- return f"cache/{videoid}_v4.png"
 
 
 
 
 
114
 
115
  url = f"https://www.youtube.com/watch?v={videoid}"
116
  results = VideosSearch(url, limit=1)
@@ -146,8 +153,6 @@ async def gen_thumb(videoid: str):
146
 
147
  async with aiohttp.ClientSession() as session:
148
  async with session.get(thumbnail) as resp:
149
-
150
- content = await resp.read()
151
  if resp.status == 200:
152
  content_type = resp.headers.get('Content-Type')
153
  if 'jpeg' in content_type or 'jpg' in content_type:
@@ -158,14 +163,14 @@ async def gen_thumb(videoid: str):
158
  logging.error(f"Unexpected content type: {content_type}")
159
  return None
160
 
161
- filepath = f"cache/thumb{videoid}.png"
162
  f = await aiofiles.open(filepath, mode="wb")
163
  await f.write(await resp.read())
164
  await f.close()
165
  # os.system(f"file {filepath}")
166
 
167
 
168
- image_path = f"cache/thumb{videoid}.png"
169
  youtube = Image.open(image_path)
170
  image1 = changeImageSize(1280, 720, youtube)
171
 
@@ -181,9 +186,9 @@ async def gen_thumb(videoid: str):
181
  background = Image.blend(background, gradient_image, alpha=0.2)
182
 
183
  draw = ImageDraw.Draw(background)
184
- arial = ImageFont.truetype("AviaxMusic/assets/font2.ttf", 30)
185
- font = ImageFont.truetype("AviaxMusic/assets/font.ttf", 30)
186
- title_font = ImageFont.truetype("AviaxMusic/assets/font3.ttf", 45)
187
 
188
 
189
  circle_thumbnail = crop_center_circle(youtube, 400, 20, start_gradient_color)
@@ -233,13 +238,13 @@ async def gen_thumb(videoid: str):
233
  draw_text_with_shadow(background, draw, (text_x_position, 400), "00:00", arial, (255, 255, 255))
234
  draw_text_with_shadow(background, draw, (1080, 400), duration, arial, (255, 255, 255))
235
 
236
- play_icons = Image.open("AviaxMusic/assets/play_icons.png")
237
  play_icons = play_icons.resize((580, 62))
238
  background.paste(play_icons, (text_x_position, 450), play_icons)
239
 
240
- os.remove(f"cache/thumb{videoid}.png")
241
 
242
- background_path = f"cache/{videoid}_v4.png"
243
  background.save(background_path)
244
 
245
  return background_path
 
7
  import re
8
  import aiofiles
9
  import aiohttp
10
+ import traceback
11
+ import tempfile
12
  from PIL import Image, ImageDraw, ImageEnhance, ImageFilter, ImageFont
13
  from youtubesearchpython.__future__ import VideosSearch
14
 
 
111
 
112
  async def gen_thumb(videoid: str):
113
  try:
114
+ # Create cache directory if it doesn't exist
115
+ cache_dir = "cache"
116
+ if not os.path.exists(cache_dir):
117
+ os.makedirs(cache_dir)
118
+
119
+ if os.path.isfile(f"{cache_dir}/{videoid}_v4.png"):
120
+ return f"{cache_dir}/{videoid}_v4.png"
121
 
122
  url = f"https://www.youtube.com/watch?v={videoid}"
123
  results = VideosSearch(url, limit=1)
 
153
 
154
  async with aiohttp.ClientSession() as session:
155
  async with session.get(thumbnail) as resp:
 
 
156
  if resp.status == 200:
157
  content_type = resp.headers.get('Content-Type')
158
  if 'jpeg' in content_type or 'jpg' in content_type:
 
163
  logging.error(f"Unexpected content type: {content_type}")
164
  return None
165
 
166
+ filepath = f"{cache_dir}/thumb{videoid}.png"
167
  f = await aiofiles.open(filepath, mode="wb")
168
  await f.write(await resp.read())
169
  await f.close()
170
  # os.system(f"file {filepath}")
171
 
172
 
173
+ image_path = f"{cache_dir}/thumb{videoid}.png"
174
  youtube = Image.open(image_path)
175
  image1 = changeImageSize(1280, 720, youtube)
176
 
 
186
  background = Image.blend(background, gradient_image, alpha=0.2)
187
 
188
  draw = ImageDraw.Draw(background)
189
+ arial = ImageFont.truetype("DragMusic/assets/font2.ttf", 30)
190
+ font = ImageFont.truetype("DragMusic/assets/font.ttf", 30)
191
+ title_font = ImageFont.truetype("DragMusic/assets/font3.ttf", 45)
192
 
193
 
194
  circle_thumbnail = crop_center_circle(youtube, 400, 20, start_gradient_color)
 
238
  draw_text_with_shadow(background, draw, (text_x_position, 400), "00:00", arial, (255, 255, 255))
239
  draw_text_with_shadow(background, draw, (1080, 400), duration, arial, (255, 255, 255))
240
 
241
+ play_icons = Image.open("DragMusic/assets/play_icons.png")
242
  play_icons = play_icons.resize((580, 62))
243
  background.paste(play_icons, (text_x_position, 450), play_icons)
244
 
245
+ os.remove(f"{cache_dir}/thumb{videoid}.png")
246
 
247
+ background_path = f"{cache_dir}/{videoid}_v4.png"
248
  background.save(background_path)
249
 
250
  return background_path