Ejemplo n.º 1
0
    def get(self):
        logging.info("scanning twitter alerts")

        q = ParticulateData.all()
        q.filter("date", date.today())
        results = q.fetch(1000)

        for result in results:
            # Check if there are outstanding alerts

            q2 = TwitterAlert.all()
            q2.filter("stationid", result.stationid)
            alerts = q2.fetch(1000)

            for alert in alerts:
                logging.info("result value %d alert on %d", result.value, alert.amount)

                if result.value > alert.amount:
                    # Send Tweet
                    targetuser = alert.twittername

                    logging.info("tweet to %s", targetuser)

                    updateString = (
                        "@%s De waarde voor station %s is morgen te hoog, nl: %d, zie: http://www.vervuilingsalarm.nl/station/%s/"
                        % (targetuser, result.stationid, result.value, result.stationid)
                    )

                    twitterQueue = taskqueue.Queue("twitterqueue")
                    twitterTask = taskqueue.Task(url="/sendtweets/worker/", params={"update": updateString})
                    twitterQueue.add(twitterTask)
Ejemplo n.º 2
0
    def get(self, stationid):
        response = {}

        q = ParticulateData.all()
        q.filter("stationid =", stationid)
        q.filter("date > ", date.today() - timedelta(90))
        q.order("date")
        results = q.fetch(1000)

        data = []
        for result in results:
            data.append(result.value)

        logging.info("data got for station %s" % stationid)

        response["data"] = data

        # Pachube is not very happy if we hammer them on every index load
        environmentXML = memcache.get("%s_environmentxml" % stationid)

        if environmentXML is None:
            mappingQuery = PachubeMapping.all()
            mappingQuery.filter("stationid =", stationid)
            mapping = None

            if mappingQuery.count():
                mapping = mappingQuery[0]

                environmentURL = "http://www.pachube.com/api/%s.xml?key=%s" % (mapping.pachubeid, PACHUBE_API_KEY)
                logging.info("fetching url: %s", environmentURL)

                environmentGet = urlfetch.fetch(url=environmentURL)

                if environmentGet.status_code == 200 and environmentGet.content:
                    environmentXML = environmentGet.content
                    memcache.set("%s_environmentxml" % stationid, environmentGet.content, time=7 * 24 * 60 * 60)

        lat = ""
        lon = ""

        logging.debug("environment xml is - %s", environmentXML)

        if environmentXML:
            lat = re.search("<lat>(.+?)</lat>", environmentXML).group(1)
            lon = re.search("<lon>(.+?)</lon>", environmentXML).group(1)

            logging.info("got lon %s and lat %s", lon, lat)

        response["lat"] = lat
        response["lon"] = lon

        self.response.headers["Content-Type"] = "text/json"

        self.response.out.write(simplejson.dumps(response))
Ejemplo n.º 3
0
    def get(self, stationid):
        q = ParticulateData.all()
        q.filter("stationid =", stationid)
        q.filter("date > ", date.today() - timedelta(90))
        q.order("date")
        results = q.fetch(1000)

        mappingQuery = PachubeMapping.all()
        mappingQuery.filter("stationid =", stationid)
        mapping = None

        if mappingQuery.count():
            mapping = mappingQuery[0]

        values = {"values": results, "station": stationid, "mapping": mapping}

        # Add the flash if we got it
        if self.request.get("flash", ""):
            values["flash"] = self.request.get("flash")

        self.response.headers["Content-Type"] = "text/html"

        templatepath = os.path.join(os.path.dirname(__file__), "templates", "station.html")
        self.response.out.write(template.render(templatepath, values))