<s> import os <EOL> import sys <EOL> import web <EOL> import time <EOL> import random <EOL> import datetime <EOL> import threading <EOL> from StringIO import StringIO <EOL> FILEROOT = os . path . dirname ( __file__ ) <EOL> sys . path . insert ( <NUM_LIT:0> , os . path . join ( FILEROOT , "<STR_LIT:..>" ) ) <EOL> sys . path . insert ( <NUM_LIT:1> , os . path . join ( FILEROOT , "<STR_LIT:..>" , "<STR_LIT>" ) ) <EOL> os . environ [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> from colorsys import * <EOL> from graphication import * <EOL> from graphication . wavegraph import WaveGraph <EOL> from graphication . color import hex_to_rgba <EOL> from PIL import Image <EOL> import lastslice . shortslice_css as slice_style <EOL> import lastslice . longslice_css as long_style <EOL> from lastgui . fetch import fetcher <EOL> from django . core . cache import cache <EOL> errfile = open ( "<STR_LIT>" , "<STR_LIT:a>" ) <EOL> urls = ( <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> ) <EOL> fetcher . debug = False <EOL> class DataError ( StandardError ) : pass <EOL> def rgba_to_hex ( r , g , b , a ) : <EOL> return "<STR_LIT>" % ( r * <NUM_LIT:255> , g * <NUM_LIT:255> , b * <NUM_LIT:255> , a * <NUM_LIT:255> ) <EOL> class ThreadedWeek ( threading . Thread ) : <EOL> def __init__ ( self , user , start , end ) : <EOL> threading . Thread . __init__ ( self ) <EOL> self . user = user <EOL> self . range = start , end <EOL> def run ( self ) : <EOL> self . data = list ( fetcher . weekly_artists ( self . user , self . range [ <NUM_LIT:0> ] , self . range [ <NUM_LIT:1> ] ) ) <EOL> def get_data ( user , length = <NUM_LIT:4> ) : <EOL> cache_key = '<STR_LIT>' % ( length , user . replace ( "<STR_LIT:U+0020>" , "<STR_LIT:+>" ) ) <EOL> data = None <EOL> while data == "<STR_LIT>" : <EOL> time . sleep ( <NUM_LIT> ) <EOL> if not data : <EOL> try : <EOL> weeks = list ( fetcher . weeks ( user ) ) <EOL> except : <EOL> import traceback <EOL> try : <EOL> errfile . write ( traceback . format_exc ( ) ) <EOL> errfile . flush ( ) <EOL> except : <EOL> pass <EOL> return None , None <EOL> threads = [ ThreadedWeek ( user , start , end ) for start , end in weeks [ - length : ] ] <EOL> for thread in threads : <EOL> thread . start ( ) <EOL> for thread in threads : <EOL> thread . join ( ) <EOL> data = ( [ thread . data for thread in threads ] , weeks [ - length : ] ) <EOL> return data <EOL> def get_series ( user , length = <NUM_LIT:4> , limit = <NUM_LIT:15> ) : <EOL> if "<STR_LIT::>" in user : <EOL> user = user . replace ( "<STR_LIT:_>" , "<STR_LIT:+>" ) <EOL> data , weeks = get_data ( user , length ) <EOL> if not data and not weeks : <EOL> data , weeks = get_data ( user , length ) <EOL> if not data and not weeks : <EOL> data , weeks = get_data ( user , length ) <EOL> if not data and not weeks : <EOL> return None <EOL> artists = { } <EOL> for week in data : <EOL> for artist , plays in week : <EOL> artists [ artist ] = [ ] <EOL> for week in data : <EOL> week = dict ( week ) <EOL> for artist in artists : <EOL> plays = week . get ( artist , <NUM_LIT:0> ) <EOL> if plays < <NUM_LIT:2> : <EOL> plays = <NUM_LIT:0> <EOL> artists [ artist ] . append ( plays ) <EOL> artists = artists . items ( ) <EOL> artists . sort ( key = lambda ( x , y ) : max ( y ) ) <EOL> artists . reverse ( ) <EOL> sh , ss , sv = rgb_to_hsv ( * hex_to_rgba ( "<STR_LIT>" ) [ : <NUM_LIT:3> ] ) <EOL> eh , es , ev = rgb_to_hsv ( * hex_to_rgba ( "<STR_LIT>" ) [ : <NUM_LIT:3> ] ) <EOL> a = True <EOL> ad = <NUM_LIT> <EOL> th , ts , tv = ( eh - sh ) / float ( limit ) , ( es - ss ) / float ( limit ) , ( ev - sv ) / float ( limit ) <EOL> series_set = SeriesSet ( ) <EOL> for artist , data in artists [ : <NUM_LIT:15> ] : <EOL> series_set . add_series ( Series ( <EOL> artist , <EOL> dict ( [ ( datetime . datetime . fromtimestamp ( weeks [ i ] [ <NUM_LIT:0> ] ) , x ) for i , x in enumerate ( data ) ] ) , <EOL> rgba_to_hex ( * ( hsv_to_rgb ( sh , ss , sv ) + ( a and <NUM_LIT:1> or <NUM_LIT:1> - ad , ) ) ) , <EOL> ) ) <EOL> sh += th <EOL> ss += ts <EOL> sv += tv <EOL> a = not a <EOL> ad += ( <NUM_LIT> / limit ) <EOL> return series_set <EOL> class Slice ( object ) : <EOL> def GET ( self , username , width = <NUM_LIT> , height = <NUM_LIT> , labels = False ) : <EOL> web . header ( "<STR_LIT:Content-Type>" , "<STR_LIT>" ) <EOL> width = int ( width ) <EOL> height = int ( height ) <EOL> series_set = get_series ( username ) <EOL> output = FileOutput ( padding = <NUM_LIT:0> , style = slice_style ) <EOL> if series_set : <EOL> scale = AutoWeekDateScale ( series_set , short_labels = True ) <EOL> wg = WaveGraph ( series_set , scale , slice_style , bool ( labels ) , vertical_scale = False ) <EOL> output . add_item ( wg , x = <NUM_LIT:0> , y = <NUM_LIT:0> , width = width , height = height ) <EOL> else : <EOL> output . add_item ( Label ( "<STR_LIT>" ) , x = <NUM_LIT:0> , y = <NUM_LIT:0> , width = width , height = height ) <EOL> print output . stream ( '<STR_LIT>' ) . read ( ) <EOL> class LongSlice ( object ) : <EOL> def GET ( self , username , width = <NUM_LIT> , height = <NUM_LIT> , labels = False ) : <EOL> web . header ( "<STR_LIT:Content-Type>" , "<STR_LIT>" ) <EOL> width = int ( width ) <EOL> height = int ( height ) <EOL> series_set = get_series ( username , <NUM_LIT:12> , <NUM_LIT> ) <EOL> output = FileOutput ( padding = <NUM_LIT:0> , style = long_style ) <EOL> if series_set : <EOL> scale = AutoWeekDateScale ( series_set , year_once = False ) <EOL> wg = WaveGraph ( series_set , scale , long_style , not bool ( labels ) , textfix = True ) <EOL> output . add_item ( wg , x = <NUM_LIT:0> , y = <NUM_LIT:0> , width = width , height = height ) <EOL> else : <EOL> output . add_item ( Label ( "<STR_LIT>" ) , x = <NUM_LIT:0> , y = <NUM_LIT:0> , width = width , height = height ) <EOL> img = Image . open ( output . stream ( '<STR_LIT>' ) ) <EOL> mark = Image . open ( os . path . join ( os . path . dirname ( __file__ ) , "<STR_LIT>" ) ) <EOL> nw , nh = img . size <EOL> nh += <NUM_LIT> <EOL> out = Image . new ( "<STR_LIT>" , ( nw , nh ) , "<STR_LIT>" ) <EOL> out . paste ( img , ( <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> out . paste ( mark , ( width - <NUM_LIT> , height + <NUM_LIT:10> ) ) <EOL> outf = StringIO ( ) <EOL> out . save ( outf , "<STR_LIT>" ) <EOL> outf . seek ( <NUM_LIT:0> ) <EOL> print outf . read ( ) <EOL> class LongSlicePDF ( object ) : <EOL> def GET ( self , username , width = <NUM_LIT> , height = <NUM_LIT> , labels = False ) : <EOL> web . header ( "<STR_LIT:Content-Type>" , "<STR_LIT>" ) <EOL> width = int ( width ) <EOL> height = int ( height ) <EOL> series_set = get_series ( username , <NUM_LIT:12> , <NUM_LIT> ) <EOL> output = FileOutput ( padding = <NUM_LIT:0> , style = long_style ) <EOL> scale = AutoWeekDateScale ( series_set ) <EOL> wg = WaveGraph ( series_set , scale , long_style , not bool ( labels ) , textfix = True ) <EOL> output . add_item ( wg , x = <NUM_LIT:0> , y = <NUM_LIT:0> , width = width , height = height ) <EOL> print output . stream ( '<STR_LIT>' ) . read ( ) <EOL> class Colours : <EOL> def GET ( self , username ) : <EOL> series_set = get_series ( username ) <EOL> for series in series_set : <EOL> print "<STR_LIT>" % ( series . title , series . color ) <EOL> if __name__ == "<STR_LIT:__main__>" : web . run ( urls , globals ( ) ) </s>
