taslim19
commited on
Commit
·
93fc186
1
Parent(s):
fa83fbc
added cache again
Browse files
DragMusic/utils/thumbnails.py
CHANGED
@@ -6,6 +6,7 @@ import aiofiles
|
|
6 |
import aiohttp
|
7 |
from PIL import Image, ImageDraw, ImageEnhance, ImageFilter, ImageFont
|
8 |
from youtubesearchpython.__future__ import VideosSearch
|
|
|
9 |
|
10 |
logging.basicConfig(level=logging.INFO)
|
11 |
|
@@ -106,9 +107,9 @@ def draw_text_with_shadow(background, draw, position, text, font, fill, shadow_o
|
|
106 |
|
107 |
async def gen_thumb(videoid: str):
|
108 |
try:
|
109 |
-
|
110 |
-
if os.path.isfile(f"
|
111 |
-
return f"
|
112 |
|
113 |
url = f"https://www.youtube.com/watch?v={videoid}"
|
114 |
results = VideosSearch(url, limit=1)
|
@@ -156,14 +157,14 @@ async def gen_thumb(videoid: str):
|
|
156 |
logging.error(f"Unexpected content type: {content_type}")
|
157 |
return None
|
158 |
|
159 |
-
filepath = f"
|
160 |
f = await aiofiles.open(filepath, mode="wb")
|
161 |
await f.write(await resp.read())
|
162 |
await f.close()
|
163 |
# os.system(f"file {filepath}")
|
164 |
|
165 |
|
166 |
-
image_path = f"
|
167 |
youtube = Image.open(image_path)
|
168 |
image1 = changeImageSize(1280, 720, youtube)
|
169 |
|
@@ -235,9 +236,9 @@ async def gen_thumb(videoid: str):
|
|
235 |
play_icons = play_icons.resize((580, 62))
|
236 |
background.paste(play_icons, (text_x_position, 450), play_icons)
|
237 |
|
238 |
-
os.remove(f"
|
239 |
|
240 |
-
background_path = f"
|
241 |
background.save(background_path)
|
242 |
|
243 |
return background_path
|
|
|
6 |
import aiohttp
|
7 |
from PIL import Image, ImageDraw, ImageEnhance, ImageFilter, ImageFont
|
8 |
from youtubesearchpython.__future__ import VideosSearch
|
9 |
+
import tempfile
|
10 |
|
11 |
logging.basicConfig(level=logging.INFO)
|
12 |
|
|
|
107 |
|
108 |
async def gen_thumb(videoid: str):
|
109 |
try:
|
110 |
+
temp_dir = tempfile.gettempdir()
|
111 |
+
if os.path.isfile(os.path.join(temp_dir, f"{videoid}_v4.png")):
|
112 |
+
return os.path.join(temp_dir, f"{videoid}_v4.png")
|
113 |
|
114 |
url = f"https://www.youtube.com/watch?v={videoid}"
|
115 |
results = VideosSearch(url, limit=1)
|
|
|
157 |
logging.error(f"Unexpected content type: {content_type}")
|
158 |
return None
|
159 |
|
160 |
+
filepath = os.path.join(temp_dir, f"thumb{videoid}.png")
|
161 |
f = await aiofiles.open(filepath, mode="wb")
|
162 |
await f.write(await resp.read())
|
163 |
await f.close()
|
164 |
# os.system(f"file {filepath}")
|
165 |
|
166 |
|
167 |
+
image_path = os.path.join(temp_dir, f"thumb{videoid}.png")
|
168 |
youtube = Image.open(image_path)
|
169 |
image1 = changeImageSize(1280, 720, youtube)
|
170 |
|
|
|
236 |
play_icons = play_icons.resize((580, 62))
|
237 |
background.paste(play_icons, (text_x_position, 450), play_icons)
|
238 |
|
239 |
+
os.remove(os.path.join(temp_dir, f"thumb{videoid}.png"))
|
240 |
|
241 |
+
background_path = os.path.join(temp_dir, f"{videoid}_v4.png")
|
242 |
background.save(background_path)
|
243 |
|
244 |
return background_path
|