00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00031
00032 #ifdef HAVE_CONFIG_H
00033 #include "autoconfig.h"
00034 #endif
00035
00036 #ifdef HAVE_LASTFMLIB
00037
00038 #include "lastfm_scrobbler.h"
00039 #include "config_manager.h"
00040 #include "metadata_handler.h"
00041 #include "tools.h"
00042 using namespace zmm;
00043
00044
00045 SINGLETON_MUTEX(LastFm, false);
00046
00047 LastFm::LastFm()
00048 : Singleton<LastFm>()
00049 , scrobbler(NULL)
00050 , currentTrackId(-1)
00051 {
00052 }
00053
00054 LastFm::~LastFm()
00055 {
00056 if (currentTrackId != -1 && scrobbler)
00057 finished_playing(scrobbler);
00058 }
00059
00060 void LastFm::init()
00061 {
00062 Ref<ConfigManager> config = ConfigManager::getInstance();
00063
00064 if (!config->getBoolOption(CFG_SERVER_EXTOPTS_LASTFM_ENABLED))
00065 return;
00066
00067 String username = config->getOption(CFG_SERVER_EXTOPTS_LASTFM_USERNAME);
00068 String password = config->getOption(CFG_SERVER_EXTOPTS_LASTFM_PASSWORD);
00069
00070 scrobbler = create_scrobbler(username.c_str(), password.c_str(), 0, 0);
00071 authenticate_scrobbler(scrobbler);
00072 set_commit_only_mode(scrobbler, 1);
00073 }
00074
00075 void LastFm::shutdown()
00076 {
00077 if (!scrobbler)
00078 return;
00079
00080 finished_playing(scrobbler);
00081 destroy_scrobbler(scrobbler);
00082 scrobbler = NULL;
00083 }
00084
00085 void LastFm::startedPlaying(Ref<CdsItem> item)
00086 {
00087 if (currentTrackId == item->getID() || scrobbler == NULL)
00088 return;
00089
00090 currentTrackId = item->getID();
00091
00092 log_debug("Artist:\t%s\n",
00093 item->getMetadata(MetadataHandler::getMetaFieldName(M_ARTIST)).c_str());
00094 log_debug("Title:\t%s\n",
00095 item->getMetadata(MetadataHandler::getMetaFieldName(M_TITLE)).c_str());
00096
00097 String artist =
00098 item->getMetadata(MetadataHandler::getMetaFieldName(M_ARTIST));
00099 String title =
00100 item->getMetadata(MetadataHandler::getMetaFieldName(M_TITLE));
00101
00102 if (!string_ok(artist) || !string_ok(title))
00103 {
00104 finished_playing(scrobbler);
00105 currentTrackId = -1;
00106 return;
00107 }
00108
00109 submission_info* info = create_submission_info();
00110 info->artist = artist.c_str();
00111 info->track = title.c_str();
00112
00113 String album = item->getMetadata(MetadataHandler::getMetaFieldName(M_ALBUM));
00114 if (string_ok(album))
00115 info->album = album.c_str();
00116
00117 String trackNr =
00118 item->getMetadata(MetadataHandler::getMetaFieldName(M_TRACKNUMBER));
00119 if (string_ok(trackNr))
00120 info->track_nr = atoi(trackNr.c_str());
00121
00122 if (item->getResourceCount() > 0)
00123 {
00124 Ref<CdsResource> resource = item->getResource(0);
00125 String duration = resource->getAttribute(MetadataHandler::getResAttrName(R_DURATION));
00126 info->track_length_in_secs = HMSToSeconds(duration.c_str());
00127 }
00128
00129 started_playing(scrobbler, info);
00130
00131 destroy_submission_info(info);
00132 }
00133
00134 #endif//HAVE_LASTFMLIB