def updatePttAndMessage(self, pttSource, d): messagestr = d['g_message'].replace('\\', '') message = json.loads(messagestr) dbHandler = MySQLHandler() # 把是否有oraclet的判斷移到外面 # self.newFindCode(mysqlData['title'],mysqlData['content']) codeInfo = self.newFindCode(pttSource[0]['title'], pttSource[0]['content']) print codeInfo if codeInfo != False: try: oracletNumber = dbHandler.getOneOraclet( pttSource[0]['author'].split(u'(')[0].encode('utf-8'), pttSource[0]['date'].date().strftime("%Y-%m-%d"), str(codeInfo[0]), self.getEvent( pttSource[0]['content']).encode('utf-8'))[0]['number'] except IndexError as e: logging.exception("updatePtt") return False # 如果標的文沒有event的話會跳到這,也就是此文章沒有立卦的意思 except AttributeError as e: print 'updatePttAndMessage AttributeError---------------------------------------------' logging.exception("updatePttGetEvent") return False dbMessage = json.loads(pttSource[0]['message']) updateName = set() # 如果新撈到的留言筆數比原資料庫中的筆數多 if len(dbMessage) < len(message): # 需更新pttsource資料庫!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! dbHandler.updatePttsource(pttSource[0]['author'], pttSource[0]['date'], messagestr) for temp in message: # 不確定能不能用,需確認 if temp not in dbMessage: updateName.add(temp['id']) message = SetOracletHandler.normalizeMessage(messagestr) print message for temp in message: if temp['id'] in updateName: # 需要測試!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! dbHandler.updateOrInsetMessage( temp['id'], pttSource[0]['date'], temp['time'], temp['content'].replace(' ', ''), oracletNumber, temp['status'])
def dataToMYsql(self, d): dbHandler = MySQLHandler() pttSource = dbHandler.getOnePttSource(d['b_author'], d['d_date']) if pttSource == False: # 新增ptt_source dbHandler.setOnePttSource(d) # 卜卦立案 oracletDict = self.pttBuild(d) if oracletDict != False: # 毛盾卜卦找尋集更新、更新留言庫 dbHandler.setOneContradiction(oracletDict) messagestr = d['g_message'].replace('\\', '') message = SetOracletHandler.normalizeMessage(messagestr) dbHandler.setNewMessage(message, oracletDict) else: # 拿取oracelt 沒拿到代表不用做更新 dbHandler.updatePttsource(d['b_author'], d['d_date'], d['g_message']) self.updatePttAndMessage(pttSource, d) # 檢查是否需要更新ptt_source、更新留言庫 pass pass