From dc6c0ac3a1ac2631324461590c79bac47e5e25f3 Mon Sep 17 00:00:00 2001 From: Vincent Le Gallic Date: Mon, 8 Apr 2013 02:16:20 +0200 Subject: [PATCH] =?utf8?q?On=20r=C3=A9cup=C3=A8re=20tous=20les=20check=20s?= =?utf8?q?ur=20le=20serveur=20distant=20et=20on=20stocke=20en=20local=20le?= =?utf8?q?s=20last=5Fread?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- today.py | 54 +++++++++++++++++++++++++----------------------------- 1 file changed, 25 insertions(+), 29 deletions(-) diff --git a/today.py b/today.py index be2c99c..9e0fbc2 100755 --- a/today.py +++ b/today.py @@ -55,14 +55,14 @@ def get_now(): now = datetime.datetime(*time.localtime(timestamp)[:7]) return now -def get_lasts(): +def get_last_seen(): """Récupère la liste des derniers trucs vus/lus""" with open("lasts") as f: return json.loads(f.read()) def update_lasts(what, value): """Met à jour un des derniers trucs vus/lus""" - lasts = get_lasts() + lasts = get_last_seen() if not what in lasts.keys(): print """%r n'est pas un "truc vu/lu" valide""" % (what,) return @@ -150,50 +150,46 @@ def check_birthdays(): 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]) + return t + def get_dtc(*args): """Récupère les quotes DTC non lues""" if len(args) == 0: - cmd = "~/bin/dtc last" + last_dtc = get_last_seen()["dtc"] + cmd = "~/bin/dtc %s + --json" % (last_dtc+1,) elif len(args) == 1: - cmd = "~/bin/dtc %s" % args[0] + cmd = "~/bin/dtc %s --json" % args[0] elif len(args) == 2: - cmd = "~/bin/dtc %s %s" % (args[0], args[1]) + cmd = "~/bin/dtc %s %s --json" % (args[0], args[1]) else: return None proc = subprocess.Popen(["ssh", "pimeys", cmd], stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = proc.communicate() out += err - return out.decode("UTF-8") - -def check_dtc(): - """Vérifie si il y a des quotes DTC non lues""" - cmd = "~/bin/dtc check" - proc = subprocess.Popen(["ssh", "pimeys", cmd], stdout=subprocess.PIPE, stderr=subprocess.PIPE) - out, err = proc.communicate() - out += err - if err: - return err - n = int(out) - return n - -def check_xkcd(): - last_read = get_lasts()["xkcd"] - try: - p = urllib.urlopen("http://xkcd.com") - except IOError: - return "Impossible de se connecter à xkcd" - t = p.read() - current_id = int(re.findall("Permanent link to this comic: http://xkcd.com/(.*?)/", t)[0]) - return current_id - last_read + quotes = json.loads(out) + if quotes: + last_id = max([q["id"] for q in quotes]) + update_lasts("dtc", last_id) + textquotes = format_quotes(quotes) + return textquotes def update_xkcd(newid): update_lasts("xkcd", int(newid)) def check_all(): + """Vérifie si il y a des derniers trucs non lus/vus.""" + cmd = "/home/vincent/scripts/today/today-server.py whatsup" + proc = subprocess.Popen(["ssh", "pimeys", cmd], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + out, err = proc.communicate() + news = json.loads(out) + seen = get_last_seen() n_birth = check_birthdays() - n_dtc = check_dtc() - n_xkcd = check_xkcd() + n_dtc = news["dtc"] - seen["dtc"] + n_xkcd = news["xkcd"] - seen["xkcd"] l = [["Anniversaires", n_birth], ["Quotes DTC", n_dtc], ["XKCD non lus", n_xkcd]] -- 2.39.2