#!/usr/bin/env python3
#
# Copyright 2019 Edward G. Bruck <ed.bruck1@gmail.com>
#
# This file is part of Radiotray-NG.
#
# Radiotray-NG is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Radiotray-NG is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Radiotray-NG.  If not, see <http://www.gnu.org/licenses/>.

import dbus
import json
import os
import urllib.parse

try:
    radiotray_ng = dbus.SessionBus().get_object('com.github.radiotray_ng', '/com/github/radiotray_ng')
    player_state = json.loads(radiotray_ng.get_dbus_method('get_player_state', 'com.github.radiotray_ng')())

    if player_state['state'] == "playing":
        if len(player_state['artist']) or len(player_state['title']):
            os.system('xdg-open http://www.google.com/search?q=' +
                      urllib.parse.quote(player_state['artist'] + ', ' + player_state['title']) + ' > /dev/null 2>&1')
except Exception as msg:
    print(msg)
