Refactor music video model and views: rename user to owner, add title field, and implement music management in group detail view
This commit is contained in:
parent
8ed39c78b8
commit
4e28311b1c
11 changed files with 272 additions and 8 deletions
35
game/utils.py
Normal file
35
game/utils.py
Normal file
|
@ -0,0 +1,35 @@
|
|||
from urllib.parse import parse_qs, urlparse
|
||||
|
||||
import requests
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
def parse_musik(raw):
|
||||
if "youtube.com" in raw:
|
||||
return parse_qs(urlparse(raw).query).get("v", [None])[0]
|
||||
elif "youtu.be" in raw:
|
||||
return urlparse(raw).path[1:]
|
||||
|
||||
return raw
|
||||
|
||||
|
||||
def get_yt_title(yt_id):
|
||||
if not yt_id:
|
||||
return None
|
||||
req = requests.get(
|
||||
"https://www.googleapis.com/youtube/v3/videos",
|
||||
params={
|
||||
"part": "snippet",
|
||||
"id": yt_id,
|
||||
"key": settings.YOUTUBE_API_KEY,
|
||||
},
|
||||
)
|
||||
|
||||
if req.status_code != 200:
|
||||
raise Exception(f"Error fetching YouTube video: {req.status_code} {req.text}")
|
||||
|
||||
data = req.json()
|
||||
if not data.get("items"):
|
||||
return None
|
||||
|
||||
return data["items"][0]["snippet"]["title"]
|
Loading…
Add table
Add a link
Reference in a new issue