2018-02-23 13:16:25 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-02-27 12:33:07 -06:00
|
|
|
import isodate
|
2018-02-23 13:16:25 -06:00
|
|
|
import youtube_dl
|
|
|
|
from googleapiclient.discovery import build
|
|
|
|
from googleapiclient.errors import HttpError
|
|
|
|
from .import baseFile
|
2018-02-27 12:33:07 -06:00
|
|
|
from update.utils import seconds_to_string
|
2018-02-23 13:16:25 -06:00
|
|
|
|
|
|
|
DEVELOPER_KEY = "AIzaSyCU_hvZJEjLlAGAnlscquKEkE8l0lVOfn0"
|
|
|
|
YOUTUBE_API_SERVICE_NAME = "youtube"
|
|
|
|
YOUTUBE_API_VERSION = "v3"
|
|
|
|
|
|
|
|
class interface(object):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.results = []
|
2018-02-25 04:54:18 -06:00
|
|
|
self.name = "youtube"
|
|
|
|
self.needs_transcode = True
|
2018-02-23 13:16:25 -06:00
|
|
|
|
|
|
|
def search(self, text, page=1):
|
|
|
|
type = "video"
|
|
|
|
max_results = 20
|
|
|
|
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()
|
|
|
|
self.results = []
|
2018-02-27 12:33:07 -06:00
|
|
|
ids = []
|
2018-02-23 13:16:25 -06:00
|
|
|
for search_result in search_response.get("items", []):
|
|
|
|
if search_result["id"]["kind"] == "youtube#video":
|
|
|
|
s = baseFile.song(self)
|
|
|
|
s.title = search_result["snippet"]["title"]
|
2018-02-27 12:33:07 -06:00
|
|
|
ids.append(search_result["id"]["videoId"])
|
2018-02-23 13:16:25 -06:00
|
|
|
s.url = "https://www.youtube.com/watch?v="+search_result["id"]["videoId"]
|
|
|
|
self.results.append(s)
|
2018-02-27 12:33:07 -06:00
|
|
|
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())
|
2018-02-23 13:16:25 -06:00
|
|
|
|
|
|
|
def get_download_url(self, url):
|
|
|
|
ydl = youtube_dl.YoutubeDL({'quiet': True, 'format': 'bestaudio/best', 'outtmpl': u'%(id)s%(ext)s'})
|
|
|
|
with ydl:
|
|
|
|
result = ydl.extract_info(url, download=False)
|
|
|
|
if 'entries' in result:
|
|
|
|
video = result['entries'][0]
|
|
|
|
else:
|
|
|
|
video = result
|
|
|
|
return video["url"]
|