Example #1
0
 def insertpic(self, picreg):
     if (isinstance(picreg, Picreg)):
         self.inition()
         #获取操作游标
         cursor = self.conn.cursor()
         try:
             cursor.execute(
                 """INSERT INTO weibocatch.pic_reg
                         (path) VALUES (%s);""", picreg.picpath)
             self.conn.commit()
             #使用这函数向一个给定Connection对象返回的值是该Connection对象产生
             #对影响AUTO_INCREMENT列的最新语句第一个AUTO_INCREMENT值的。
             #这个值不能被其它Connection对象的影响,即它们产生它们自己的AUTO_INCREMENT值。
             #第二、LAST_INSERT_ID 是与table无关的,
             #如果向表a插入数据后,再向表b插入数据,LAST_INSERT_ID返回表b中的Id值。
             cursor.execute("SELECT LAST_INSERT_ID()")
             #"只获取一条记录:"
             result = cursor.fetchone()
             #这个返回的是一个tuple,一个元素的元组后面都会跟一个”,“。没实际意义
             print time.strftime('%H-%M-%S') + ":" + "图片存入成功"
             return result[0]
         except Exception as err:
             self.conn.rollback()
             print time.strftime('%H-%M-%S') + ":" + str(err)
             werror = Werror()
             werror.id = '00000'  #图片路径存储异常
             werror.error = str(err)
             self.seterror(werror)
         finally:
             #关闭连接,释放资源
             cursor.close()
             if (self.conn.open == 1):
                 self.conn.close()
Example #2
0
 def insertpic(self,picreg):
     if(isinstance(picreg,Picreg)):
         self.inition()
         #获取操作游标     
         cursor = self.conn.cursor()
         try:
             cursor.execute("""INSERT INTO weibocatch.pic_reg
                         (path) VALUES (%s);""",picreg.picpath)
             self.conn.commit()
             #使用这函数向一个给定Connection对象返回的值是该Connection对象产生
             #对影响AUTO_INCREMENT列的最新语句第一个AUTO_INCREMENT值的。
             #这个值不能被其它Connection对象的影响,即它们产生它们自己的AUTO_INCREMENT值。
             #第二、LAST_INSERT_ID 是与table无关的,
             #如果向表a插入数据后,再向表b插入数据,LAST_INSERT_ID返回表b中的Id值。
             cursor.execute("SELECT LAST_INSERT_ID()")
             #"只获取一条记录:"   
             result = cursor.fetchone();#这个返回的是一个tuple,一个元素的元组后面都会跟一个”,“。没实际意义
             print time.strftime('%H-%M-%S')+":"+"图片存入成功"
             return result[0]
         except Exception as err:
             self.conn.rollback()
             print time.strftime('%H-%M-%S')+":"+str(err)
             werror=Werror()
             werror.id='00000'#图片路径存储异常
             werror.error=str(err)
             self.seterror(werror)
         finally:
             #关闭连接,释放资源     
             cursor.close();
             if(self.conn.open==1):
                 self.conn.close()
Example #3
0
 def insertcont(self, weibocontent, picid, wconn):
     if (isinstance(weibocontent, WeiboContent)):
         self.inition()
         self.md5.update(
             weibocontent.id.encode('utf8') +
             weibocontent.intime.encode('utf8') +
             weibocontent.content.encode('utf8'))
         cursor = self.conn.cursor()
         try:
             if (picid == ''):
                 picid = 0
             cursor.execute(
                 """INSERT INTO weibocatch.w_content
                             (weiboid,wid,content,url,map,label,intime,picid,wfrom)
                             VALUES
                             (%s,%s,%s,%s,%s,%s,%s,%s,%s);
                             """,
                 (self.md5.hexdigest(), weibocontent.id,
                  weibocontent.content.encode('utf8'), weibocontent.url,
                  weibocontent.map.encode('utf8'),
                  weibocontent.label.encode('utf8'),
                  weibocontent.intime.encode('utf8'), int(picid),
                  weibocontent.wfrom.encode('utf8')))
             weibocontentjson = self.class_to_dict(weibocontent)
             self.initionMo()
             table1 = self.db.weibocontent
             table1.insert_one(weibocontentjson)
             if (isinstance(wconn, Wconn)):
                 if (wconn.tag == 1 or wconn.tag == 0):  #判断字符串不为空
                     cursor.execute(
                         """INSERT INTO weibocatch.w_conn
                                     (weiboid,tag,atid,atname)
                                     VALUES
                                     (%s,%s,%s,%s);""",
                         (self.md5.hexdigest(), wconn.tag, wconn.atid,
                          wconn.atname))
                     #tag表示微博是来自原创还是转发,1是转发,0是原创
                     wconnjson = self.class_to_dict(wconn)
                     table2 = self.db.wconn
                     table2.insert_one(wconnjson)
             self.conn.commit()  # 一定不要漏了,不然不会执行的
             print time.strftime(
                 '%H-%M-%S'
             ) + ":" + "%s原创微博存入成功" % weibocontent.id.encode('utf8')
         except Exception as err:
             self.conn.rollback()
             print time.strftime('%H-%M-%S') + ":" + str(err)
             if (err[0] != 1062):
                 werror = Werror()
                 werror.id = weibocontent.id
                 werror.error = str(err)
                 self.seterror(werror)
         finally:
             #关闭连接,释放资源
             cursor.close()
             if (self.conn.open == 1):
                 self.conn.close()
             if (self.client != None):
                 self.client.close()
Example #4
0
 def inserttran(self,weibotran,picid,wconn):
     if(isinstance(weibotran, WeiboTransfer)):
         self.inition()#因为mysql 连接很容易超时就断开了,默认10s,所以索性每次都用的时候在申明
         self.md5.update(weibotran.id.encode('utf8')+weibotran.intime.encode('utf8')
                         +weibotran.remark.encode('utf8')+weibotran.content.encode('utf8'))
         #获取操作游标     
         cursor = self.conn.cursor()
         try: 
             if(picid==''):
                 picid=0
             #插入一条记录 全部用%s作占位符号,即使是数字也不要用%d 
             #因为这个插件程序会帮你转换的,字符串会自动给你加'',如果数字不会加
             cursor.execute("""INSERT INTO weibocatch.w_transfer
                             (weiboid,wid,transferid,content,remark,label,url,
                             intime,picid,wfrom)
                             VALUES
                             (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);
                             """,(self.md5.hexdigest(),weibotran.id,weibotran.tranferid,
                                  weibotran.content.encode('utf8'),weibotran.remark.encode('utf8'),weibotran.label.encode('utf8'),
                                  weibotran.url,weibotran.intime.encode('utf8'),int(picid),weibotran.wfrom.encode('utf8')));
             weibotranjson=self.class_to_dict(weibotran)
             self.initionMo()
             table1=self.db.weibotran
             table1.insert_one(weibotranjson)
             if(isinstance(wconn, Wconn)):
                 if(wconn.tag==1 or wconn.tag==0):#判断字符串不为空
                     cursor.execute("""INSERT INTO weibocatch.w_conn
                                     (weiboid,tag,atid,atname)
                                     VALUES
                                     (%s,%s,%s,%s);""",(self.md5.hexdigest(),wconn.tag,
                                                        wconn.atid,wconn.atname))
                     wconnjson=self.class_to_dict(wconn)
                     table2=self.db.wconn
                     table2.insert_one(wconnjson)
             self.conn.commit()# 一定不要漏了,不然不会执行的
             print time.strftime('%H-%M-%S')+":"+"%s转发微博存入成功" % weibotran.id.encode('utf8')
         except Exception as err:
             self.conn.rollback()
             print time.strftime('%H-%M-%S')+":"+str(err)
             if(err[0]!=1062):
                 werror=Werror()
                 werror.id=weibotran.id
                 werror.error=str(err)
                 self.seterror(werror)
         finally:
             #关闭连接,释放资源     
             cursor.close();
             if(self.conn.open==1):
                 self.conn.close() 
             if(self.client!=None):
                 self.client.close()
Example #5
0
 def insertcont(self,weibocontent,picid,wconn):
     if(isinstance(weibocontent, WeiboContent)):
         self.inition()
         self.md5.update(weibocontent.id.encode('utf8')+weibocontent.intime.encode('utf8')
                         +weibocontent.content.encode('utf8'))
         cursor=self.conn.cursor()
         try:
             if(picid==''):
                 picid=0
             cursor.execute("""INSERT INTO weibocatch.w_content
                             (weiboid,wid,content,url,map,label,intime,picid,wfrom)
                             VALUES
                             (%s,%s,%s,%s,%s,%s,%s,%s,%s);
                             """,(self.md5.hexdigest(),weibocontent.id,
                                  weibocontent.content.encode('utf8'),weibocontent.url,weibocontent.map.encode('utf8'),
                                  weibocontent.label.encode('utf8'),weibocontent.intime.encode('utf8'),int(picid),
                                  weibocontent.wfrom.encode('utf8')))
             weibocontentjson=self.class_to_dict(weibocontent)
             self.initionMo()
             table1=self.db.weibocontent
             table1.insert_one(weibocontentjson)
             if(isinstance(wconn, Wconn)):
                 if(wconn.tag==1 or wconn.tag==0):#判断字符串不为空
                     cursor.execute("""INSERT INTO weibocatch.w_conn
                                     (weiboid,tag,atid,atname)
                                     VALUES
                                     (%s,%s,%s,%s);""",(self.md5.hexdigest(),wconn.tag,
                                                        wconn.atid,wconn.atname))
                     #tag表示微博是来自原创还是转发,1是转发,0是原创
                     wconnjson=self.class_to_dict(wconn)
                     table2=self.db.wconn
                     table2.insert_one(wconnjson)
             self.conn.commit()# 一定不要漏了,不然不会执行的
             print time.strftime('%H-%M-%S')+":"+"%s原创微博存入成功" % weibocontent.id.encode('utf8')
         except Exception as err:
             self.conn.rollback()
             print time.strftime('%H-%M-%S')+":"+str(err)
             if(err[0]!=1062):
                 werror=Werror()
                 werror.id=weibocontent.id
                 werror.error=str(err)
                 self.seterror(werror)
         finally:
             #关闭连接,释放资源     
             cursor.close();
             if(self.conn.open==1):
                 self.conn.close() 
             if(self.client!=None):
                 self.client.close()
Example #6
0
 def insertuser(self,wuser,relation):
     if(isinstance(wuser, Wuser)):
         if(isinstance(relation, Relation)):
             self.inition()
             cursor=self.conn.cursor()
             try:
                 cursor.execute("""INSERT INTO weibocatch.w_user
                                 (wid,wname,recon,color,flag)
                                 VALUES
                                 (%s,%s,%s,%s,%s);
                                 """,(wuser.id,wuser.name,
                                      wuser.recon,wuser.color,0))
                 self.conn.commit()
                 #1 生成的json不能输出汉字,只输出utf8原始编码,形如 u\x55..格式,
                 #需要在 json.dumps(xxxx,ensure_ascii=false,indent=1)参数列表指定 ensure_ascii=false
                 #2 indent =1 单行输出不换行, indent=2 多行输出如下
                 #insert_one 可以接受传入字典类型并存为json ,还有虽然字典里中文会编码,
                 #但是默认在mongodb里默认是utf8编码,字典里内容会自动转为明码标示。
                 wuserjson=self.class_to_dict(wuser)
                 self.initionMo()
                 table1=self.db.wuser
                 table1.insert_one(wuserjson)
                 print time.strftime('%H-%M-%S')+":"+"用户存入成功!"
             except Exception as err:
                 self.conn.rollback()
                 print time.strftime('%H-%M-%S')+":"+str(err)
                 if(err[0]!=1062):#判断不是主键重复错误
                     werror=Werror()
                     werror.id=wuser.id
                     werror.error=str(err)
                     self.seterror(werror)
             finally:
                 #关闭连接,释放资源     
                 cursor.close();
                 if(self.conn.open==1):
                     self.conn.close();
                 if(self.client!=None):
                     self.client.close()
         self.inition()
         cursor=self.conn.cursor()
         try:
             cursor.execute("""INSERT INTO weibocatch.w_relation
                             (wid,wfriendid)
                             VALUES
                             (%s,%s);
                             """,(relation.id,relation.friendid))
             self.conn.commit()
             #1 生成的json不能输出汉字,只输出utf8原始编码,形如 u\x55..格式,
             #需要在 json.dumps(xxxx,ensure_ascii=false,indent=1)参数列表指定 ensure_ascii=false
             #2 indent =1 单行输出不换行, indent=2 多行输出如下
             #insert_one 可以接受传入字典类型并存为json ,还有虽然字典里中文会编码,
             #但是默认在mongodb里默认是utf8编码,字典里内容会自动转为明码标示。
             relationjson=self.class_to_dict(relation)
             self.initionMo()
             table2=self.db.relation
             table2.insert_one(relationjson)
             print time.strftime('%H-%M-%S')+":"+"关注存入成功!"
         except Exception as err:
             self.conn.rollback()
             print time.strftime('%H-%M-%S')+":"+str(err)
             werror=Werror()
             werror.id=wuser.id
             werror.error=str(err)
             self.seterror(werror)
         finally:
             #关闭连接,释放资源     
             cursor.close();
             if(self.conn.open==1):
                 self.conn.close();
             if(self.client!=None):
                 self.client.close()
Example #7
0
 def insertuser(self, wuser, relation):
     if (isinstance(wuser, Wuser)):
         if (isinstance(relation, Relation)):
             self.inition()
             cursor = self.conn.cursor()
             try:
                 cursor.execute(
                     """INSERT INTO weibocatch.w_user
                                 (wid,wname,recon,color,flag)
                                 VALUES
                                 (%s,%s,%s,%s,%s);
                                 """,
                     (wuser.id, wuser.name, wuser.recon, wuser.color, 0))
                 self.conn.commit()
                 #1 生成的json不能输出汉字,只输出utf8原始编码,形如 u\x55..格式,
                 #需要在 json.dumps(xxxx,ensure_ascii=false,indent=1)参数列表指定 ensure_ascii=false
                 #2 indent =1 单行输出不换行, indent=2 多行输出如下
                 #insert_one 可以接受传入字典类型并存为json ,还有虽然字典里中文会编码,
                 #但是默认在mongodb里默认是utf8编码,字典里内容会自动转为明码标示。
                 wuserjson = self.class_to_dict(wuser)
                 self.initionMo()
                 table1 = self.db.wuser
                 table1.insert_one(wuserjson)
                 print time.strftime('%H-%M-%S') + ":" + "用户存入成功!"
             except Exception as err:
                 self.conn.rollback()
                 print time.strftime('%H-%M-%S') + ":" + str(err)
                 if (err[0] != 1062):  #判断不是主键重复错误
                     werror = Werror()
                     werror.id = wuser.id
                     werror.error = str(err)
                     self.seterror(werror)
             finally:
                 #关闭连接,释放资源
                 cursor.close()
                 if (self.conn.open == 1):
                     self.conn.close()
                 if (self.client != None):
                     self.client.close()
         self.inition()
         cursor = self.conn.cursor()
         try:
             cursor.execute(
                 """INSERT INTO weibocatch.w_relation
                             (wid,wfriendid)
                             VALUES
                             (%s,%s);
                             """, (relation.id, relation.friendid))
             self.conn.commit()
             #1 生成的json不能输出汉字,只输出utf8原始编码,形如 u\x55..格式,
             #需要在 json.dumps(xxxx,ensure_ascii=false,indent=1)参数列表指定 ensure_ascii=false
             #2 indent =1 单行输出不换行, indent=2 多行输出如下
             #insert_one 可以接受传入字典类型并存为json ,还有虽然字典里中文会编码,
             #但是默认在mongodb里默认是utf8编码,字典里内容会自动转为明码标示。
             relationjson = self.class_to_dict(relation)
             self.initionMo()
             table2 = self.db.relation
             table2.insert_one(relationjson)
             print time.strftime('%H-%M-%S') + ":" + "关注存入成功!"
         except Exception as err:
             self.conn.rollback()
             print time.strftime('%H-%M-%S') + ":" + str(err)
             werror = Werror()
             werror.id = wuser.id
             werror.error = str(err)
             self.seterror(werror)
         finally:
             #关闭连接,释放资源
             cursor.close()
             if (self.conn.open == 1):
                 self.conn.close()
             if (self.client != None):
                 self.client.close()
Example #8
0
 def inserttran(self, weibotran, picid, wconn):
     if (isinstance(weibotran, WeiboTransfer)):
         self.inition()  #因为mysql 连接很容易超时就断开了,默认10s,所以索性每次都用的时候在申明
         self.md5.update(
             weibotran.id.encode('utf8') + weibotran.intime.encode('utf8') +
             weibotran.remark.encode('utf8') +
             weibotran.content.encode('utf8'))
         #获取操作游标
         cursor = self.conn.cursor()
         try:
             if (picid == ''):
                 picid = 0
             #插入一条记录 全部用%s作占位符号,即使是数字也不要用%d
             #因为这个插件程序会帮你转换的,字符串会自动给你加'',如果数字不会加
             cursor.execute(
                 """INSERT INTO weibocatch.w_transfer
                             (weiboid,wid,transferid,content,remark,label,url,
                             intime,picid,wfrom)
                             VALUES
                             (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);
                             """,
                 (self.md5.hexdigest(), weibotran.id, weibotran.tranferid,
                  weibotran.content.encode('utf8'),
                  weibotran.remark.encode('utf8'),
                  weibotran.label.encode('utf8'), weibotran.url,
                  weibotran.intime.encode('utf8'), int(picid),
                  weibotran.wfrom.encode('utf8')))
             weibotranjson = self.class_to_dict(weibotran)
             self.initionMo()
             table1 = self.db.weibotran
             table1.insert_one(weibotranjson)
             if (isinstance(wconn, Wconn)):
                 if (wconn.tag == 1 or wconn.tag == 0):  #判断字符串不为空
                     cursor.execute(
                         """INSERT INTO weibocatch.w_conn
                                     (weiboid,tag,atid,atname)
                                     VALUES
                                     (%s,%s,%s,%s);""",
                         (self.md5.hexdigest(), wconn.tag, wconn.atid,
                          wconn.atname))
                     wconnjson = self.class_to_dict(wconn)
                     table2 = self.db.wconn
                     table2.insert_one(wconnjson)
             self.conn.commit()  # 一定不要漏了,不然不会执行的
             print time.strftime(
                 '%H-%M-%S'
             ) + ":" + "%s转发微博存入成功" % weibotran.id.encode('utf8')
         except Exception as err:
             self.conn.rollback()
             print time.strftime('%H-%M-%S') + ":" + str(err)
             if (err[0] != 1062):
                 werror = Werror()
                 werror.id = weibotran.id
                 werror.error = str(err)
                 self.seterror(werror)
         finally:
             #关闭连接,释放资源
             cursor.close()
             if (self.conn.open == 1):
                 self.conn.close()
             if (self.client != None):
                 self.client.close()