def user_ready(username): if username.endswith(".php"): return False uh = UserHistory(username) # First, check to see if the file is fresh uh.load_if_possible() if uh.data_age() < settings.HISTORY_TTL: return uh.num_weeks() # Then, do a quick weeklist fetch to compare try: weeks = list(fetcher.weeks(username)) except AssertionError: # They probably don't exist return None present = True for start, end in weeks: if not uh.has_week(start): present = False break # If all weeks were present, update the timestamp if present: uh.set_timestamp() uh.save_default() return len(weeks) else: return False
def user_export_artist_json(request, username, artist): uh = UserHistory(username) uh.load_if_possible() try: return jsonify({"username": username, "artist":artist, "weeks":uh.artists[artist]}) except KeyError: raise Http404("No such artist.")
def user_root(request, username): ready = ready_or_update(username) if not ready: flash(request, "This user's data is currently out-of-date, and is being updated.") uh = UserHistory(username) uh.load_if_possible() lfuser = LastFmUser.by_username(username) return render(request, "user_root.html", {"username": username, "num_weeks": len(uh.weeks), "lfuser": lfuser})
def user_export_artist_json(request, username, artist): uh = UserHistory(username) uh.load_if_possible() try: return jsonify({ "username": username, "artist": artist, "weeks": uh.artists[artist] }) except KeyError: raise Http404("No such artist.")
def graph_timeline_data(username): uh = UserHistory(username) uh.load_if_possible() series_set = SeriesSet() series_set.add_series(Series( "Plays", uh.week_plays(), "#369f", {0: 4}, )) return series_set
def graph_timeline_data(username): uh = UserHistory(username) uh.load_if_possible() series_set = SeriesSet() series_set.add_series(Series( "Plays", uh.week_plays(), "#369f", {0:4}, )) return series_set
def user_export_all_tabular(request, username, filetype): uh = UserHistory(username) uh.load_if_possible() data = [(("Week", {"bold":True}),("Artist", {"bold":True}),("Plays", {"bold":True}))] for week, artists in uh.weeks.items(): for artist, plays in artists.items(): data.append((week, artist, plays)) try: return as_filetype(data, filetype, filename="%s_all" % username) except KeyError: raise Http404("No such filetype")
def user_export_artist_tabular(request, username, artist, filetype): uh = UserHistory(username) uh.load_if_possible() data = [(("Week", {"bold":True}),("Plays", {"bold":True}))] try: for week, plays in uh.artists[artist].items(): data.append((week, plays)) except KeyError: raise Http404("No such artist.") try: return as_filetype(data, filetype, filename="%s_%s" % (username, artist)) except KeyError: raise Http404("No such filetype")
def user_artists(request, username): uh = UserHistory(username) uh.load_if_possible() artists = [(sum(weeks.values()), artist) for artist, weeks in uh.artists.items()] artists.sort() artists.reverse() try: max_plays = float(artists[0][0]) except IndexError: max_plays = 1000 artists = [(plays, artist, 100*plays/max_plays) for plays, artist in artists] return render(request, "user_artists.html", {"username": username, "artists": artists})
def user_export_artist_tabular(request, username, artist, filetype): uh = UserHistory(username) uh.load_if_possible() data = [(("Week", {"bold": True}), ("Plays", {"bold": True}))] try: for week, plays in uh.artists[artist].items(): data.append((week, plays)) except KeyError: raise Http404("No such artist.") try: return as_filetype(data, filetype, filename="%s_%s" % (username, artist)) except KeyError: raise Http404("No such filetype")
def user_root(request, username): ready = ready_or_update(username) if not ready: flash( request, "This user's data is currently out-of-date, and is being updated.") uh = UserHistory(username) uh.load_if_possible() lfuser = LastFmUser.by_username(username) return render(request, "user_root.html", { "username": username, "num_weeks": len(uh.weeks), "lfuser": lfuser })
def graph_artist(request, username, artist, width=800, height=300): ready_or_update(username) width = int(width) height = int(height) if not width: width = 800 if not height: width = 300 uh = UserHistory(username) uh.load_if_possible() series_set = SeriesSet() series_set.add_series( Series( artist, uh.artist_plays(artist), "#369f", {0: 4}, )) # Create the output output = FileOutput(padding=0, style=artist_detail_white_css) try: scale = AutoWeekDateScale(series_set, short_labels=True, month_gap=2) except ValueError: raise Http404("Bad data (ValueError)") # OK, render that. wg = WaveGraph(series_set, scale, artist_detail_white_css, False, vertical_scale=True) output.add_item(wg, x=0, y=0, width=width, height=height) # Save the images return stream_graph(output)
def user_export_all_tabular(request, username, filetype): uh = UserHistory(username) uh.load_if_possible() data = [(("Week", { "bold": True }), ("Artist", { "bold": True }), ("Plays", { "bold": True }))] for week, artists in uh.weeks.items(): for artist, plays in artists.items(): data.append((week, artist, plays)) try: return as_filetype(data, filetype, filename="%s_all" % username) except KeyError: raise Http404("No such filetype")
def update_user(username): """Returns an up-to-date UserHistory for this username, perhaps creating it on the way or loading from disk.""" uh = UserHistory(username) if uh.has_file(): uh.load_default() try: update_user_history(uh) uh.set_timestamp() # We assume we got all the data for now. except KeyboardInterrupt: pass uh.save_default() return uh
def user_artists(request, username): uh = UserHistory(username) uh.load_if_possible() artists = [(sum(weeks.values()), artist) for artist, weeks in uh.artists.items()] artists.sort() artists.reverse() try: max_plays = float(artists[0][0]) except IndexError: max_plays = 1000 artists = [(plays, artist, 100 * plays / max_plays) for plays, artist in artists] return render(request, "user_artists.html", { "username": username, "artists": artists })
def graph_artist(request, username, artist, width=800, height=300): ready_or_update(username) width = int(width) height = int(height) if not width: width=800 if not height: width=300 uh = UserHistory(username) uh.load_if_possible() series_set = SeriesSet() series_set.add_series(Series( artist, uh.artist_plays(artist), "#369f", {0:4}, )) # Create the output output = FileOutput(padding=0, style=artist_detail_white_css) try: scale = AutoWeekDateScale(series_set, short_labels=True, month_gap=2) except ValueError: raise Http404("Bad data (ValueError)") # OK, render that. wg = WaveGraph(series_set, scale, artist_detail_white_css, False, vertical_scale=True) output.add_item(wg, x=0, y=0, width=width, height=height) # Save the images return stream_graph(output)
def graph_data(poster): uh = UserHistory(poster.user.username) uh.load_if_possible() # Get the start/end pairs of week times weeks = uh.weeks.keys() if not weeks: raise BadData("Empty") weeks.sort() weeks = zip(weeks, weeks[1:]+[weeks[-1]+7*86400]) # Limit those to ones in the graph range pstart = datetime_to_epoch(poster.start) pend = datetime_to_epoch(poster.end) weeks = [(start, end) for start, end in weeks if (end > pstart) and (start < pend)] weekdata = [(start, end, uh.weeks[start].items()) for start, end in weeks] return weekdata
import sys, os def usage(): print >> sys.stderr, "Usage: %s <filename.last> <action>" % sys.argv[0] try: filename = sys.argv[1] except IndexError: usage() sys.exit(1) try: action = sys.argv[2] except IndexError: usage() sys.exit(1) uh = UserHistory(None) uh.load(open(filename)) if action == "artists": print "\n".join(uh.artists.keys()) elif action == "weeks": print "\n".join(map(str, uh.weeks.keys())) elif action == "age": print uh.data_age() else: print >> sys.stderr, "Unknown action" sys.exit(1)
def user_export_all_json(request, username): uh = UserHistory(username) uh.load_if_possible() return jsonify({"username": username, "weeks":uh.weeks})
from lastgui.storage import UserHistory import sys, os def usage(): print >> sys.stderr, "Usage: %s <filename.last> <action>" % sys.argv[0] try: filename = sys.argv[1] except IndexError: usage() sys.exit(1) try: action = sys.argv[2] except IndexError: usage() sys.exit(1) uh = UserHistory(None) uh.load(open(filename)) if action == "artists": print "\n".join(uh.artists.keys()) elif action == "weeks": print "\n".join(map(str, uh.weeks.keys())) elif action == "age": print uh.data_age() else: print >> sys.stderr, "Unknown action" sys.exit(1)
def user_export_all_json(request, username): uh = UserHistory(username) uh.load_if_possible() return jsonify({"username": username, "weeks": uh.weeks})