Add script for searching for a song on youtube.

This commit is contained in:
John Maguire 2011-07-08 15:11:48 +00:00
parent d6e6a72250
commit 3df5906250
5 changed files with 57 additions and 0 deletions

BIN
scripts/youtube/YouTube.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 71 KiB

View File

@ -0,0 +1,9 @@
[Script]
name=YouTube
description=Finds songs on YouTube
author=John Maguire <john.maguire@gmail.com>
url=http://www.clementine-player.org
icon=YouTube.png
language=python
script_file=youtube.py

View File

@ -0,0 +1,41 @@
import clementine
from PythonQt.Qt import QUrl
from PythonQt.QtGui import QAction
from PythonQt.QtGui import QDesktopServices
from PythonQt.QtNetwork import QNetworkRequest
import json
class Plugin:
def __init__(self):
self.action = QAction("Find on YouTube", None)
self.action.connect("activated()", self.SearchYoutube)
clementine.ui.AddAction("song_menu", self.action)
self.network = clementine.NetworkAccessManager()
def SearchYoutube(self):
selection = clementine.playlists.current_selection().indexes()
title = selection[clementine.Playlist.Column_Title].data()
artist = selection[clementine.Playlist.Column_Artist].data()
query = title + ' ' + artist
url = QUrl('https://gdata.youtube.com/feeds/api/videos')
url.addQueryItem('q', query)
url.addQueryItem('alt', 'json')
url.addQueryItem('max-results', 1)
reply = self.network.get(QNetworkRequest(url))
def SearchFinished():
data = json.loads(str(reply.readAll()))
feed = data['feed']
try:
print feed['entry'][0]['media$group']['media$player'][0]['url']
youtube_url = feed['entry'][0]['media$group']['media$player'][0]['url']
QDesktopServices.openUrl(QUrl.fromEncoded(str(youtube_url)))
except Exception, e:
print e
reply.connect("finished()", SearchFinished)
plugin = Plugin()

View File

@ -42,8 +42,12 @@
#include "ui/settingsdialog.h"
#include <QFile>
#include <QModelIndex>
#include <QtDebug>
Q_DECLARE_METATYPE(QModelIndex)
Q_DECLARE_METATYPE(QList<QModelIndex>)
const char* PythonEngine::kClementineModuleName = "clementine";
const char* PythonEngine::kScriptModulePrefix = "clementinescripts";
PythonEngine* PythonEngine::sInstance = NULL;
@ -121,6 +125,7 @@ bool PythonEngine::EnsureInitialised() {
RegisterListConverter<Directory>("QList<Directory>");
RegisterListConverter<CoverSearchResult>("QList<CoverSearchResult>");
RegisterListConverter<PlaylistItemPtr>("QList<PlaylistItemPtr>");
RegisterListConverter<QModelIndex>("QList<QModelIndex>");
// Connect stdout, stderr
connect(python_qt, SIGNAL(pythonStdOut(QString)), SLOT(PythonStdOut(QString)));

View File

@ -676,6 +676,8 @@ MainWindow::MainWindow(
scripts_->ui()->RegisterActionLocation("help_menu", ui_->menu_help, NULL);
scripts_->ui()->RegisterActionLocation("playlist_menu", ui_->menu_playlist, NULL);
scripts_->ui()->RegisterActionLocation("song_menu", playlist_menu_, NULL);
// Load theme
StyleSheetLoader* css_loader = new StyleSheetLoader(this);
css_loader->SetStyleSheet(this, ":mainwindow.css");