def updateMsg(self, m_msg): try: key = m_msg.key #sql = "replace into msg(key,answer,详情,l0,l1,l2,l3,l4,l5,l6,l7,l8,l9,syntax,author) values(\'"+key+"\',\'"+m_msg.answer['answer']+"\',\'"+m_msg.answer['详情']+"\',"+m_msg.price_sql()+"\'"+m_msg.answer['syntax']+"\',\'"+m_msg.answer['author']+"\')" #print sql d_l = "" d_v = "" l_l = "" l_v = "" for d in default_list[1:]: d_l += "%s," % d d_v += "'%s'," % m_msg.answer[d] for l in title: l_l += "%s," % l l_v += "'%s'," % m_msg.answer[l] sql = "replace into msg(key,%s%sl0,l1,l2,l3,l4,l5,l6,l7,l8,l9) values('%s',%s%s%s)" % ( d_l, l_l, m_msg.key, d_v, l_v, m_msg.price_sql()) print sql self.cs.execute(sql) self.conn.commit() print 'success' msg.update({key: m_msg}) return True except Exception as res: print "updateMsg Error!!" print res log_.warning("更新消息失败:" + res) return res
def updateImageInMsg(self, key, news_id): try: sql = "update msg set \'图片\' = \'" + news_id + "\' where key = \'" + key + "\'" self.cs.execute(sql) except Exception as res: print "updateImageInMsg ERR!!" log_.warning("升级图片信息失败:" + res) print res
def usersto(users=None): print users if users == None: return [manager.open_id] else: if isinstance(users, list): return users else: log_.warning("必须传入一个列表") print "'users' must be a list!" return
def updateFewImage(self, news): try: for (key, media_id) in news.items(): self.updateImageInMsg(key, media_id) self.conn.commit() return True except Exception as res: print "updateFewImage ERR!!" print res log_.warning("升级多个图片失败:" + res) return False
def getTargetUser(self, open_id): try: log_.info("获取目标用户,open_id = " + open_id) sql = "SELECT id,name,level,edit,address from user WHERE open_id = \'" + open_id + '\'' self.cs.execute(sql) res = self.cs.fetchone() user = USER(res[0], toStr(res[1]), toStr(open_id), res[2], res[3], res[4]) return user except Exception as res: print res log_.warning("获取用户失败") return USER(9999, '未知用户', open_id, 0, 0, 0)
def updateImage(self, key, news, images): try: img_id = "" img_url = "" for img in images: img_id += img.media_id + "," img_url += img.url + "," sql = "replace into IMG(key,img_id,img_url) values(\'" + key + "\',\'" + img_id + "\',\'" + umg_url + "\')" self.cs.execute(sql) self.updateImageInMsg(new_key, news) self.conn.commit() except Exception as res: print "updateImage ERR!!" log_.warning("更新图片失败:" + res) print res
def updateAnswer(self, content): try: log_.info("更新回答") key = content.split('号')[0] + '号' if not msg.has_key(key): return 'nomember' sql = "update msg set answer = \'" + content + "\' where key = \'" + key + "\'" self.cs.execute(sql) self.conn.commit() msg[key].answer['answer'] = content return 'success' except Exception as res: print res log_.warning("更新回答失败:" + res) return res
def update_one_user(self, user_info): try: log_.info( "更新一个用户: name: %s open_id: %s level %d edit %d address %d" % (user_info.name, user_info.open_id, user_info.level, user_info.can_edit, user_info.edit_address)) sql = "replace into user(name,open_id,level,edit,address) values(\'" + user_info.name + "\',\'" + user_info.open_id + "\'," + str( user_info.level) + "," + str(user_info.can_edit) + "," + str( user_info.edit_address) + ")" self.cs.execute(sql) self.conn.commit() return True except Exception as res: print res log_.warning("更新用户失败:" + res) return False
def getNewsList(open_id): getUrl = "https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token=%s" % tk.get_url_token[0] print getUrl result = urllib2.urlopen(urllib2.Request(getUrl)) res = json.loads(result.read()) if 'errcode' not in res: total = int(res['news_count']) offset = int(cfg.wechat['last_news_id']) cnt = total - offset ret = "本地有素材%d个,服务器有素材%d个" % (offset,total) if cnt > 0: thread = threading.Thread(target=getAllNews,args=(open_id,cnt)) thread.start() return ret + ",检测到%d个新素材" % cnt return ret + ",未检测到新素材" else: error = toStr(res['errmsg']) tk.sender(error,[open_id]) log_.warning("服务器返回错误消息:" + error) return ""
def getAnswer(self, content): try: log_.info("获取回答") ask = content.split('+') sql = "SELECT answer" for t in title: sql += t sql += ",l0,l1,l2,l3,l4,l5,l6,l7,l8,l9 from msg WHERE key = \'" + ask[ 0] + '\'' self.cs.execute(sql) res = self.cs.fetchone() answer_dict = {'answer': toStr(res[0])} price = Xstr(res[-9:]) for i, t in title: answer_dict[t] = toStr(res[i + 1]) return MSG(ask[0], answer_dict, price) except Exception as res: print res log_.warning("回答获取失败:" + res) return res
def get_user(open_id): if user.has_key(open_id): return user.get(open_id) try: mDB = DB() new_user = mDB.getTargetUser(open_id) if new_user.name != '未知用户': user.update({open_id: new_user}) mDB.refresh() return user.get(open_id) nick_name = tk.get_userInfo(open_id) new_user = USER(9999, nick_name, open_id, 0, 0, 0) if mDB.update_one_user(new_user): new_user = mDB.getTargetUser(open_id) user.update({open_id: new_user}) return new_user else: return new_user except Exception as e: print e log_.warning("获取用户信息失败:" + open_id) return USER(9999, "未知用户", open_id, 0, 0, 0) finally: mDB.close()