File indexing completed on 2025-01-05 04:25:26

0001 /*#########################################################################
0002 #   Amarok script for interfacing with SeeqPod.com.                       #
0003 #                                                                         #
0004 #   Copyright                                                             #
0005 #   (C) 2007, 2008 Nikolaj Hald Nielsen <nhnFreespirit@gmail.com>         #
0006 #   (C) 2008 Mark Kretschmann <kretschmann@kde.org>                       #
0007 #                                                                         #
0008 #   This program is free software; you can redistribute it and/or modify  #
0009 #   it under the terms of the GNU General Public License as published by  #
0010 #   the Free Software Foundation; either version 2 of the License, or     #
0011 #   (at your option) any later version.                                   #
0012 #                                                                         #
0013 #   This program is distributed in the hope that it will be useful,       #
0014 #   but WITHOUT ANY WARRANTY; without even the implied warranty of        #
0015 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
0016 #   GNU General Public License for more details.                          #
0017 #                                                                         #
0018 #   You should have received a copy of the GNU General Public License     #
0019 #   along with this program; if not, write to the                         #
0020 #   Free Software Foundation, Inc.,                                       #
0021 #   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         #
0022 #########################################################################*/
0023 
0024 
0025 Importer.loadQtBinding( "qt.core" );
0026 Importer.loadQtBinding( "qt.xml" );
0027 Importer.loadQtBinding( "qt.network" );
0028 Importer.loadQtBinding( "qt.gui" );
0029 
0030 QByteArray.prototype.toString = function() 
0031 {
0032     ts = new QTextStream( this, QIODevice.ReadOnly );
0033     return ts.readAll();
0034 }
0035 
0036 function configure()
0037 {
0038     print ("Seeqpod onConfigure");
0039     uid = "";
0040 
0041 
0042     uid = QInputDialog.getText( 0, "Seeqpod Script", "Please enter your uid", QLineEdit.Normal, "", 0 );
0043 
0044     if ( uid != "" ) {
0045         Amarok.debug ("got " + uid );
0046         Amarok.Script.writeConfig( "uid", uid );
0047     }
0048     
0049 }
0050 
0051 function Seeqpod()
0052 {
0053    // onConfigure();
0054     print ("Seeqpod Seeqpod");
0055     
0056     var currentDir = Amarok.Info.scriptPath();
0057     print( "got current dir: " + currentDir );
0058 
0059 
0060     //get config file ( if it exists, otherwise, ask for uid )
0061     readConfig();
0062 
0063 
0064 
0065     var file = new QFile( currentDir + "/SeeqpodService.html" );
0066     file.open( QIODevice.OpenMode( QIODevice.ReadOnly, QIODevice.Text ) );
0067 
0068     while ( !file.atEnd() ) {
0069         html += file.readLine().toString();
0070     }
0071 
0072     Amarok.debug ("creating service...");
0073     Amarok.debug ("html: " + html );
0074     ScriptableServiceScript.call( this, "Seeqpod.com", 2, "Search and stream music from all over the web", html, true );
0075     Amarok.debug ("done creating service!");
0076 }
0077 
0078 
0079 
0080 function readConfig()
0081 {
0082     print ("Seeqpod readConfig");
0083 
0084     uid = Amarok.Script.readConfig( "uid", "" );
0085     if ( uid == "" )
0086         configure();
0087 }
0088 
0089 
0090 function queryResult( reply )
0091 {
0092 
0093     try
0094     {
0095     
0096         doc.setContent( reply );
0097 
0098         trackElements = doc.elementsByTagName( "track" );
0099 
0100         Amarok.debug ("got " + trackElements.length() + " tracks!");
0101 
0102         var titles = new Array( trackElements.length() );
0103         var links = new Array( trackElements.length() );
0104         var artists = new Array( trackElements.length() );
0105         var albums = new Array( trackElements.length() );
0106 
0107 
0108         var i = 0;
0109         for ( ; i < trackElements.length(); i++ )
0110         {
0111 
0112 
0113             elt = trackElements.at( i );
0114             elt2 = elt.firstChildElement( "title" );
0115 
0116             titles[i] = elt2.text();
0117 
0118             elt2 = elt.firstChildElement( "location" );
0119             links[i] = elt2.text();
0120 
0121             elt2 = elt.firstChildElement( "album" );
0122             albums[i] = elt2.text();
0123 
0124             elt2 = elt.firstChildElement( "creator" );
0125             artists[i] = elt2.text();
0126 
0127 
0128         }
0129 
0130 
0131         for( i = 0; i < trackElements.length(); i++ )
0132         {
0133 
0134             title = titles[i]
0135             link = links[i];
0136             album = albums[i];
0137             artist = artists[i]
0138 
0139 
0140             item = Amarok.StreamItem;
0141             item.level = 0;
0142             item.callbackData = "";
0143             item.itemName = title;
0144             item.artist = artist;
0145             item.album = album;
0146             item.playableUrl = link;
0147             item.infoHtml = "";
0148 
0149             script.insertItem( item );
0150 
0151         }
0152 
0153     }
0154     catch( err )
0155     {
0156         Amarok.debug( err );
0157     }
0158 
0159     script.donePopulating();
0160 
0161 }
0162 
0163 function onPopulate( level, callback, filter )
0164 {
0165     print ("Seeqpod onPopulate");
0166     Amarok.debug( "Seeqpod onPopulate, filter: " + filter );
0167     offset = 0;
0168 
0169     offsetMarker = filter.lastIndexOf("#");
0170     
0171     if ( offsetMarker != -1 ) {
0172         Amarok.debug( "non 0 marker at " + offsetMarker );
0173         offset = filter.substring( offsetMarker + 1, filter.length )
0174                 Amarok.debug( "Got offset: " + offset );
0175         offset = offset.replace( "%20", " " );
0176         offset = parseInt( offset );
0177         
0178         Amarok.debug( "Got offset: " + offset );
0179         //offset = offset.trim();
0180         
0181         filter = filter.substring( 0, offsetMarker );
0182                 
0183         Amarok.debug( "Got final offset: " + offset );
0184         Amarok.debug( "Got filter: " + filter );
0185     }
0186     
0187 
0188     if ( filter != "" )
0189     {
0190         name = filter.replace( "%20", " " );
0191     }
0192     else
0193     {
0194         name = "Enter Query..."
0195     }
0196 
0197     if ( level == 1 ) {
0198         
0199         if ( offset > 0 )
0200             name = name + " ( " + offset + " - " + (offset + 100) + " )";
0201 
0202         item = Amarok.StreamItem;
0203         item.level = 1;
0204         item.callbackData = "dummy";
0205         item.itemName = name;
0206         item.playableUrl = "";
0207         item.infoHtml = html;
0208         script.insertItem( item );
0209 
0210         script.donePopulating();
0211 
0212     }
0213     else if ( level == 0 )
0214     {
0215         Amarok.debug( " Populating result level..." );
0216         Amarok.debug( " url: " +  callback );
0217 
0218         try{
0219 
0220             path = "http://www.seeqpod.com/api/v0.2/<UID>/music/search/<QUERY>/<OFFSET>/100";
0221 
0222             path = path.replace( "<UID>", uid )
0223             path = path.replace( "<QUERY>", filter )
0224             path = path.replace( "<OFFSET>", offset );
0225             qurl = new QUrl( path );
0226 
0227             b = new Downloader( qurl, queryResult );
0228         }
0229         catch( err )
0230         {
0231             Amarok.debug( err );
0232         }
0233 
0234     }
0235 }
0236 
0237 
0238 http = new QHttp;
0239 data = new QBuffer;
0240 doc = new QDomDocument("doc");
0241 elt = new QDomElement;
0242 elt2 = new QDomElement;
0243 bookElements = new QDomNodeList;
0244 html = "";
0245 uid = "";
0246 
0247 script = new Seeqpod();
0248 script.populate.connect( onPopulate );
0249 
0250