def tweet_if_appropriate(timestamp, t): l = log_parser.read_log() ld = log_to_dict(l) msg = None if t in ld: msg = "Word count: %s" % ld[t] print "gunna tweet" just_date = datetime(t.year, t.month, t.day) wpd = log_to_dict(log_parser.calc_wpd(l)) if just_date in wpd: print "...with wpd" msg = msg + (", so far today: %s" % wpd[just_date]) possible_img = os.path.join(IMAGE_DIRECTORY, "%s.png" % timestamp) if os.path.isfile(possible_img): print "...and img" msg = msg + (" %s%s.png" % (IMAGE_URL_BASE, timestamp)) else: print "not gunna tweet" if msg: print "tweeting: ", msg send_tweet(msg)
def notify(self, evt): job.Job.notify(self, evt) if isinstance(evt, ReadLogContinueEvent) and evt.getSource() == self: log.debug("Continuing read of file") # Continue to read the log try: self._progress += log_parser.read_log( self._logfile_hndl, self._db, LINEINCR) log.debug("Read %d %% of file (%d / %d)" % (self.getProgress(), self._progress, self._log_size)) except log_parser.EndOfLogException, e: self._progress = self._log_size # Log file is complete, updated the db entry self._mark_complete() # Add an event to notify that the file is complete self._logfile_hndl.close() new_evt = ReadLogCompleteEvent(self) self.getAgent().addEvent(new_evt) except log_parser.InvalidLogException, e: log.warning("Invalid log file: %s" % str(e)) self._logfile_hndl.close() new_evt = ReadLogCompleteEvent(self) self.getAgent().addEvent(new_evt)
def build_chart(): l = log_parser.read_log() if len(l) > 0: # add current time's value, which is the same as the last update l.append((datetime.now(), l[-1][1])) wpd = log_parser.calc_wpd(l) params = {} params['cht'] = 's' params['chs'] = '500x300' params['chtt'] = 'Ian\'s+Dissertation' params['chts'] = '000000,20' params['chxt'] = 'y,r' # get line chart data first = min(l[0][0], wpd[0][0]) first = datetime(first.year, first.month, first.day) # change first to beginning of first day # insert a point to make the chart meet up correctly at the 0 x = [0] y = [l[0][1]] for i in l: x.append(time_delta_to_minutes(i[0]-first)) y.append(i[1]) # make the chart (but not the graph) go through the end of the current day (so last bar fits on) n = datetime.now() last = datetime(n.year, n.month, n.day) + ONE_DAY # get wpd chart data dx = [] dy = [] for i in wpd: dx.append(time_delta_to_minutes((i[0]+HALF_DAY)-first)) dy.append(i[1]) # scale everything to 100 xmin = 0 xmax = time_delta_to_minutes(last-first) ymin = min(y) ymax = max(y) dymin = min(dy) dymax = max(dy) yrange = ymax-ymin addition = 0.1*yrange ymin = ymin - addition ymax = ymax + addition dyrange = dymax-dymin addition = 0.1*dyrange dymin = dymin - addition dymax = dymax + addition xscale = 100.0/(xmax-xmin) yscale = 100.0/(ymax-ymin) dyscale = 100.0/(dymax-dymin) for i in range(len(x)): x[i] = (x[i]-xmin) * xscale y[i] = (y[i]-ymin) * yscale for i in range(len(dx)): dx[i] = (dx[i]-xmin) * xscale dy[i] = (dy[i]-dymin) * dyscale dyzero = (0-dymin) * dyscale # double up the wpd points to make bars a = [] b = [] for i in range(len(dx)): a.append(dx[i]) a.append(dx[i]) b.append(dyzero) b.append(dy[i]) a.extend(x) b.extend(y) xpoints = ",".join(map(one_decimal,a)) ypoints = ",".join(map(one_decimal,b)) params['chd'] = 't:' + xpoints + '|' + ypoints # make the stupid bar marks barmarks = [] barwidth = max((440 / (last-first).days) - 2, 1) for i in range(len(dx)): barmarks.append('D,A0BAE9,0,'+ str(i*2) + ':' + str(i*2+1) +',' + str(barwidth) + ',1') params['chm'] = 'o,000000,0,-1,0|' + '|'.join(barmarks) + '|H,A0BAE9,0,0,1|D,ff9900,0,' + str(len(dx)*2) + ':' + str(len(a)-1) + ',2,1' # figure out axes params['chxr'] = '0,' + str(ymin) + ',' + str(ymax) + '|1,' + str(dymin) + ',' + str(dymax) # output it q = [] for k in params.keys(): q.append(k + '=' + str(params[k])) return "&".join(q)