From 4d26da8768fcbbc0425cd2139e48c98796521491 Mon Sep 17 00:00:00 2001 From: boyska Date: Sat, 17 Dec 2016 19:51:27 +0100 Subject: [PATCH] parsing date --- messagenet.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/messagenet.py b/messagenet.py index 5cf661d..59b0079 100644 --- a/messagenet.py +++ b/messagenet.py @@ -1,4 +1,6 @@ import os +from datetime import datetime, date, timedelta +import logging from splinter import Browser @@ -73,14 +75,32 @@ class Messagenet(object): ok = r.has_class('statusOK') cells = r.find_by_tag('td') call_type_desc = cells[0].value - date = cells[1].value - # avanzato: prendi la data {{{2 + human_date = cells[1].value + # avanzato: parsa la data {{{2 + try: + parsed_date = datetime.strptime(human_date, '%Y-%m-%d, %H.%M.%S') + except ValueError as exc: + print 'parsing date', exc + if human_date.startswith('Oggi, '): + d = date.today() + t = datetime.strptime(human_date.split(',', 1)[1].strip(), + '%H.%M.%S').time() + parsed_date = datetime.combine(d, t) + elif human_date.startswith('Ieri, '): + d = date.today() - timedelta(days=1) + t = datetime.strptime(human_date.split(',', 1)[1].strip(), + '%H.%M.%S').time() + parsed_date = datetime.combine(d, t) + else: + logging.error("Could not parse date '%s'" % human_date) + parsed_date = None # }}} call_from = cells[2].value deviata = bool(cells[3].value) call_to = cells[4].value duration = cells[5].value - yield {'status': (ok, call_type_desc), 'date': date, + yield {'status': (ok, call_type_desc), + 'date': human_date, 'datetime': parsed_date, 'deviata': deviata, 'from': call_from, 'to': call_to, 'duration': duration}