Exemplo n.º 1
0
  def stored_number(self, lang):
    """
    use for local
    """
    result = 0
    currentNumber = memcache.get("currentNumber")
    if currentNumber is not None:
      # result = currentNumber.serial_number
      result = currentNumber
      logging.info("### get currentNumberl from Memcache!")
    else:
      query = Adminkun.all()
      query.filter('lang =', lang)
      query.order('-serial_number')
      data = query.get()
      if data is not None:
        result = int(data.serial_number)
        if not memcache.add("currentNumber", result, 60 * 60 * 12):
          logging.error("*** Memcache set failed for currentNumber.")
        else:
          logging.info("### Set new currentNumber to Memcache.")

    logging.info(u"### Current stored number is %s.", result)
    return result
    pass
Exemplo n.º 2
0
  def fetch(self, lang):
    """
      指定された言語のインデックス情報を10件取得する
    """
    if lang == 'ja':
      cacheAdminkun = memcache.get("cacheAdminkun")
      if cacheAdminkun is not None:
        data = []
        data2 = []
        data = eval(cacheAdminkun)  
        logging.info("### get data successful from Memcache!")
        status_code = "200"
      else:
        result = urlfetch.fetch(Config['index_ja_url'])
        logging.info("*** url is %s" % Config['index_ja_url'])
        # print result.content
        
        html = unicode(result.content,'cp932') # shift-jisではエラーが発生する
        data = self.parse_adminkun(html, 'backbox')
        data2 = self.parse_adminkun(html, 'newbox')
        data.append(data2[0]) # 新着の先頭が最新
        status_code = result.status_code
        if not memcache.add("cacheAdminkun", '%s'%data, 60 * 60 * 6): # 24時間おき
#        if not memcache.add("cacheAdminkun", '%s'%data, 60):

          logging.error("*** Memcache set failed.")
          # return cacheAdminkun
        else:
          logging.info("### Set new data to Memcache.")
    else: # 英語版(未実装)
      result = urlfetch.fetch(Config['index_en_url'])
      html = unicode(result.content,'shift-jis')
      data = self.parse_adminkun(html, 'backbox')
      data.append(self.parse_adminkun(html, 'newbox'))

    # query = Adminkun.all()
    # query.filter('lang =', lang)
    # query.order('-serial_number')
    # current_number = 0
    # if query.get() is not None:
    #   current_number = int(query.get().serial_number)
    current_number = self.stored_number(lang)
    addedNumber = 0
    for i in data:
      if int(i['serial_number']) > current_number:
        adminkun = Adminkun (
          serial_number   = int(i['serial_number']),
          read_flag       = False,
          index_title     = i['index_title'].strip(),
          index_overview  = i['index_overview'].strip(),
          index_image_url = u'%s%s' % (SITE_URL, i['index_image_url']),
          body_url        = u'%s%s' % (SITE_URL, i['body_url']),
          body_image_url  = self.make_body_image_url(i['serial_number'], lang),
          lang = lang
        )
        adminkun.put() # データの保存
        addedNumber = addedNumber + 1
        # TODO: ここにpush通知の処理を入れる
        if addedNumber == 5: # 5件取得したら終了
          break;
    memcache.delete('currentNumber')
#    memcache.delete('cacheAdminkun')
    result = self.to_json({'added_comic': addedNumber, 'status_code' : status_code})
    self.render(json = result)
    pass
Exemplo n.º 3
0
  def get(self):
    """
    呼び出し方:?serial_number=1&lang=ja
    指定した連載番号記事のインデックス情報をデータストアから取得し、JSON形式で返す。
    errorCode:000 success
              011 引数の数が不正
              012 引数が不正
              013 連載番号が指定されていない
              014 言語パラメータが存在しない
              015 言語パラメータの値が存在しない
              016 言語パラメータの値が不正(ja or en)
    """
    statusCode = '000'
    result = ""
    resultArray = []
    if len(self.params) < 2+2: # action, controller, serial_number, lang
      statusCode = '011'
      result = self.to_json({'status_code' : statusCode})
    elif self.params.has_key('serial_number') is False:
      statusCode = '012' # no parameter
      result = self.to_json({'status_code' : statusCode})
    elif len(self.params['serial_number']) == 0:
      statusCode = '013' # no number
      result = self.to_json({'status_code' : statusCode})
    elif self.params.has_key('lang') is False:
      statusCode = '014' # no lang parameter
      result = self.to_json({'status_code' : statusCode})
    elif len(self.params['lang']) == 0:
      statusCode = '015' # no lang
      result = self.to_json({'status_code' : statusCode})
    elif self.params['lang'] != 'ja' and self.params['lang'] != 'en':
      statusCode = '016' # out of param. (ja or en only)
      result = self.to_json({'status_code' : statusCode})
    else:
      query = Adminkun.all()
      query.filter('lang =', self.params['lang'])
      # current_number = query.count()
      current_number = self.stored_number(self.params['lang'])
      if int(self.params['serial_number']) > current_number:
        statusCode = '17' # 登録されているデータの範囲を超えた
        result = self.to_json({'status_code' : statusCode})
        self.render(json = result)
        return
    
      number = self.params['serial_number']
      lang = self.params['lang']
      query = Adminkun.all()
      query.filter('serial_number =', int(number))
      query.filter('lang =', lang)
      query.order('-serial_number')

      data = query.get()
      result = self.to_json({'serial_number'  : data.serial_number,
                            'index_title'     : data.index_title,
                            'index_overview'  : data.index_overview,
                            'index_image_url' : data.index_image_url,
                            'body_url'        : data.body_url,
                            'body_image_url'  : data.body_image_url,
                            'status_code'     : statusCode,
                            'lang'            : lang
                            })

    self.render(json = result)
    pass