1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-11-27 08:06:03 +03:00

refactor(skill/youtube_downloader): from module to skill

This commit is contained in:
louistiti 2022-02-22 09:20:54 +08:00
parent 131659928e
commit cf6340b3ca
No known key found for this signature in database
GPG Key ID: 7ECA3DD523793FE6
10 changed files with 212 additions and 0 deletions

View File

@ -136,6 +136,11 @@
"method": "GET",
"route": "/api/action/utilities/speed_test/run",
"params": []
},
{
"method": "GET",
"route": "/api/action/utilities/youtube_downloader/run",
"params": []
}
]
}

View File

@ -0,0 +1,42 @@
{
"actions": {
"run": {
"utterance_samples": [
"Download new videos from YouTube"
]
}
},
"answers": {
"success": [
"All of the videos have been downloaded.",
"I finished to download the videos.",
"New videos are now in my memory."
],
"downloading": [
"I'm downloading %video_title%.",
"I am currently downloading %video_title%.",
"Download for %video_title% has started."
],
"reaching_playlist": [
"I'm trying to collect videos from the YouTube playlist.",
"I'm getting videos from the YouTube playlist.",
"I am reaching the YouTube playlist to collect videos."
],
"nb_to_download": [
"I will download %nb% videos.",
"There are %nb% new videos to download."
],
"nothing_to_download": [
"There is no new video to download. Add a new video to the YouTube playlist if you want to download a new one.",
"I have no new video to download."
],
"settings_errors": [
"Please verify my settings. There is an error for the following reason: %reason%, which includes this message: %message%.",
"I have an error for this reason: %reason%, which includes the following message: %message%. Please check if my settings are correct."
],
"request_errors": [
"I cannot reach YouTube for now. Please verify the URL and your network settings are correct.",
"I currently can't reach YouTube. Do you think YouTube is broken? Please verify the URL and your network settings."
]
}
}

View File

@ -0,0 +1,42 @@
{
"actions": {
"run": {
"utterance_samples": [
"Télécharge les nouvelles vidéos depuis YouTube"
]
}
},
"answers": {
"success": [
"Toutes les vidéos ont été téléchargées.",
"J'ai terminé de télécharger les vidéos.",
"Les nouvelles vidéos sont maintenant enregistrées dans ma mémoire."
],
"downloading": [
"Je télécharge %video_title%.",
"Je suis actuellement en train de télécharger %video_title%.",
"Le téléchargement pour %video_title% a débuté."
],
"reaching_playlist": [
"J'essaye de collecter les vidéos depuis la playlist YouTube.",
"Je récupère les vidéos depuis la playlist YouTube.",
"Je requête la playlist YouTube pour collecter les vidéos."
],
"nb_to_download": [
"Je vais télécharger %nb% vidéos.",
"Il y a %nb% nouvelles vidéos à télécharger."
],
"nothing_to_download": [
"Il n'y a pas de nouvelle vidéo à télécharger. Ajoutez une nouvelle vidéo à la playlist YouTube pour en télécharger une nouvelle.",
"Je n'ai pas de nouvelle vidéo à télécharger."
],
"settings_errors": [
"Merci de vérifier mes paramètres. Il y a une erreur pour la raison suivante : %reason%, qui inclut ce message : %message%.",
"J'ai une erreur pour cette raison : %reason%, qui inclut le message suivant : %message%. Merci de vérifier si paramètres sont corrects."
],
"request_errors": [
"Je ne peux pas atteindre YouTube pour le moment. Merci de vérifier si l'URL ainsi que vos paramètres réseaux sont corrects.",
"Je ne peux actuellement pas atteindre YouTube. Pensez-vous que YouTube est hors ligne ? Merci de vérifier l'URL et vos paramètres réseaux."
]
}
}

View File

@ -0,0 +1,11 @@
{
"name": "YouTube Downloader",
"bridge": "python",
"version": "1.0.0",
"description": "Download new videos from a YouTube playlist.",
"author": {
"name": "Louis Grenard",
"email": "louis.grenard@gmail.com",
"url": "https://github.com/louistiti"
}
}

View File

@ -0,0 +1,97 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import requests
import os
import utils
from time import time
from pytube import YouTube
def run(string, entities):
"""Download new videos from a YouTube playlist"""
db = utils.db()['db']
query = utils.db()['query']
operations = utils.db()['operations']
api_key = utils.config('credentials')['api_key']
playlist_id = utils.config('options')['playlist_id']
# https://developers.google.com/youtube/v3/docs/playlistItems/list
url = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=' + playlist_id + '&key=' + api_key
utils.output('inter', 'reaching_playlist', utils.translate('reaching_playlist'))
# Get videos from the playlist
try:
r = utils.http('GET', url)
# In case there is a problem like wrong settings
if 'error' in r.json():
error = r.json()['error']['errors'][0]
return utils.output('settings_error', 'settings_error', utils.translate('settings_errors', {
'reason': error['reason'],
'message': error['message']
}))
items = r.json()['items']
video_ids = []
videos = []
for item in items:
resource = item['snippet']['resourceId']
if resource['kind'] == 'youtube#video':
video_ids.append(resource['videoId'])
videos.append({
'id': resource['videoId'],
'title': item['snippet']['title']
})
except requests.exceptions.RequestException as e:
return utils.output('request_error', 'request_error', utils.translate('request_errors'))
Entry = query()
# First initialization
if db.count(Entry.platform == 'youtube') == 0:
db.insert({
'platform': 'youtube',
'checked_at': int(time()),
'downloaded_videos': []
})
else:
db.update({ 'checked_at': int(time()) }, Entry.platform == 'youtube')
# Get videos already downloaded
downloaded_videos = db.get(Entry.platform == 'youtube')['downloaded_videos']
to_download = []
for video in videos:
if video['id'] not in downloaded_videos:
to_download.append(video)
nbrto_download = len(to_download)
if nbrto_download == 0:
return utils.output('nothing_to_download', 'nothing_to_download', utils.translate('nothing_to_download'))
utils.output('inter', 'nb_to_download', utils.translate('nb_to_download', {
'nb': nbrto_download
}))
# Create the module downloads directory
skill_dl_dir = utils.create_dl_dir()
for i, video in enumerate(to_download):
utils.output('inter', 'downloading', utils.translate('downloading', {
'video_title': video['title']
}))
# Download the video
yt = YouTube('https://youtube.com/watch?v=' + video['id'])
yt.streams.first().download(skill_dl_dir)
# Add the new downloaded video to the DB
downloaded_videos.append(video['id'])
db.update({ 'downloaded_videos': downloaded_videos }, Entry.platform == 'youtube')
# Will synchronize the content (because "end" type) if synchronization enabled
return utils.output('end', 'success', utils.translate('success'))

View File

@ -0,0 +1,15 @@
{
"configurations": {
"options": {
"playlist_id": "PLAYLIST_ID",
"synchronization": {
"enabled": true,
"method": "direct",
"email": ""
}
},
"credentials": {
"api_key": "YOUR_GOOGLE_API_KEY"
}
}
}