decklin/rawdog-contrib
view feedtime @ 13:900be58d356d
Big refactoring. Gather the deltas in one place (no dict, slow), and use a
smarter median after. Scale with sqrt again... in the absence of better info
everything will settle on DEF. Average in the existing interval value to avoid
crazy big adjustments, too.
smarter median after. Scale with sqrt again... in the absence of better info
everything will settle on DEF. Average in the existing interval value to avoid
crazy big adjustments, too.
| author | decklin |
|---|---|
| date | Fri Jun 29 20:16:01 2007 +0000 (2007-06-29) |
| parents | 8f4b9824a87d |
| children |
line source
1 #!/usr/bin/python
3 import sys
4 import math
5 import time
6 import pickle
8 from rawdoglib import rawdog
10 MIN = 60 * 30
11 DEF = 60 * 144
13 def scale(t):
14 return math.sqrt(DEF) * math.sqrt(t)
16 def ok_median(l):
17 mean = float(sum(l)) / len(l)
18 dsq = sum([(x - mean) ** 2 for x in l])
19 stdev = math.sqrt(dsq / len(l))
21 l = [x for x in l if abs(x - mean) <= 3*stdev]
22 l.sort()
24 if len(l) % 2:
25 return l[len(l)//2]
26 else:
27 return (l[len(l)//2-1] + l[len(l)//2]) / 2
29 def delta_dates(state, url):
30 arts = [a for a in state.articles.values() if a.feed == url]
31 dates = list(set([a.date or a.added for a in arts]))
33 if len(dates) == 1:
34 dates.append(time.time())
35 else:
36 dates.sort()
38 return [b-a for a, b in zip(dates, dates[1:]) if b-a > MIN/2]
40 if __name__ == '__main__':
41 state = pickle.load(file('state'))
43 for line in file('config'):
44 tokens = line.split()
46 if tokens and tokens[0] == 'feed' and 't=fix' not in tokens[3:]:
47 intervals = [rawdog.parse_time(tokens[1])]
48 intervals += delta_dates(state, tokens[2])
49 t = scale(ok_median(intervals))
50 print 'feed %dm %s' % (t/60, ' '.join(tokens[2:]))
51 else:
52 print line,
54 def unparse_time(t):
55 units = ((604800, 'w'), (86400, 'd'), (3600, 'h'), (60, 'm'), (1, 's'))
56 s = []
58 for u in units:
59 p, t = divmod(t, u[0])
60 if p or s:
61 s.append('%d%s' % (p, u[1]))
62 if t == 0:
63 break
65 return ''.join(s)
