summaryrefslogtreecommitdiff
path: root/sublab_project/sublab_monitor
diff options
context:
space:
mode:
Diffstat (limited to 'sublab_project/sublab_monitor')
-rw-r--r--sublab_project/sublab_monitor/tasks.py33
1 files changed, 29 insertions, 4 deletions
diff --git a/sublab_project/sublab_monitor/tasks.py b/sublab_project/sublab_monitor/tasks.py
index 681ce5f..982b0a3 100644
--- a/sublab_project/sublab_monitor/tasks.py
+++ b/sublab_project/sublab_monitor/tasks.py
@@ -4,10 +4,12 @@ from datetime import timedelta
from celery.task import PeriodicTask
import subprocess
import urllib2
+import urlparse
from PIL import Image, ImageOps
from StringIO import StringIO
from django.conf import settings
import os
+import re
import sublab_monitor
@@ -73,6 +75,13 @@ class ImageFetcher(object):
"""
raise NotImplementedError
+ def replace_file(self, filename, save):
+ fn = os.path.join(settings.MEDIA_ROOT, filename)
+ fn_new = os.path.join(settings.MEDIA_ROOT, 'new-%s' % filename)
+
+ save(fn_new)
+ os.rename(fn_new ,fn)
+
def run(self, **kwargs):
upstream = urllib2.urlopen(self.fetch_url).read()
image = Image.open(StringIO(upstream))
@@ -82,12 +91,16 @@ class ImageFetcher(object):
except NotImplementedError:
pass
- fn = os.path.join(settings.MEDIA_ROOT, self.store_name)
- fn_new = os.path.join(settings.MEDIA_ROOT, 'new-%s' % self.store_name)
+ self.replace_file(self.store_name, image.save)
+
+ if image.mode.upper() == 'RGBA':
+ image = Image.composite(image, Image.new("RGB", image.size, (255,255,255)), image)
+ image.convert("RGB")
- image.save(fn_new)
- os.rename(fn_new, fn)
+ image.thumbnail((180, 180), Image.ANTIALIAS)
+ self.replace_file('thumb_' + self.store_name, image.save)
+ return 'Done.'
class EnhancingImageFetcher(ImageFetcher):
def process_image(self, image):
@@ -107,3 +120,15 @@ class TempGraphFetcher(ImageFetcher, PeriodicTask):
run_every = timedelta(minutes=5)
fetch_url = 'http://taifun.local.sublab.org/temperature/temp-2hour.png'
store_name = 'tempgraph.png'
+
+class ReeknerSprookTempFetcher(ImageFetcher, PeriodicTask):
+ run_every = timedelta(minutes=5)
+ store_name = 'reeknersprook.png'
+
+ @property
+ def fetch_url(self):
+ url = 'http://reeknersprook.de/sublog/graph-small'
+ buf = urllib2.urlopen(url).read()
+ rel = re.search(r'src="(.*?)"', buf).group(1)
+
+ return urlparse.urljoin(url, rel)