Use utils.formatTime

This commit is contained in:
HarHar 2013-01-24 17:54:38 -02:00
parent d9d30d9279
commit 052bd09540

View File

@ -6,18 +6,19 @@ import sys
import os
import socket
import threading
from syncplay import utils
class Bot(object):
def __init__(self, server='irc.rizon.net', serverPassword='', port=6667, nick='SyncBot', nickservPass='', channel='', channelPassword='', functions=[]):
#Arguments
# server - IRC Server to connect to
# server - IRC Server to connect to
# serverPassword - probably empty
# port - usually between 6660 - 6669, 7000
# nick - duh
# port - usually between 6660 - 6669, 7000
# nick - duh
# nickservPass - if not given, NickServ identification string won't be sent
# channel - channel to autojoin and interact with
# channel - channel to autojoin and interact with
# channelPassword - if channel is +k
# functions - list/tuple of functions that can be used from the bot:
# functions - list/tuple of functions that can be used from the bot:
# * pause(room, state=bool)
# * getRooms() -> list
# * getPosition(room) -> int
@ -114,7 +115,7 @@ class Bot(object):
users = self.functions[4](room)
paused = self.functions[5](room)
out = chr(2) + '<Paused>' + chr(15) if paused else chr(2) + '<Playing>' + chr(15)
out += ' [' + elapsed_time(self.functions[2](room)) + '/' + elapsed_time(users[0]['length']) + '] '
out += ' [' + utils.formatTime(self.functions[2](room)) + '/' + utils.formatTime(users[0]['length']) + '] '
out += users[0]['file']
self.msg(to, out)
out = 'Users: '
@ -201,32 +202,3 @@ def handlingThread(sock, bot):
msg = msg[:-1].lstrip()
# ---END WTF BLOCK- --
bot.irc_onMsg(nfrom, addrnfrom, to, msg)
def elapsed_time(seconds, suffixes=['y','w','d','','',''], add_s=False, separator=':'):
"""
Takes an amount of seconds and turns it into a human-readable amount of time.
"""
# the formatted time string to be returned
time = []
# the pieces of time to iterate over (days, hours, minutes, etc)
# - the first piece in each tuple is the suffix (d, h, w)
# - the second piece is the length in seconds (a day is 60s * 60m * 24h)
parts = [(suffixes[0], 60 * 60 * 24 * 7 * 52),
(suffixes[1], 60 * 60 * 24 * 7),
(suffixes[2], 60 * 60 * 24),
(suffixes[3], 60 * 60),
(suffixes[4], 60),
(suffixes[5], 1)]
# for each time piece, grab the value and remaining seconds, and add it to
# the time string
for suffix, length in parts:
value = seconds / length
if value > 0:
seconds = seconds % length
time.append('%s%s' % (str(value),
(suffix, (suffix, suffix + 's')[value > 1])[add_s]))
if seconds < 1:
break
return separator.join(time)