#!/usr/bin/env python
# This program shows me the average scrobbles per day.
# Sure, the website tells me as well, but I want to have at least a few
# decimals, to see the changes over time. It also displays the average scrobbles
# and artists for the last 7, 30, 90, 180 and 365 on one page

from datetime import date, datetime
import time
from lxml import html
import requests
import re

# set a few things like days and the integer for the for loop
days = 7, 30, 90, 180, 365
d = len(days)

# Enter your details here. Date format is DD/MM/YYYY
username = 'StonedEars'
joined = '09/01/2009'

# Get the days between today and the lastfm joined date
date_format = "%d/%m/%Y"
date_joined = datetime.strptime(joined, date_format)
today = datetime.strptime(time.strftime("%d/%m/%Y"), date_format)
delta = today - date_joined

# Print some sort of header
print("Stats for " + username + ":\n")

# This for-loop iterates 5 times to get the average, the playcount and the scrobbled artists for each number of days
# stored in the days tuple and displays the output
for i in range (d):
    lastpage_scrobbles = requests.get('https://www.last.fm/user/' + username + '/library?date_preset=LAST_' + str(days[i]) + '_DAYS')
    lastpage_artists = requests.get('https://www.last.fm/user/' + username + '/library/artists?date_preset=LAST_' + str(days[i]) + '_DAYS')
    laststuff_scrobbles = html.fromstring(lastpage_scrobbles.content)
    laststuff_artists = html.fromstring(lastpage_artists.content)
    lastpage_scrobbles = laststuff_scrobbles.xpath('//*[@id="mantle_skin"]/div[4]/div/div[1]/ul[1]/li[1]/p/text()')
    lastpage_artists = laststuff_artists.xpath('//*[@id="mantle_skin"]/div[4]/div/div[1]/ul/li/p/text()')
    lastavg = laststuff_scrobbles.xpath('//*[@id="mantle_skin"]/div[4]/div/div[1]/ul[1]/li[2]/p/text()')
    # print the results from the website
    print ("Last " + str(days[i]) + " Days:")
    print ("Scrobbled Artists: " + lastpage_artists[0])
    print ("Scrobbled Tracks: " + lastpage_scrobbles[0])
    print ("Average: " + lastavg[0] + "\n")

# get the current playcount from lastfm overall
page_scrobbles = requests.get('https://www.last.fm/user/' + username)
stuff = html.fromstring(page_scrobbles.content)
page_scrobbles = stuff.xpath('//*[@id="content"]/div[2]/header/div[2]/div/div[2]/div[2]/ul/li[1]/p/a/text()')
cleancount = int(re.sub("[^\d\.]", "", page_scrobbles[0])) / int(delta.days + 1)

# get the current amount of different artists scrobbled from lastfm overall
page_artists = requests.get('https://www.last.fm/user/' + username + '/library/artists')
stuff_artists = html.fromstring(page_artists.content)
artists = stuff_artists.xpath('//*[@id="mantle_skin"]/div[4]/div/div[1]/ul/li/p/text()')

# Display the overall average
print ("Overall:")
print ("Scrobbled Artists: " + artists[0])
print ("Scrobbled Tracks: " + page_scrobbles[0])
print ("Passed Days: " + str(int(delta.days + 1)))
print ("Average: " + str("%.4f" % cleancount))

@@ -21,275 +21,157 @@
 n%0A# 
-This program shows me the average scrobbles per day.%0A# Sure, the website tells me as well, but I want to have at least a few%0A# decimals, to see the changes over time. It also displays the average scrobbles%0A# and artists for the last 7, 30, 90, 180 and 365 on one page
+Shows all scrobbling info for the last 7, 30, 90, 180, 360 days as well as%0A# the scrobbled artist for that timeframe along with the average scrobbles
 %0A%0Afr
@@ -525,16 +525,32 @@
 ed date%0A
+def date():%0A    
 date_for
@@ -566,16 +566,20 @@
 /%25m/%25Y%22%0A
+    
 date_joi
@@ -623,16 +623,20 @@
 format)%0A
+    
 today = 
@@ -693,16 +693,20 @@
 format)%0A
+    
 delta = 
@@ -724,16 +724,38 @@
 e_joined
+%0A    return delta.days
 %0A%0A# Prin
@@ -2324,33 +2324,29 @@
 0%5D)) / int(d
-elta.days
+ate()
  + 1)%0A%0A# get
@@ -2824,25 +2824,21 @@
 tr(int(d
-elta.days
+ate()
  + 1)))%0A

update date function