Permalink
Browse files

Add support for Zhejiang PM2.5

  • Loading branch information...
tualatrix committed Mar 25, 2012
1 parent 66a533d commit bd7d08d9f7c324288ddb362729d1c614c8247ea1
Showing with 71 additions and 0 deletions.
  1. +1 −0 imtx/urls.py
  2. +70 −0 imtx/views.py
View
@@ -33,6 +33,7 @@ def lastmod(self, obj):
urlpatterns = patterns('',
(r'^sitemap.xml$', cache_page(sitemap_views.sitemap, 60 * 60 * 6), {'sitemaps': sitemaps}),
+ (r'^zhejiangpm25$', 'views.zhejiangpm25'),
(r'^admin/', include(admin.site.urls)),
url(r'^feed/(?P<url>.*)/$', 'django.contrib.syndication.views.feed',
{'feed_dict': feed}, name='feed'),
View
@@ -0,0 +1,70 @@
+#coding: utf-8
+import urllib
+
+from django.http import HttpResponse
+from django.utils import simplejson
+
+from BeautifulSoup import BeautifulSoup
+
+def linear(aqi_high, aqi_low, conc_high, conc_low, concentration):
+ a = 0
+ conc = float(concentration)
+ a = ((conc - conc_low) / (conc_high - conc_low)) * (aqi_high - aqi_low) + aqi_low
+ return int(round(a))
+
+def aqi_pm25(concentration):
+ conc = float(concentration)
+ c = int(round(10*conc)) / 10
+
+ if c >= 0 and c < 15.5:
+ return linear(50, 0, 15.4, 0, c)
+ elif c >= 15.5 and c < 35.5:
+ return linear(100, 51, 35.4, 15.5, c)
+ elif c >= 35.5 and c < 65.5:
+ return linear(150, 101, 65.4, 35.5, c)
+ elif c >= 65.5 and c < 150.5:
+ return linear(200, 151, 150.4, 65.5, c)
+ elif c >= 150.5 and c < 250.5:
+ return linear(300, 201, 250.4, 150.5, c)
+ elif c >= 250.5 and c < 350.5:
+ return linear(400, 301, 350.4, 250.5, c)
+ elif c >= 350.5 and c < 500.5:
+ return linear(500,401,500.4,350.5, c)
+ else:
+ return "Out of Range"
+
+def aqi_category(aqi):
+ if aqi <= 50:
+ return u"优良"
+ elif aqi > 50 and aqi <= 100:
+ return u"中等"
+ elif aqi > 100 and aqi <= 150:
+ return u"敏感群体有害"
+ elif aqi > 150 and aqi <= 200:
+ return u"不健康"
+ elif aqi > 200 and aqi <= 300:
+ return u"非常不健康"
+ elif aqi > 300 and aqi <= 400:
+ return u"有毒害一级"
+ elif aqi > 400 and aqi <= 500:
+ return u"有毒害二级"
+ else:
+ return u"尼玛!PM2.5爆表啦!"
+
+def zhejiangpm25(request):
+ data = {}
+ content = urllib.urlopen('http://app.zjepb.gov.cn:8080/wasdemo/search?channelid=121215').read()
+
+ soup = BeautifulSoup(content)
+ for td in soup.findAll(height='23'):
+ tr = td.parent
+
+ city = tr.findChild().text
+ concentration = int(tr.findChildren()[3].text)
+ aqi = aqi_pm25(concentration)
+ category = aqi_category(aqi_pm25(aqi))
+ data[city] = {'concentration': concentration,
+ 'aqi': aqi,
+ 'category': category}
+ response = simplejson.dumps(data)
+ return HttpResponse(response, mimetype="application/json")

0 comments on commit bd7d08d

Please sign in to comment.