42
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

1 NEW_SONGS_SYNC = 2

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

2

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

3 class statsengine:

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

4 def __init__(self, statsfile):

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

5 self.__fname = statsfile

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

6 try:

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

7 self.__stats = eval(open(statsfile, "r").read())

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

8 except:

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

9 self.__stats = {}

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

10 self.__new_songs = 0

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

11

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

12 self.current = None

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

13

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

14 def __del__(self):

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

15 self.sync()

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

16

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

17 def sync(self):

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

18 f = open(self.__fname, "w")

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

19 f.write(repr(self.__stats))

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

20 f.close()

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

21

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

22 self.__new_songs = 0

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

23

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

24 def played(self):

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

25 s = str(self.current)

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

26

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

27 if not self.__stats.has_key(s):

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

28 self.__stats[s] = 0

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

29

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

30 self.__stats[s] += 1

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

31

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

32 self.__new_songs += 1

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

33 if self.__new_songs >= NEW_SONGS_SYNC:

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

34 self.sync()

Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

35
