From 97d75bf32aab9f32668c3d226fd2becfee24c1f9 Mon Sep 17 00:00:00 2001 From: Vincent Le Gallic Date: Mon, 8 Apr 2013 04:00:57 +0200 Subject: [PATCH] today xantah --- today.py | 90 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 49 insertions(+), 41 deletions(-) diff --git a/today.py b/today.py index 477fdfa..a949652 100755 --- a/today.py +++ b/today.py @@ -17,10 +17,10 @@ class Config(object): """Configuration (pas de couleurs si on n'output pas dans un terminal""" def __init__(self, color=True): if color: - self.endcolor = "" - self.blue = "" - self.red = "" - self.green = "" + self.endcolor = u"" + self.blue = u"" + self.red = u"" + self.green = u"" else: self.endcolor=self.blue=self.red=self.green="" #: Serveur distant où aller récupérer les checks @@ -60,7 +60,7 @@ def print_date(timestamp=None,color=True): def add_title(titre, texte): """Ajoute un titre à la partie si il y a quelque chose dedans""" if texte: - texte = " %s%s :%s\n" % (config.green, titre, config.endcolor) + texte + "\n" + texte = u" %s%s :%s\n" % (config.green, titre, config.endcolor) + texte + "\n" return texte def get_now(): @@ -90,7 +90,7 @@ def parse_datefile(namefile): data = [] for l in rawdata: date, truc = l.split("\t",1) - date = datetime.datetime(*time.strptime(date,"%d/%m/%Y")[:7]) + date = datetime.datetime(*time.strptime(date, "%d/%m/%Y")[:7]) data.append([date, truc]) return data @@ -105,8 +105,8 @@ def get_timers(): if delta > datetime.timedelta(0): timers.append([event,delta.days]) eventsize = max([0]+[len(l[0]) for l in timers]) - timers = "\n".join([("%%-%ss J-%%s" % eventsize) % (event, timer) for event,timer in timers]) - timers = add_title("Timers", timers) + timers = u"\n".join([(u"%%-%ss J-%%s" % eventsize) % (event, timer) for event,timer in timers]) + timers = add_title(u"Timers", timers) return timers def exists(l): @@ -130,13 +130,13 @@ def get_birthdays(*search): if delta > datetime.timedelta(0): age = now.year - date.year if abs(delta) < datetime.timedelta(1): - birthdays.append([config.red,"%s a %s ans AUJOURD'HUI !"%(nom,age),0,config.endcolor]) + birthdays.append([config.red, u"%s a %s ans AUJOURD'HUI !" % (nom, age), 0, config.endcolor]) elif delta < datetime.timedelta(7): - birthdays.append([config.red,"%s va avoir %s ans"%(nom,age),-delta.days,config.endcolor]) + birthdays.append([config.red, u"%s va avoir %s ans" % (nom, age), -delta.days, config.endcolor]) elif delta < datetime.timedelta(14): - birthdays.append(["","%s va avoir %s ans"%(nom,age),-delta.days,""]) + birthdays.append([u"", u"%s va avoir %s ans" % (nom, age), -delta.days, u""]) elif datetime.timedelta(30-4) < delta < datetime.timedelta(30+4): - birthdays.append(["","%s va avoir %s ans"%(nom,age),-delta.days,""]) + birthdays.append([u"", u"%s va avoir %s ans" % (nom, age), -delta.days, u""]) else: # Recherche spécifique search = [i.lower() for i in search] @@ -147,28 +147,28 @@ def get_birthdays(*search): delta = thisyeardate - now + datetime.timedelta(1) age = now.year - date.year if delta.days<0: - birthdays.append(["", "%s a eu %s ans"%(nom,age), -delta.days, ""]) + birthdays.append([u"", u"%s a eu %s ans" % (nom, age), -delta.days, ""]) else: - birthdays.append(["", "%s va avoir %s ans"%(nom,age), -delta.days, ""]) + birthdays.append([u"", u"%s va avoir %s ans" % (nom, age), -delta.days, ""]) birthdays.sort(lambda x,y: -cmp(x[2], y[2])) eventsize = max([0]+[len(l[1]) for l in birthdays]) - template = ("%%s%%-%ss J%%+d%%s" % eventsize) - birthdays = "\n".join([template % tuple(tup) for tup in birthdays]) - birthdays = add_title("Anniversaires", birthdays) + template = (u"%%s%%-%ss J%%+d%%s" % eventsize) + birthdays = u"\n".join([template % tuple(tup) for tup in birthdays]) + birthdays = add_title(u"Anniversaires", birthdays) return birthdays def check_birthdays(): """Compte combien il y a d'anniversaires à afficher""" birthdays = get_birthdays() if birthdays: - n = birthdays.count("\n") - 1 + n = birthdays.count(u"\n") - 1 else: n = 0 return n def format_quotes(liste): """Formate les quotes de dicos à texte""" - t = ("\n" + "_"*80 + "\n").join(["%(id)s (%(date)s)\n%(quote)s" % q for q in liste]) + t = (u"\n" + u"_"*80 + u"\n").join([u"%(id)s (%(date)s)\n%(quote)s" % q for q in liste]) return t def get_dtc(*args): @@ -195,6 +195,16 @@ def get_dtc(*args): def update_xkcd(newid): update_last_seen({"xkcd" : int(newid)}) +def update_xantah(newid): + update_last_seen({"xantah" : int(newid)}) + +THINGS = { + "dtc" : u"Quotes DTC", + "xkcd" : u"Épisodes de XKCD", + "xantah" : u"Épisodes de La Légende de Xantah", + + "birthdays" : u"Anniversaires à souhaiter", + } def check_all(): """Vérifie si il y a des derniers trucs non lus/vus.""" @@ -203,26 +213,22 @@ def check_all(): out, err = proc.communicate() news = json.loads(out) seen = get_last_seen() - n_birth = check_birthdays() - n_dtc = news["dtc"] - seen.get("dtc", 0) - n_xkcd = news["xkcd"] - seen.get("xkcd", 0) - l = [["Anniversaires", n_birth], - ["Quotes DTC", n_dtc], - ["XKCD non lus", n_xkcd]] + news["birthdays"] = check_birthdays() checks = [] - for (text, n) in l: + for (thing, comm) in THINGS.iteritems(): + n = news[thing] - seen.get(thing, 0) if type(n) != int: print n elif n > 0: - checks.append("%s : %s" % (text, n)) - checks = "\n".join(checks) - checks = add_title("Checks", checks) + checks.append("%s : %s" % (comm, n)) + checks = u"\n".join(checks) + checks = add_title(u"Checks", checks) return checks def get_everything(): """Récupère toutes les infos""" work = [action() for action in AUTOMATED_ACTIONS.values()] - chain = "\n\n".join([result for result in work if result]) + chain = u"\n\n".join([result for result in work if result]) return chain def _is_there_something_in_today(): @@ -261,12 +267,12 @@ def ping(): something = get_everything() _there_is_something_in_today(something) if something: - return "You have something in %stoday%s" % (config.red, config.endcolor) + return u"You have something in %stoday%s" % (config.red, config.endcolor) else: - return "Nothing in today" + return u"Nothing in today" else: if _is_there_something_in_today(): - return "You have something in %stoday%s" % (config.red, config.endcolor) + return u"You have something in %stoday%s" % (config.red, config.endcolor) def affiche(): """Action par défaut, affiche toutes les infos""" @@ -279,17 +285,17 @@ def initialize(): """Crée les fichiers (vides) nécessaires au fonctionnement du script""" files = [config.birthdays_file, config.timers_file, config.saints_file, config.last_seen_file] - contents = ["# Anniversaires\n#jj/mm/aaaa Prénom Nom\n", - "# Évènements à venir\n#jj/mm/aaaa Évènement\n", - "# Fêtes\n#jj/mm/aaaa Saint\n", - "{}"] + contents = [u"# Anniversaires\n#jj/mm/aaaa Prénom Nom\n", + u"# Évènements à venir\n#jj/mm/aaaa Évènement\n", + u"# Fêtes\n#jj/mm/aaaa Saint\n", + u"{}"] for ifile in range(len(files)): namefile = files[ifile] if os.path.isfile(namefile): print "%s exists, skipping." % (namefile,) else: f = open(namefile, "w") - f.write(contents[ifile]) + f.write(contents[ifile].encode("utf-8")) f.close() def sync(): @@ -306,7 +312,7 @@ def sync(): out = proc.stdout.read() newdict = json.loads(out) update_last_seen(newdict) - print "Nouvel état : %r" % newdict + print u"Nouvel état : %r" % newdict @@ -320,6 +326,8 @@ AUTOMATED_ACTIONS = { #: Les actions qu'on peut effectuer en rajoutant des paramètres OTHER_ACTIONS = { "xkcd" : update_xkcd, + "xantah" : update_xantah, + "dtc" : get_dtc, "ping" : ping, "show" : affiche, @@ -337,7 +345,7 @@ ACTIONS[None] = affiche # action par défaut if __name__ == "__main__": import sys if "--no-color" in sys.argv: - config.endcolor, config.red, config.blue = "","","" + config.endcolor, config.red, config.blue = u"", u"", u"" if len(sys.argv) == 1: # Juste un today output = ACTIONS[None]() @@ -346,4 +354,4 @@ if __name__ == "__main__": args = sys.argv[2:] output = ACTIONS[commande](*args) if output: - print output.encode("utf-8") + print output -- 2.39.2