diff --git a/data/interfaces/default/home_stats.html b/data/interfaces/default/home_stats.html
index 01fb0cdd..36c17d7f 100644
--- a/data/interfaces/default/home_stats.html
+++ b/data/interfaces/default/home_stats.html
@@ -17,19 +17,19 @@ data[array_index]['rows'] :: Usable parameters
row_id Return the db row id for a metadata item if one exists
-== Only if 'stat_id' is 'top_tv' or 'popular_tv' or 'top_movies' or 'popular_movies' or 'last_watched' ==
+== Only if 'stat_id' is 'top_tv' or 'popular_tv' or 'top_movies' or 'popular_movies' or 'top_music' or 'popular_music' or 'last_watched' ==
thumb Return the thumb for the media item.
-== Only if 'stat_id' is 'top_tv' or 'popular_tv' ==
+== Only if 'stat_id' is 'top_tv' or 'popular_tv' or 'top_music' or 'popular_music' ==
grandparent_thumb Returns location of the item's thumbnail. Use with pms_image_proxy.
rating_key Returns the unique identifier for the media item.
title Returns the title for the associated stat.
-== Only if 'stat_id' is 'top_tv' or 'top_movies' or 'top_user' or 'top_platform' ==
+== Only if 'stat_id' is 'top_tv' or 'top_movies' or 'top_music' or 'top_user' or 'top_platform' ==
total_plays Returns the count for the associated stat.
total_duration Returns the total duration for the associated stat.
-== Only of 'stat_id' is 'popular_tv' or 'popular_movies' ==
+== Only of 'stat_id' is 'popular_tv' or 'popular_movies' or 'popular_music' ==
users_watched Returns the count for the associated stat.
== Only if 'stat_id' is 'top_user' or 'last_watched' ==
@@ -372,6 +372,158 @@ DOCUMENTATION :: END
% endif
+ % elif top_stat['stat_id'] == 'top_music' and top_stat['rows']:
+
+ % elif top_stat['stat_id'] == 'popular_music' and top_stat['rows']:
+
% elif top_stat['stat_id'] == 'top_users' and top_stat['rows']:
diff --git a/plexpy/datafactory.py b/plexpy/datafactory.py
index ebf7f339..83698d6e 100644
--- a/plexpy/datafactory.py
+++ b/plexpy/datafactory.py
@@ -324,6 +324,98 @@ class DataFactory(object):
home_stats.append({'stat_id': stat,
'rows': popular_movies})
+ elif 'top_music' in stat:
+ top_music = []
+ try:
+ query = 'SELECT session_history_metadata.id, ' \
+ 'session_history_metadata.grandparent_title, ' \
+ 'COUNT(session_history_metadata.grandparent_title) as total_plays, ' \
+ 'SUM(case when session_history.stopped > 0 ' \
+ 'then (session_history.stopped - session_history.started) ' \
+ ' - (case when session_history.paused_counter is NULL then 0 else session_history.paused_counter end) ' \
+ 'else 0 end) as total_duration, ' \
+ 'session_history_metadata.grandparent_rating_key, ' \
+ 'MAX(session_history.started) as last_watch,' \
+ 'session_history_metadata.grandparent_thumb ' \
+ 'FROM session_history_metadata ' \
+ 'JOIN session_history on session_history_metadata.id = session_history.id ' \
+ 'WHERE datetime(session_history.stopped, "unixepoch", "localtime") ' \
+ '>= datetime("now", "-%s days", "localtime") ' \
+ 'AND session_history_metadata.media_type = "track" ' \
+ 'GROUP BY session_history_metadata.grandparent_title ' \
+ 'ORDER BY %s DESC LIMIT %s' % (time_range, sort_type, stats_count)
+ result = monitor_db.select(query)
+ except:
+ logger.warn("Unable to execute database query.")
+ return None
+
+ for item in result:
+ row = {'title': item[1],
+ 'total_plays': item[2],
+ 'total_duration': item[3],
+ 'users_watched': '',
+ 'rating_key': item[4],
+ 'last_play': item[5],
+ 'grandparent_thumb': item[6],
+ 'thumb': '',
+ 'user': '',
+ 'friendly_name': '',
+ 'platform_type': '',
+ 'platform': '',
+ 'row_id': item[0]
+ }
+ top_music.append(row)
+
+ home_stats.append({'stat_id': stat,
+ 'stat_type': sort_type,
+ 'rows': top_music})
+
+ elif 'popular_music' in stat:
+ popular_music = []
+ try:
+ query = 'SELECT session_history_metadata.id, ' \
+ 'session_history_metadata.grandparent_title, ' \
+ 'COUNT(DISTINCT session_history.user_id) as users_watched, ' \
+ 'session_history_metadata.grandparent_rating_key, ' \
+ 'MAX(session_history.started) as last_watch, ' \
+ 'COUNT(session_history.id) as total_plays, ' \
+ 'SUM(case when session_history.stopped > 0 ' \
+ 'then (session_history.stopped - session_history.started) ' \
+ ' - (case when session_history.paused_counter is NULL then 0 else session_history.paused_counter end) ' \
+ 'else 0 end) as total_duration, ' \
+ 'session_history_metadata.grandparent_thumb ' \
+ 'FROM session_history_metadata ' \
+ 'JOIN session_history ON session_history_metadata.id = session_history.id ' \
+ 'WHERE datetime(session_history.stopped, "unixepoch", "localtime") ' \
+ '>= datetime("now", "-%s days", "localtime") ' \
+ 'AND session_history_metadata.media_type = "track" ' \
+ 'GROUP BY session_history_metadata.grandparent_title ' \
+ 'ORDER BY users_watched DESC, %s DESC ' \
+ 'LIMIT %s' % (time_range, sort_type, stats_count)
+ result = monitor_db.select(query)
+ except:
+ logger.warn("Unable to execute database query.")
+ return None
+
+ for item in result:
+ row = {'title': item[1],
+ 'users_watched': item[2],
+ 'rating_key': item[3],
+ 'last_play': item[4],
+ 'total_plays': item[5],
+ 'grandparent_thumb': item[7],
+ 'thumb': '',
+ 'user': '',
+ 'friendly_name': '',
+ 'platform_type': '',
+ 'platform': '',
+ 'row_id': item[0]
+ }
+ popular_music.append(row)
+
+ home_stats.append({'stat_id': stat,
+ 'rows': popular_music})
+
elif 'top_users' in stat:
top_users = []
try: