]> gitweb.pimeys.fr Git - today.git/commitdiff
On récupère tous les check sur le serveur distant et on stocke en local les last_read
authorVincent Le Gallic <legallic@crans.org>
Mon, 8 Apr 2013 00:16:20 +0000 (02:16 +0200)
committerVincent Le Gallic <legallic@crans.org>
Mon, 8 Apr 2013 00:16:20 +0000 (02:16 +0200)
today.py

index be2c99ced41786d02de202a9725d98091a20a0c2..9e0fbc211bdbad6e809369df0e5d3cc467c07c21 100755 (executable)
--- 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]]