Set duration for youtube videos

This commit is contained in:
Manuel Cortez 2018-02-27 12:33:07 -06:00
parent f17a1a8738
commit 28b19d72e9
2 changed files with 8 additions and 0 deletions

View File

@ -6,4 +6,5 @@ python-vlc
google-api-python-client google-api-python-client
youtube-dl youtube-dl
pyinstaller pyinstaller
isodate
hg+http://hg.q-continuum.net/platform_utils#egg=platform_utils hg+http://hg.q-continuum.net/platform_utils#egg=platform_utils

View File

@ -1,8 +1,10 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import isodate
import youtube_dl import youtube_dl
from googleapiclient.discovery import build from googleapiclient.discovery import build
from googleapiclient.errors import HttpError from googleapiclient.errors import HttpError
from .import baseFile from .import baseFile
from update.utils import seconds_to_string
DEVELOPER_KEY = "AIzaSyCU_hvZJEjLlAGAnlscquKEkE8l0lVOfn0" DEVELOPER_KEY = "AIzaSyCU_hvZJEjLlAGAnlscquKEkE8l0lVOfn0"
YOUTUBE_API_SERVICE_NAME = "youtube" YOUTUBE_API_SERVICE_NAME = "youtube"
@ -21,12 +23,17 @@ class interface(object):
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY) youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY)
search_response = youtube.search().list(q=text, part="id,snippet", maxResults=max_results, type=type).execute() search_response = youtube.search().list(q=text, part="id,snippet", maxResults=max_results, type=type).execute()
self.results = [] self.results = []
ids = []
for search_result in search_response.get("items", []): for search_result in search_response.get("items", []):
if search_result["id"]["kind"] == "youtube#video": if search_result["id"]["kind"] == "youtube#video":
s = baseFile.song(self) s = baseFile.song(self)
s.title = search_result["snippet"]["title"] s.title = search_result["snippet"]["title"]
ids.append(search_result["id"]["videoId"])
s.url = "https://www.youtube.com/watch?v="+search_result["id"]["videoId"] s.url = "https://www.youtube.com/watch?v="+search_result["id"]["videoId"]
self.results.append(s) self.results.append(s)
ssr = youtube.videos().list(id=",".join(ids), part="contentDetails", maxResults=1).execute()
for i in range(len(self.results)):
self.results[i].duration = seconds_to_string(isodate.parse_duration(ssr["items"][i]["contentDetails"]["duration"]).total_seconds())
def get_download_url(self, url): def get_download_url(self, url):
ydl = youtube_dl.YoutubeDL({'quiet': True, 'format': 'bestaudio/best', 'outtmpl': u'%(id)s%(ext)s'}) ydl = youtube_dl.YoutubeDL({'quiet': True, 'format': 'bestaudio/best', 'outtmpl': u'%(id)s%(ext)s'})