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

0001 #!/usr/bin/env ruby
0002 ###########################################################################
0003 #   Amarok script for interfacing with SeeqPod.com.                       #
0004 #                                                                         #
0005 #   Copyright                                                             #
0006 #   (C) 2007, 2008 Nikolaj Hald Nielsen <nhnFreespirit@gmail.com>         #
0007 #   (C) 2008 Mark Kretschmann <kretschmann@kde.org>                       #
0008 #                                                                         #
0009 #   This program is free software; you can redistribute it and/or modify  #
0010 #   it under the terms of the GNU General Public License as published by  #
0011 #   the Free Software Foundation; either version 2 of the License, or     #
0012 #   (at your option) any later version.                                   #
0013 #                                                                         #
0014 #   This program is distributed in the hope that it will be useful,       #
0015 #   but WITHOUT ANY WARRANTY; without even the implied warranty of        #
0016 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
0017 #   GNU General Public License for more details.                          #
0018 #                                                                         #
0019 #   You should have received a copy of the GNU General Public License     #
0020 #   along with this program; if not, write to the                         #
0021 #   Free Software Foundation, Inc.,                                       #
0022 #   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         #
0023 ###########################################################################
0024 
0025 require "net/http"
0026 require "rexml/document"
0027 begin
0028   require "Qt4"
0029 rescue LoadError
0030   error = "Qt4-Ruby (Ruby bindings for Qt4) is required for the 'SeeqPod' script.\n\nsudo apt-get install libqt4-ruby"
0031   `kdialog --sorry '#{error}'`
0032   exit 1
0033 end
0034 include REXML
0035 
0036 
0037 def configure
0038   ok = Qt::Boolean.new  # Will become nil if the user presses Cancel
0039   uid = Qt::InputDialog.getText( nil, "Configuration", "Please enter your SeeqPod API UID (check your SeeqPod profile):", Qt::LineEdit::Normal, "", ok )
0040   unless ok.nil?
0041     @uid = uid
0042     @settings.setValue( "uid", Qt::Variant.new( @uid ) )
0043     @settings.sync
0044   end
0045 end
0046 
0047 
0048 app = Qt::Application.new(ARGV)
0049 @settings = Qt::Settings.new( "./seeqpod_service_rc", Qt::Settings::IniFormat )
0050 @uid = @settings.contains( "uid" ) ? @settings.value( "uid" ).toString : ""
0051 service_name = "SeeqPod.com"
0052 
0053 
0054 #SeeqPod has given permission to use their background and logo in the html front page below!
0055 
0056 root_html = "" +
0057 "<HTML>" +
0058 "    <STYLE type=\"text/css\">" +
0059 "        BODY {" +
0060 "            margin: 0px;" +
0061 "            padding: 0px;" +
0062 "            font-family: Arial, Helvetica, sans-serif;" +
0063 "            font-size: 13px;" +
0064 "            background-color: #FFF;" +
0065 "            color: #878787;" +
0066 "            background-attachment: fixed;" +
0067 "            background-image:url(\"http://www.seeqpod.com/css/page/bg2.jpg\");" +
0068 "            background-repeat: no-repeat;" +
0069 "            background-position: center top;" +
0070 "        }" +
0071 "        DIV { text-align: center;  }" +
0072 "    </STYLE>" +
0073 "    <BODY>" +
0074 "        <DIV>" +
0075 "        <br><br>" +
0076 "        <IMG src=\"http://www.seeqpod.com/images/logo.png\" alt=\"SeeqPod.com\">" +
0077 "        <br><br>" +
0078 "        Welcome to the Amarok 2 SeeqPod.com scripted service. To search SeeqPod, simply type in your search in the search box in the service and expand the item that appears in the list. By default, this script will only return at most 100 matches, but by appending an #offset to your search, it will return further matches. For instance, typing in \"Foobar\" will return the first 100 matches, \"Foobar#100\" the next 100, and so on." +
0079 "        </DIV>" +
0080 "    </BODY>" +
0081 "<HTML>"
0082 
0083 
0084 loop do
0085     message = gets
0086     puts "script got message" + message
0087     args = message.chomp.split(" ")
0088 
0089     case args[0]
0090         when "configure"
0091             configure
0092 
0093         when "init"
0094             #2 levels, categories and stations
0095             levels = "2"
0096             short_description = "Search and stream from SeeqPod.com"
0097 
0098             # init new browser
0099             system("qdbus", "org.kde.amarok", "/ScriptableServiceManager", "initService", service_name, levels, short_description, root_html, "true" )
0100 
0101         when "populate"
0102             configure if @uid.empty?
0103             filter = "_none_"
0104 
0105             offset = 0;
0106 
0107             if args.length == 5
0108                 callback = args[4].strip();
0109                 callback_args = callback.chomp.split("#")
0110                 filter = callback_args[0];
0111 
0112                 if callback_args.length == 2
0113                     offset = callback_args[1].to_i
0114                 end
0115 
0116                 name = filter.gsub( "%20", " " );
0117                 name = name.strip();
0118 
0119             else
0120                 name = "Enter Query..."
0121             end
0122 
0123             if args[1].strip() == "1"
0124                 puts " Populating main level..."
0125 
0126                 html = "The results of your query for: " + filter;
0127 
0128                 if offset > 0
0129                     name = name + " ( " + offset.to_s + " - " + (offset + 100).to_s + " )"
0130                 end
0131 
0132                 system("qdbus", "org.kde.amarok", "/ScriptableServiceManager", "insertItem", service_name, "1", "-1", name, html, filter, "" )
0133 
0134                 #tell service that all items has been added ( no parent since these are top level items )
0135                `qdbus org.kde.amarok /ScriptableServiceManager donePopulating "SeeqPod.com" "-1"`
0136                 
0137             else if args[1].strip() == "0"
0138                 parent_id = args[2]
0139 
0140                 url = "http://www.seeqpod.com/api/v0.2/<UID>/music/search/<QUERY>/<OFFSET>/100"
0141 
0142                 url = url.gsub( "<UID>", @uid )
0143                 url = url.gsub( "<QUERY>", filter )
0144                 url = url.gsub( "<OFFSET>", offset.to_s );
0145 
0146                 #fetch results
0147 
0148                 data = Net::HTTP.get_response(URI.parse(url)).body
0149 
0150                 #some brute force parsing....
0151                 doc = REXML::Document.new(data)
0152                 titles = []
0153                 links = []
0154                 doc.elements.each('playlist/trackList/track/title') do |ele|
0155                     titles << ele.text
0156                 end
0157                 doc.elements.each('playlist/trackList/track/location') do |ele|
0158                     links << ele.text
0159                 end
0160 
0161                 count = 0
0162                         
0163                 titles.each_with_index do |title, idx|
0164                     link = links[idx]
0165 
0166                     system("qdbus", "org.kde.amarok", "/ScriptableServiceManager", "insertItem", service_name, "0", parent_id, title, "", "", link )
0167                     count = count + 1
0168                 end
0169 
0170                 #tell service that all items has been added to a parent item
0171                 system("qdbus", "org.kde.amarok", "/ScriptableServiceManager", "donePopulating", service_name, parent_id )
0172 
0173             end
0174         end
0175     end
0176 end
0177