#! /usr/bin/env python __rcsId__ = """$Id: filminfo.py,v 1.8 2002/01/23 22:50:55 bellman Exp $""" import string import operator import time def hms_to_seconds(t): if ':' in t: h,ms = string.split(t, ':', 1) else: h = 0 ms = t if '.' in ms: m,s = string.split(ms, '.', 1) else: m,s = ms,'0' return int(h)*3600 + int(m)*60 + int(s) def seconds_to_hms(secs, force_s=0): mins,s = divmod(secs, 60) if force_s < 0: if s + mins%2 > 30: # If s==30, round to even minutes mins = mins + 1 s = 0 h,m = divmod(mins, 60) t = '%d:%02d' % (h, m) if force_s > 0 or (s and force_s >= 0): t = t + ('.%02d' % (s,)) return t def seconds_to_dhms(secs, force_s=0): d,s = divmod(secs, 86400) t = seconds_to_hms(s, force_s) if d: t = ('%dd ' % (d,)) + t return t def runtime_as_seconds(runtime): secs = reduce(operator.add, map(hms_to_seconds, string.split(runtime, '+')), 0) return secs def runtime_as_hms(runtime, force_s=0): return seconds_to_hms(runtime_as_seconds(runtime), force_s) def filmlength_in_seconds(film): return runtime_as_seconds(film.length) def sum_runtimes(films, force_s=0, show_days=0): films = filter(lambda f: hasattr(f, 'length'), films) secs = reduce(operator.add, map(filmlength_in_seconds, films), 0) if show_days: t = seconds_to_dhms(secs, force_s) else: t = seconds_to_hms(secs, force_s) return t def count_medias(films): mediacounts = {} for f in films: if not hasattr(f, 'media'): continue medias = map(string.strip, string.split(f.media, '+')) for m in medias: l = map(string.strip, string.split(m, '*')) m = l[0] if len(l) == 1: n = 1 else: n = int(l[1]) mediacounts[m] = mediacounts.get(m, 0) + n return mediacounts def count_studios(films): studiocounts = {} for f in films: if hasattr(f, 'studio'): st = f.studio else: st = None studiocounts[st] = studiocounts.get(st, 0) + 1 return studiocounts def shorten_title(title, maxlen): diff = len(title) - maxlen if diff > 0: title = (title[:maxlen/2-3+(maxlen%2)] + " ... " + title[-(maxlen/2-2):]) return title def film_line(film): t = shorten_title(film.title, 50) m = film.media.split()[0].split('-')[0] try: l = sum_runtimes([film]) except: l = "?:??" return "%-50s %-5s %5s" % (t,m,l) def print_films(films, linedelay=0): for i in range(len(films)): if i != 0: time.sleep(linedelay) try: print film_line(films[i]) except: pass