# Path: lastgui/storage.py
# class UserHistory(object):
#     
#     """
#     Represents a user's listening history, with artist-week-level granularity.
#     """
#     
#     def __init__(self, username):
#         self.username = username
#         self.artists = {}
#         self.weeks = {}
#         self.timestamp = 0
#     
#     def set_plays(self, artist, week, plays):
#         "Set the number of plays for an artist on a week."
#         if artist not in self.artists:
#             self.artists[artist] = {}
#         self.artists[artist][week] = plays
#         if week not in self.weeks:
#             self.weeks[week] = {}
#         self.weeks[week][artist] = plays
#     
#     def delete_week(self, week):
#         "Erases all plays for a week"
#         if week in self.weeks:
#             del self.weeks[week]
#             for artist in self.artists:
#                 if week in self.artists[artist]:
#                     del self.artists[artist][week]
#     
#     def get_plays(self, artist, week):
#         "Gets the number of plays for an artist in a week."
#         return self.artists.get(artist, {}).get(week, 0)
#     
#     def total_artist(self, artist):
#         return sum(self.artists.get(artist, {}).values())
#     
#     def total_week(self, week):
#         return sum(self.weeks.get(week, {}).values())
#     
#     def artist_plays(self, artist):
#         plays = {}
#         for week in self.weeks:
#             plays[week] = self.artists.get(artist, {}).get(week, 0)
#         return plays
#     
#     def week_plays(self):
#         plays = {}
#         for week, artists in self.weeks.items():
#             plays[week] = sum(artists.values())
#         return plays
#     
#     def has_week(self, week):
#         return week in self.weeks
#     
#     def num_weeks(self):
#         return len(self.weeks)
#     
#     def num_artists(self):
#         return len(self.artists)
#     
#     def save(self, file):
#         #print "Saving %s..." % self.username
#         pickle.dump((self.username, self.timestamp, self.artists, self.weeks), file, -1)
#     
#     def save_default(self):
#         self.save(open(self.get_default_path(), "w"))
#     
#     def load(self, file):
#         try:
#             self.username, self.timestamp, self.artists, self.weeks = pickle.load(file)
#         except EOFError:
#             raise ValueError("Invalid pickle file '%s'" % file)
#         #print "Loading %s..." % self.username
#     
#     def load_default(self):
#         self.load(open(self.get_default_path(), "r"))
#     
#     def has_file(self):
#         return os.path.isfile(self.get_default_path())
#     
#     def set_timestamp(self, ttime=None):
#         if ttime is None:
#             ttime = time.time()
#         self.timestamp = ttime
#     
#     def data_age(self):
#         return time.time() - self.timestamp
#     
#     def get_default_path(self):
#         from django.conf import settings
#         return os.path.join(settings.USER_DATA_ROOT, "%s.last" % self.username)
#     
#     def load_if_possible(self):
#         if self.has_file():
#             try:
#                 self.load_default()
#             except ValueError:
#                 pass
# 
# Path: lastgui/fetch.py
# class LastFmFetcher(object):
#     def __init__(self):
#     def fetch(self, url):
#     def weeks(self, username):
#     def weekly_artists(self, username, start, end):
# def update_user_history(uh):
# def update_user(username):


import datetime
from urlparse import urlparse
from shortcuts import *
from lastgui.models import *
from lastgui.storage import UserHistory
from lastgui.fetch import fetcher
from lastgui.data import hotlink_png
from django.conf import settings
from django.http import HttpResponse, HttpResponseRedirect
from django import forms
from lastgui.export import as_filetype
from graphication import FileOutput, Series, SeriesSet, AutoWeekDateScale, Label
from graphication.wavegraph import WaveGraph
from lastgui.css import artist_detail_css, artist_detail_white_css, basic_timeline_css, sig1_css


def user_export_all_json(request, username):
    
    uh = UserHistory(username)
    uh.load_if_possible()
    
    return jsonify({"username": username, "weeks":uh.weeks})


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_export_artist_json(request, username, artist):
    

# Path: lastgui/storage.py
# class UserHistory(object):
#     
#     """
#     Represents a user's listening history, with artist-week-level granularity.
#     """
#     
#     def __init__(self, username):
#         self.username = username
#         self.artists = {}
#         self.weeks = {}
#         self.timestamp = 0
#     
#     def set_plays(self, artist, week, plays):
#         "Set the number of plays for an artist on a week."
#         if artist not in self.artists:
#             self.artists[artist] = {}
#         self.artists[artist][week] = plays
#         if week not in self.weeks:
#             self.weeks[week] = {}
#         self.weeks[week][artist] = plays
#     
#     def delete_week(self, week):
#         "Erases all plays for a week"
#         if week in self.weeks:
#             del self.weeks[week]
#             for artist in self.artists:
#                 if week in self.artists[artist]:
#                     del self.artists[artist][week]
#     
#     def get_plays(self, artist, week):
#         "Gets the number of plays for an artist in a week."
#         return self.artists.get(artist, {}).get(week, 0)
#     
#     def total_artist(self, artist):
#         return sum(self.artists.get(artist, {}).values())
#     
#     def total_week(self, week):
#         return sum(self.weeks.get(week, {}).values())
#     
#     def artist_plays(self, artist):
#         plays = {}
#         for week in self.weeks:
#             plays[week] = self.artists.get(artist, {}).get(week, 0)
#         return plays
#     
#     def week_plays(self):
#         plays = {}
#         for week, artists in self.weeks.items():
#             plays[week] = sum(artists.values())
#         return plays
#     
#     def has_week(self, week):
#         return week in self.weeks
#     
#     def num_weeks(self):
#         return len(self.weeks)
#     
#     def num_artists(self):
#         return len(self.artists)
#     
#     def save(self, file):
#         #print "Saving %s..." % self.username
#         pickle.dump((self.username, self.timestamp, self.artists, self.weeks), file, -1)
#     
#     def save_default(self):
#         self.save(open(self.get_default_path(), "w"))
#     
#     def load(self, file):
#         try:
#             self.username, self.timestamp, self.artists, self.weeks = pickle.load(file)
#         except EOFError:
#             raise ValueError("Invalid pickle file '%s'" % file)
#         #print "Loading %s..." % self.username
#     
#     def load_default(self):
#         self.load(open(self.get_default_path(), "r"))
#     
#     def has_file(self):
#         return os.path.isfile(self.get_default_path())
#     
#     def set_timestamp(self, ttime=None):
#         if ttime is None:
#             ttime = time.time()
#         self.timestamp = ttime
#     
#     def data_age(self):
#         return time.time() - self.timestamp
#     
#     def get_default_path(self):
#         from django.conf import settings
#         return os.path.join(settings.USER_DATA_ROOT, "%s.last" % self.username)
#     
#     def load_if_possible(self):
#         if self.has_file():
#             try:
#                 self.load_default()
#             except ValueError:
#                 pass
# 
# Path: lastgui/fetch.py
# class LastFmFetcher(object):
#     def __init__(self):
#     def fetch(self, url):
#     def weeks(self, username):
#     def weekly_artists(self, username, start, end):
# def update_user_history(uh):
# def update_user(username):


# Path: lastgui/views.py
import datetime
from urlparse import urlparse
from shortcuts import *
from lastgui.models import *
from lastgui.storage import UserHistory
from lastgui.fetch import fetcher
from lastgui.data import hotlink_png
from django.conf import settings
from django.http import HttpResponse, HttpResponseRedirect
from django import forms
from lastgui.export import as_filetype
from graphication import FileOutput, Series, SeriesSet, AutoWeekDateScale, Label
from graphication.wavegraph import WaveGraph
from lastgui.css import artist_detail_css, artist_detail_white_css, basic_timeline_css, sig1_css



def user_export_all_json(request, username):
    
    uh = UserHistory(username)
    uh.load_if_possible()
    
    return jsonify({"username": username, "weeks":uh.weeks})


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_export_artist_json(request, username, artist):
    

    uh = UserHistory(username)