Skip to content
This repository has been archived by the owner on Jan 28, 2018. It is now read-only.

Commit

Permalink
Setting up ping charts
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelm committed Apr 23, 2012
1 parent 728b088 commit 2d66ddd
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 1 deletion.
30 changes: 29 additions & 1 deletion generate.py
Expand Up @@ -45,7 +45,6 @@ def lastfm(self, download = True):
USER = "rami95"
FILE = "data/%s.lastfm.sqlite.db" % USER
FILEPREFIX = "charts/%s.lastfm.chart." % USER
HTMLFILE = "charts/music.lastfm.%s.html" % USER
APIK = "b8258575158335e66482df0777e5b331"
OFFSET = time.mktime(datetime.datetime(2010,01,01).timetuple())
if download:
Expand All @@ -54,6 +53,30 @@ def lastfm(self, download = True):
self.charts.append((chart, "Music (Last.fm): rami95"))
print "Music done."

def ping(self, download = True):
print "Ping"
import lml.ping
KEYHASH = "0e0f17629059a77b7d4b0a6d95845e5b7a1c4490" # SHA1 hash of my "ping" key
SERVER = "http://www.raphaelmichel.de/stats/pingserver/data/"

DEVICE = "PC"
FILE = "data/%s.ping.raw.txt" % DEVICE
FILEPREFIX = "charts/%s.ping.chart." % DEVICE
if download:
lml.ping.Download(FILE, KEYHASH, DEVICE).download()
chart = lml.ping.Charts(FILE, FILEPREFIX, DEVICE).create()
self.charts.append((chart, "Online time: desktop computer"))

DEVICE = "android"
FILE = "data/%s.ping.raw.txt" % DEVICE
FILEPREFIX = "charts/%s.ping.chart." % DEVICE
if download:
lml.ping.Download(FILE, KEYHASH, DEVICE).download()
chart = lml.ping.Charts(FILE, FILEPREFIX, DEVICE).create()
self.charts.append((chart, "Online time: smartphone"))

print "Ping done."

def main():
parser = OptionParser()
parser.add_option("-m", "--no-mail",
Expand All @@ -65,6 +88,9 @@ def main():
parser.add_option("-l", "--no-music",
action="store_false", dest="lastfm", default=True,
help="Don't scan last.fm")
parser.add_option("-p", "--no-ping",
action="store_false", dest="ping", default=True,
help="Don't scan ping data")
parser.add_option("-d", "--no-downloads",
action="store_false", dest="dl", default=True,
help="Don't use data we not already have")
Expand All @@ -78,6 +104,8 @@ def main():
g.twitter(options.dl)
if options.lastfm:
g.lastfm(options.dl)
if options.ping:
g.ping(options.dl)

g.create_simple_html_index("charts/index.html")

Expand Down
94 changes: 94 additions & 0 deletions lml/ping.py
@@ -0,0 +1,94 @@
import time
import os
import urllib
from datetime import date, datetime, timedelta

import matplotlib.cm as cm
import matplotlib.pyplot as plt
import matplotlib.dates as md

import base
from utils import daterange

class Download(base.Download):
def __init__(self, FILE, HASH, DEVICE):
self.FILE = FILE
self.HASH = HASH
self.DEVICE = DEVICE

def download(self):
f = urllib.urlopen("http://www.raphaelmichel.de/stats/pingserver/data/pings.%s.%s.txt" % (self.HASH, self.DEVICE))
f2 = open(self.FILE, 'w')
f2.write(f.read())
f.close()
f2.close()

class Charts(base.Charts):

def chart_date_time(self):
x_total = []
y_total = []

f = open(self.FILE)
for row in f:
ts = int(row.strip()) - time.timezone
x = date.fromtimestamp(ts)
y = (ts % (3600*24))/3600
x_total.append(x)
y_total.append(y)

# Pings Scatter
plt.clf()
ax = plt.subplot(111)
x_total = md.date2num(x_total)
plt.scatter(x_total, y_total, s=5, marker=(0,3,0), linewidths=0)
labels = ax.get_xticklabels()
plt.setp(labels, rotation=30, fontsize=10)
min_t = min(x_total)
max_t = max(x_total)
plt.axis([min_t, max_t, 0, 24])
plt.title("Pings")
ax.xaxis_date()
plt.yticks(range(0, 25))
ax.xaxis.set_major_formatter( md.DateFormatter('%m/%Y') )
ax.set_ylabel("time of day")
plt.savefig(self.FILEPREFIX+"times.png")
self.charts.append(self.FILEPREFIX+"times.png")

# Pings Histogram
plt.clf()
ax = plt.subplot(111)
plt.hist(y_total, bins=range(0,25))
plt.xlim(0,24)
plt.title("Pings distribution")
plt.xticks(range(0, 25))
ax.set_xlabel("time of day")
plt.savefig(self.FILEPREFIX+"times.hist.png")
self.charts.append(self.FILEPREFIX+"times.hist.png")

def create_simple_html(self):
html = "<html><head>"
html += "<title>Online statistics for %s</title>" % self.DEVICE
html += "</head><body><h1>Online statistics for %s</h1><a href='./'>Overview</a><br />" % self.DEVICE
for c in self.charts:
html += "<img src='%s' /><br />" % os.path.join((os.path.relpath(os.path.dirname(c), os.path.dirname(self.FILEPREFIX))), os.path.basename(c))
html += "generated %s" % date.today().isoformat()
html += "</body></html>"
f = open(self.FILEPREFIX+"all.html", "w")
f.write(html)
f.close()
return self.FILEPREFIX+"all.html"

def create(self):
self.charts = []
self.chart_date_time()
return self.create_simple_html()

def __init__(self, FILE, FILEPREFIX, DEVICE):
self.FILEPREFIX = FILEPREFIX
self.FILE = FILE
self.DEVICE = DEVICE

if not os.path.exists(FILE):
print "Data not found!"
exit()

0 comments on commit 2d66ddd

Please sign in to comment.