def getTransmissionsByMonth(self, stationId, year, month, limiter = None, notModifiedCheck = None, head = False): if month is not None: startTimestamp = datetime(year, month, 1) if month != 12: endTimestamp = datetime(year, month+1, 1) else: endTimestamp = datetime(year+1, 1, 1) else: startTimestamp = datetime(year, 1, 1) endTimestamp = datetime(year+1, 1, 1) startTimestamp = TimeUtils.toTimestamp(startTimestamp) endTimestamp = TimeUtils.toTimestamp(endTimestamp) transmissions = self.store.find((Transmission, Broadcast), Transmission.BroadcastID == Broadcast.ID, And(Broadcast.StationID == stationId, And(Transmission.Timestamp >= startTimestamp, Transmission.Timestamp < endTimestamp))) lastModified = max( transmissions.max(Transmission.Modified), self.store.find(Broadcast, Broadcast.StationID == stationId).max(Broadcast.TransmissionRemoved), self.store.get(Station, stationId).BroadcastRemoved ) if head: return (lastModified, None) if notModifiedCheck is not None: notModifiedCheck(lastModified) transmissions.order_by(Desc(Transmission.Timestamp)) if limiter is not None: transmissions = limiter(transmissions) return (lastModified, (transmission for (transmission, broadcast) in transmissions))
def getUpcomingBroadcasts(self, station, all, update, timeLimit, maxTimeRange, limiter = None, notModifiedCheck = None, head = False): now = TimeUtils.now() where = And(Or(Broadcast.BroadcastEnd > now, Broadcast.BroadcastEnd == None), (Broadcast.BroadcastStart < (now + timeLimit))) if not all: where = And(where, Broadcast.Type == u"data") if station is not None: where = And(where, Broadcast.StationID == stationId) broadcasts = self.store.find(Broadcast, where) lastModified = max( broadcasts.max(Broadcast.Modified), station.BroadcastRemoved if station is not None else None, station.Schedule.Modified if (station is not None and station.Schedule is not None) else None ) if head: return (lastModified, None, None, None) if notModifiedCheck is not None: notModifiedCheck(lastModified) if update: untilDate = datetime.fromtimestamp(now) untilDate += timedelta(seconds=timeLimit) untilDate = self.normalizeDate(untilDate) until = TimeUtils.toTimestamp(untilDate) if station is None: validUntil = self.scheduleMaintainer.updateSchedules(until, maxTimeRange) else: validUntil = self.scheduleMaintainer.updateSchedule(station, until, maxTimeRange) # trans.set_header_value("Expires", self.model.formatHTTPTimestamp(validUntil)) return (lastModified, broadcasts if limiter is None else limiter(broadcasts), validUntil >= until, validUntil)
def getData(self, station = None, years=None, **kwargs): where = Broadcast.BroadcastEnd != None if station is not None: where = And(where, Broadcast.StationID == station.ID) if years is not None: now = TimeUtils.nowDate() where = And(where, Broadcast.BroadcastStart >= TimeUtils.toTimestamp(datetime(year=now.year-years, month=now.month, day=1))) data = self.store.find( ( Station, Func("YEAR", Func("FROM_UNIXTIME", Broadcast.BroadcastStart)), Func("MONTH", Func("FROM_UNIXTIME", Broadcast.BroadcastStart)), Func("SUM", Broadcast.BroadcastEnd - Broadcast.BroadcastStart) ), Broadcast.StationID == Station.ID, where ).group_by(Station, Func("YEAR", Func("FROM_UNIXTIME", Broadcast.BroadcastStart)), Func("MONTH", Func("FROM_UNIXTIME", Broadcast.BroadcastStart))) data = list(self._splitData(data)) if len(data) == 0: raise NoDataArgError(kwargs, station=station, years=years) monthList = list(self._getMonthList(data)) data = list(self._mapData(data, monthList)) data.sort(key=lambda x: unicode(x[0])) dateList = [datetime(year, month+1, day=1, hour=0, minute=0) for year, month in monthList] xticks = list(self._filterTicksExt(dateList, monthList)) tickCoords = [float(i)+0.5 for i in self._filterTicksExt(range(len(monthList)), monthList)] coords = [i for i in xrange(len(monthList))] return (data, coords, xticks, tickCoords)
def priyom_timestamp(s): if allowNone and (((type(s) == str or type(s) == unicode) and (len(s) == 0 or s.lower() == "none")) or s is None): return None if type(s) == int or type(s) == float: if asDate: return TimeUtils.fromTimestamp(s) else: return s if asDate: return datetime.strptime(s, Formatting.priyomdate) else: return TimeUtils.toTimestamp(datetime.strptime(s, Formatting.priyomdate))
def getTransmissionsByMonth(self, stationId, year, month, limiter=None, notModifiedCheck=None, head=False): if month is not None: startTimestamp = datetime(year, month, 1) if month != 12: endTimestamp = datetime(year, month + 1, 1) else: endTimestamp = datetime(year + 1, 1, 1) else: startTimestamp = datetime(year, 1, 1) endTimestamp = datetime(year + 1, 1, 1) startTimestamp = TimeUtils.toTimestamp(startTimestamp) endTimestamp = TimeUtils.toTimestamp(endTimestamp) transmissions = self.store.find( (Transmission, Broadcast), Transmission.BroadcastID == Broadcast.ID, And( Broadcast.StationID == stationId, And(Transmission.Timestamp >= startTimestamp, Transmission.Timestamp < endTimestamp))) lastModified = max( transmissions.max(Transmission.Modified), self.store.find(Broadcast, Broadcast.StationID == stationId).max( Broadcast.TransmissionRemoved), self.store.get(Station, stationId).BroadcastRemoved) if head: return (lastModified, None) if notModifiedCheck is not None: notModifiedCheck(lastModified) transmissions.order_by(Desc(Transmission.Timestamp)) if limiter is not None: transmissions = limiter(transmissions) return (lastModified, (transmission for (transmission, broadcast) in transmissions))
def getUpcomingBroadcasts(self, station, all, update, timeLimit, maxTimeRange, limiter=None, notModifiedCheck=None, head=False): now = TimeUtils.now() where = And( Or(Broadcast.BroadcastEnd > now, Broadcast.BroadcastEnd == None), (Broadcast.BroadcastStart < (now + timeLimit))) if not all: where = And(where, Broadcast.Type == u"data") if station is not None: where = And(where, Broadcast.StationID == stationId) broadcasts = self.store.find(Broadcast, where) lastModified = max( broadcasts.max(Broadcast.Modified), station.BroadcastRemoved if station is not None else None, station.Schedule.Modified if (station is not None and station.Schedule is not None) else None) if head: return (lastModified, None, None, None) if notModifiedCheck is not None: notModifiedCheck(lastModified) if update: untilDate = datetime.fromtimestamp(now) untilDate += timedelta(seconds=timeLimit) untilDate = self.normalizeDate(untilDate) until = TimeUtils.toTimestamp(untilDate) if station is None: validUntil = self.scheduleMaintainer.updateSchedules( until, maxTimeRange) else: validUntil = self.scheduleMaintainer.updateSchedule( station, until, maxTimeRange) # trans.set_header_value("Expires", self.model.formatHTTPTimestamp(validUntil)) return (lastModified, broadcasts if limiter is None else limiter(broadcasts), validUntil >= until, validUntil)
def respond(self, trans): if not trans.get_request_method() in self.allowedMethods: trans.set_response_code(405) trans.set_header_value("Allow", ", ".join(self.allowedMethods)) print >> trans.get_response_stream( ), "Request method {0} is not allowed on this resource.".format( trans.get_request_method()) raise EndOfResponse self.parsePreferences(trans) self.store.autoreload() # to make sure we get current data self.trans = trans self.out = trans.get_response_stream() self.query = trans.get_fields_from_path() self.postQuery = {} if trans.get_content_type( ).media_type == "application/x-www-form-urlencoded": self.postQuery = trans.get_fields_from_body("utf-8") self.query.update(self.postQuery) trans.post_query = self.postQuery ifModifiedSince = trans.get_header_values("If-Modified-Since") if len(ifModifiedSince) > 0: try: self.ifModifiedSince = self.model.parseHTTPDate( ifModifiedSince[-1]) except ValueError as e: trans.set_response_code(400) print >> self.out, "If-Modified-Since date given in a invalid format: %s" % str( e) raise EndOfResponse self.ifModifiedSinceUnix = TimeUtils.toTimestamp( self.ifModifiedSince) else: self.ifModifiedSince = None self.ifModifiedSinceUnix = None self.normalizeQueryDict(self.query) self.normalizeQueryDict(self.postQuery) self.setupModel() self.head = trans.get_request_method() == "HEAD" try: result = self.handle(trans) finally: self.store.flush() return result
def respond(self, trans): if not trans.get_request_method() in self.allowedMethods: trans.set_response_code(405) trans.set_header_value("Allow", ", ".join(self.allowedMethods)) print >>trans.get_response_stream(), "Request method {0} is not allowed on this resource.".format(trans.get_request_method()) raise EndOfResponse self.parsePreferences(trans) self.store.autoreload() # to make sure we get current data self.trans = trans self.out = trans.get_response_stream() self.query = trans.get_fields_from_path() self.postQuery = {} if trans.get_content_type().media_type == "application/x-www-form-urlencoded": self.postQuery = trans.get_fields_from_body("utf-8") self.query.update(self.postQuery) trans.post_query = self.postQuery ifModifiedSince = trans.get_header_values("If-Modified-Since") if len(ifModifiedSince) > 0: try: self.ifModifiedSince = self.model.parseHTTPDate(ifModifiedSince[-1]) except ValueError as e: trans.set_response_code(400) print >>self.out, "If-Modified-Since date given in a invalid format: %s" % str(e) raise EndOfResponse self.ifModifiedSinceUnix = TimeUtils.toTimestamp(self.ifModifiedSince) else: self.ifModifiedSince = None self.ifModifiedSinceUnix = None self.normalizeQueryDict(self.query) self.normalizeQueryDict(self.postQuery) self.setupModel() self.head = trans.get_request_method() == "HEAD" try: result = self.handle(trans) finally: self.store.flush() return result