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
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]]