Ejemplo n.º 1
0
  def modify_wake_time(self, nick, day, hr, mn, user_data):
    import time
    import botconfig

    day = int(day)
    hr = int(hr)
    mn = int(mn)

    if (day < 1 or day > 31):
      print >>sys.stderr, '%s <- command error: invalid date(1)' % nick
      return REPLY_ERR_DATE

    today_t = time.time() - 43200 + botconfig.BotConfig["TimeOffset"]
    found_day = self.check_prev_day(day, today_t)
    if not found_day:
      print >>sys.stderr, '%s <- command error: invalid date(2)' % nick
      return REPLY_ERR_DATE

    if (hr < 0 or hr > 23 or mn < 0 or mn > 59):
      print >>sys.stderr, '%s <- command error: invalid time' % nick
      return REPLY_ERR_TIME

    user_data['mod_mon']  = found_day.tm_mon
    user_data['mod_mday'] = found_day.tm_mday
    twnmodels.WakeTime.put_time(nick, twnmodels.tw_day_id(found_day, False), hr, mn, 0)
    return REPLY_MOD_WAKE
Ejemplo n.º 2
0
  def make_stats(self, slps, wks):
    import time
    import math

    t = time.time() + 32400 - 604800
    gt = time.gmtime(t)

    stimes = []
    ssum   = 0
    avr    = -1

    for i in range(7):
      gt2 = time.gmtime(t)
      t += 3600*24
      gt3 = time.gmtime(t)

      did  = twnmodels.tw_day_id(gt2, False)
      did2 = twnmodels.tw_day_id(gt3, False)

      if (did in slps) and (did2 in wks):
        st = slps[did]
        wt = wks[did2]

        sleep_etime = wt['hour']*60 + wt['min'] + 60*24
        sleep_etime -= st['hour']*60 + st['min']

        ssum += sleep_etime
        stimes.append(sleep_etime)

    sdist = -1
    alen = len(stimes)
    if len(stimes)>0:
      avr = ssum / alen
      dsum = 0
      for s in stimes:
        dsum += ((s - avr)/60.0)*((s - avr)/60.0)

      sdist = math.sqrt(dsum / alen)

    return {'org_mon': gt.tm_mon, 'org_day': gt.tm_mday, 'avail_count': alen, 'average': avr/60.0, 'sdist': sdist}
Ejemplo n.º 3
0
  def make_user_list(cls):
    cached_list = memcache.get("sorted_userlist_cache")
    if cached_list:
      return cached_list

    import time
    t = time.time() +32400 - (86400*7)
    dayid = twnmodels.tw_day_id(time.gmtime(t), False)

    q_susers = twnmodels.SleepTime.all()
    q_susers.filter("day_id >= ", dayid).order('-day_id')

    q_wusers = twnmodels.WakeTime.all()
    q_wusers.filter("day_id >= ", dayid).order('-day_id')

    susers = q_susers.fetch(limit = 800)
    wusers = q_wusers.fetch(limit = 800)

    umap = {}
    for u in susers:
      if not u.username in umap:
        umap[u.username] = 1
      else:
        umap[u.username] += 1

    for u in wusers:
      if not u.username in umap:
        umap[u.username] = 1
      else:
        umap[u.username] += 1

    rlist = []
    for k,v in umap.iteritems():
      rlist.append((k,v))

    rlist.sort(lambda a, b: b[1]-a[1])
    memcache.set("sorted_userlist_cache", rlist, 300)

    return rlist
Ejemplo n.º 4
0
  def modify_sleep_time(self, nick, day, hr, mn, user_data):
    import time
    import botconfig

    day = int(day)
    hr = int(hr)
    mn = int(mn)

    if day < 1 or day > 31:
      print >>sys.stderr, '%s <- command error: invalid date(1)' % nick
      return REPLY_ERR_DATE

    now_t = time.time() + botconfig.BotConfig["TimeOffset"]
    today_t = now_t - 43200
    found_day = TwitterBotImpl.check_prev_day(day, today_t)
    if not found_day:
      print >>sys.stderr, '%s <- command error: invalid date(2)' % nick
      return REPLY_ERR_DATE

    if (hr >= 0 and hr < 3):
      hr += 24

    if (hr < 13 or hr > 36 or mn < 0 or mn > 59):
      print >>sys.stderr, '%s <- command error: invalid time' % nick
      return REPLY_ERR_TIME

    spec_t = time.mktime((found_day.tm_year, found_day.tm_mon, found_day.tm_mday, hr, mn, 0, 0, 0, -1))
    if (spec_t-now_t) > 600:
      print >>sys.stderr, '%s <- command error: invalid time(3)' % nick
      return REPLY_FWD_TIME

    user_data['mod_mon']  = found_day.tm_mon
    user_data['mod_mday'] = found_day.tm_mday
    twnmodels.SleepTime.put_time(nick, twnmodels.tw_day_id(found_day, False), hr, mn, 0)

    return REPLY_MOD_SLEEP
Ejemplo n.º 5
0
  def calc(cls):
    import time
    if memcache.get('bscore_cache_valid'):
      print >>sys.stderr, "BScoreUpdater aborted"
      return
    memcache.set('bscore_cache_valid', True, 1100)

    sa = twnmodels.SleepTime.all()
    wa = twnmodels.WakeTime.all()

    t = time.time() +32400 - (86400*7)
    dayid = twnmodels.tw_day_id(time.gmtime(t), False)

    # make a table for 'next' day ids
    next_did = {}
    for dt in range(8):
      next_did[ twnmodels.tw_day_id(time.gmtime(t+dt*86400), False) ] = twnmodels.tw_day_id(time.gmtime(t+(dt+1)*86400), False)

    sa.order('-day_id').filter('day_id >=', dayid)
    wa.order('-day_id').filter('day_id >=', dayid)

    slist = sa.fetch(limit = 800)
    wlist = wa.fetch(limit = 800)

    umap = {}

    st_dmap = {}
    wk_dmap = {}
    #make dayid+username map
    for st in slist:
      st_dmap[cls.make_day_and_user_key(next_did[st.day_id], st.username)] = True

    for wt in wlist:
      if wt.day_id and not (cls.make_day_and_user_key(wt.day_id, wt.username) in st_dmap): # Sleep time is missing. Assume 6 hours sleep
        dmy_h = wt.hour + 18
        slist.append(DummyTime(wt.username, dmy_h))

      wk_dmap[cls.make_day_and_user_key(wt.day_id, wt.username)] = True


    for st in slist:
      if not st.username in umap:
        umap[st.username] = 0

      if st.day_id:
        next_wake_id = cls.make_day_and_user_key(next_did[st.day_id], st.username)
        if not (next_wake_id in wk_dmap): # Wake time is missing. Assume 6 hours sleep
          dmy_h = st.hour + 6
          if (dmy_h > 23):
            dmy_h -= 24
          else:
            dmy_h = 0
          wlist.append(DummyTime(st.username, dmy_h))

      umap[st.username] += cls.calc_st_score( st.hour )

    for wt in wlist:
      if not wt.username in umap:
        umap[wt.username] = 0

      umap[wt.username] += cls.calc_wt_score( wt.hour )

    uall = twnmodels.TwitterUser.all().order("-s_timestamp")
    prefetch_map = {}
    for uobj in uall:
      prefetch_map[uobj.username] = uobj

    for score in umap:
      twnmodels.TwitterUser.set_bscore(score, umap[score], t, prefetch_map)

    print >>sys.stderr, "updated bscore"
    memcache.set("rank_timestamp", value=int(t))