Beispiel #1
0
  def post(self):
    webapp.template.register_template_library('templatetags.spacify')    
    friend_id = self.request.get('friend_id','')
    try:
      friend_id = long(friend_id)
    except:
      self.response.out.write('請輸入整數 Q____Q')
      return
    
    # Check update time I
    if datetime.utcnow().hour >= 19 or datetime.utcnow().hour < 1:
      self.response.out.write('您無法在 UTC 時間 19:00 至 01:00 期間實行手動更新。(這是台灣時間 03:00 至 09:00,日本時間 04:00 至 10:00。)')
      return
    # Check update time II
    ranker = rankingmodel.RankerList.gql('WHERE friend_id = :1', friend_id).get()
    if ranker is not None and ranker.last_updated:
      time_diff = datetime.utcnow() - ranker.last_updated
      if time_diff.days == 0 and time_diff.seconds < 900:
        self.response.out.write('您無法在15分鐘之內重複進行更新。')
        return
       
    # Connect to 573, checking player location
    try:
      url = 'https://www.ea-pass.konami.net/contents/jubeat/ripples/play_top.do?fid=' + str(friend_id)
      result = urlfetch.fetch(url=url)
    except:
      self.response.out.write('573 is down!')
      logging.warning('573 is down (urlfetch exception)')
      return
    if result.status_code != 200:
      self.response.out.write('573 is down!')
      logging.warning('573 is down (HTTP %d)' % result.status_code)
      return
    page_content = result.content
    if page_content.find('error') > -1 or page_content.find('<div id="playerNameBox">') == -1:
      self.response.out.write('發生問題。請確定您輸入的是正確的朋友ID / 或著現在573在維修。')
      return
    tbs_rank_match = re.search("<h5>([0-9]+)[^0-9<]*([0-9]+)位<\/h5>", page_content, re.UNICODE)
    if tbs_rank_match is None:
      if ranker is not None:
        r = GetRanker()
        r.SetScore(str(friend_id), None)
        ranker.delete()
        self.response.out.write('玩家資料為非公開,已將採計過的成績刪除。')
        return
      else:
        self.response.out.write('玩家資料為非公開,無法採計 :(。')
        return
    tbs = int(tbs_rank_match.group(1))
    tbs_rank = int(tbs_rank_match.group(2))
    if page_content.find('台湾') == -1:
      self.response.out.write('就跟你講台灣限定了咩 >"< / Please enter player whose last played area is Taiwan.')
      return
    # If tbs is not updated, do nothing
    if ranker is not None:
      if ranker.tbs == tbs:
        ranker.last_updated = datetime.utcnow()
        ranker.put()
        r = GetRanker()
        rank = r.FindRank([ranker.score_average])
        #self.response.out.write('<p>玩家資料已經採計(成績未刷新)。</p><p><a href="/ju/">回到排行表</a></p>')
        path = os.path.join(os.path.dirname(__file__), 'update.htm')
        result = template.render(path, {'friend_id': friend_id, 'card_name': ranker.card_name, 'rank': rank + 1})
        self.response.out.write(result)
        return
    #time.sleep(1)
    # Connect to 573, fetching score
    try:
      url = 'https://www.ea-pass.konami.net/contents/jubeat/ripples/play_mdata_list.do?fid=' + str(friend_id)
      result = urlfetch.fetch(url=url)
    except:
      self.response.out.write('573 is down!')
      return
    if result.status_code != 200:
      self.response.out.write('573 is down!')
      return
    page_content = result.content
    if page_content.find('<div id="tune">') == -1:
      self.response.out.write('拿取成績頁面時發生問題。請通知管理者。')
      return
    # Super-dirty but super-fast parsing
    player_name_match = re.search(r"<h4>([^<]+)さんの楽曲データ一覧<\/h4>", page_content)
    card_name = player_name_match.group(1)
    card_name = re.sub(r"\&nbsp\;", " ", card_name)
    find_result = re.findall(r"<td class\=\"score boderTop boderRight boderBottom (bgWhite01|bgBlue01)\">([0-9]+)<\/td>\s+<\/tr>", page_content)
    song_list = re.findall(r"<img src\=\"images\/player\_data\/music\_icon\/id([0-9]+)\.gif\"\s+\/>", page_content)
    best_score_total = 0
    if ranker is None:
      ranker = rankingmodel.RankerList(
        friend_id = friend_id,
        card_name = card_name,
      )
    ranker.card_name = card_name # &nbsp; fixes hack
    ranker.songs_exc = 0
    ranker.songs_sss = 0
    ranker.songs_ss = 0
    ranker.songs_s = 0
    ranker.songs_a = 0
    ranker.songs_b = 0
    ranker.songs_c = 0
    del ranker.song_list[:]
    del ranker.song_scores[:]
    for item in song_list:
      ranker.song_list.append(long(item))
    for item in find_result:
      item_score = int(item[1])
      ranker.song_scores.append(item_score)
      if item_score > 700000: # C
        ranker.songs_c += 1
      if item_score > 800000: # B
        ranker.songs_b += 1
      if item_score > 850000: # A
        ranker.songs_a += 1
      if item_score > 900000: # S
        ranker.songs_s += 1
      if item_score > 950000: # SS
        ranker.songs_ss += 1
      if item_score > 980000: # SSS
        ranker.songs_sss += 1
      if item_score == 1000000: # EXC
        ranker.songs_exc += 1
      best_score_total += item_score

    # Register into DB
    ranker.tbs = tbs
    ranker.tbs_rank = tbs_rank
    ranker.score_average = best_score_total / len(find_result)
    ranker.last_updated = datetime.utcnow()
    ranker.put()
    r = GetRanker()
    r.SetScore(str(friend_id), [ranker.score_average])
    rank = r.FindRank([ranker.score_average])
    # Purge memcache
    memcache.delete('mainpage')
    # Result
    path = os.path.join(os.path.dirname(__file__), 'update.htm')
    result = template.render(path, {'friend_id': friend_id, 'card_name': ranker.card_name, 'rank': rank + 1})
    self.response.out.write(result)
Beispiel #2
0
    def post(self):
        friend_id = self.request.get("friend_id", "")
        try:
            friend_id = long(friend_id)
        except:
            logging.error("Updater Error: Invalid Friend ID")
            return
        old_tbs = self.request.get("tbs", "")
        try:
            old_tbs = long(old_tbs)
        except:
            logging.warning("Updater Error: Invalid TBS")
            old_tbs = 0

        # Connect to 573, checking player location
        try:
            url = "https://www.ea-pass.konami.net/contents/jubeat/ripples/play_top.do?fid=" + str(friend_id)
            result = urlfetch.fetch(url=url)
        except:
            logging.error("573 is down (playerdata, urlfetch exception)")
            return
        if result.status_code != 200:
            logging.error("573 is down (HTTP %d)" % result.status_code)
            return
        page_content = result.content
        if page_content.find("error") > -1 or page_content.find('<div id="playerNameBox">') == -1:
            logging.warning("Page fetch error. Is 573 down?")
            return
        tbs_rank_match = re.search("<h5>([0-9]+)[^0-9<]*([0-9]+)位<\/h5>", page_content, re.UNICODE)
        if tbs_rank_match is None:
            r = GetRanker()
            r.SetScore(str(friend_id), None)
            ranker = rankingmodel.RankerList.gql("WHERE friend_id = :1", friend_id).get()
            if ranker is None:
                logging.error("Updater Error: Friend ID is not existed")
                return
            ranker.delete()
            logging.warning(str(friend_id) + "is set to inpublic. Deleted.")
            return
        tbs = int(tbs_rank_match.group(1))
        tbs_rank = int(tbs_rank_match.group(2))
        if old_tbs > 0:
            if old_tbs == tbs:
                ranker = rankingmodel.RankerList.gql("WHERE friend_id = :1", friend_id).get()
                if ranker is None:
                    logging.error("Updater Error: Friend ID is not existed")
                    return
                ranker.last_updated = datetime.utcnow()
                ranker.put()
                logging.info(str(friend_id) + ": TBS is the same as in the DB. Won't update.")
                return
        # Connect to 573, fetching score
        try:
            url = "https://www.ea-pass.konami.net/contents/jubeat/ripples/play_mdata_list.do?fid=" + str(friend_id)
            result = urlfetch.fetch(url=url)
        except:
            logging.error("573 is down (songlist, urlfetch exception")
            return
        if result.status_code != 200:
            logging.error("573 is down (HTTP %d)" % result.status_code)
            return
        page_content = result.content
        if page_content.find('<div id="tune">') == -1:
            logging.warning("Score page down")
            return
        # Super-dirty but super-fast parsing
        player_name_match = re.search(r"<h4>([^<]+)さんの楽曲データ一覧<\/h4>", page_content)
        card_name = player_name_match.group(1)
        card_name = re.sub(r"\&nbsp\;", " ", card_name)

        find_result = re.findall(
            r"<td class\=\"score boderTop boderRight boderBottom (bgWhite01|bgBlue01)\">([0-9]+)<\/td>\s+<\/tr>",
            page_content,
        )
        song_list = re.findall(r"<img src\=\"images\/player\_data\/music\_icon\/id([0-9]+)\.gif\"\s+\/>", page_content)
        best_score_total = 0
        ranker = rankingmodel.RankerList.gql("WHERE friend_id = :1", friend_id).get()
        if ranker is None:
            logging.error("Updater Error: Friend ID is not existed")
            return
        ranker.card_name = card_name  # &nbsp; fixes hack
        ranker.songs_exc = 0
        ranker.songs_sss = 0
        ranker.songs_ss = 0
        ranker.songs_s = 0
        ranker.songs_a = 0
        ranker.songs_b = 0
        ranker.songs_c = 0
        del ranker.song_list[:]
        del ranker.song_scores[:]
        for item in song_list:
            ranker.song_list.append(long(item))
        for item in find_result:
            item_score = int(item[1])
            ranker.song_scores.append(item_score)
            if item_score > 700000:  # C
                ranker.songs_c += 1
            if item_score > 800000:  # B
                ranker.songs_b += 1
            if item_score > 850000:  # A
                ranker.songs_a += 1
            if item_score > 900000:  # S
                ranker.songs_s += 1
            if item_score > 950000:  # SS
                ranker.songs_ss += 1
            if item_score > 980000:  # SSS
                ranker.songs_sss += 1
            if item_score == 1000000:  # EXC
                ranker.songs_exc += 1
            best_score_total += item_score

        # Register into DB
        ranker.tbs = tbs
        ranker.tbs_rank = tbs_rank
        ranker.score_average = best_score_total / len(find_result)
        ranker.last_updated = datetime.utcnow()
        ranker.put()
        # Purge memcache
        memcache.delete("mainpage")
        logging.info(str(card_name) + "(" + str(friend_id) + ")Updated.")