class Test(unittest.TestCase): consumer_key='' consumer_secret='' def __init__(self): """ constructor """ def getAtt(self, key): try: return self.obj.__getattribute__(key) except Exception as e: print(e) return '' def setAccessToken(self, key, secret): self.auth = OAuthHandler(self.consumer_key, self.consumer_secret) self.auth.setAccessToken(key, secret) self.api = API(self.auth, source=self.consumer_key) def basicAuth(self, source, username, password): self.authType = 'basicauth' self.auth = BasicAuthHandler(username, password) self.api = API(self.auth,source=source) def tags(self): tags = self.api.tags(user_id=1377583044) for line in tags: self.obj = line tagid=self.getAtt("id") value = self.getAtt(tagid) print (tagid,value) def tag_create(self ,message): # message = message.encode("utf-8") tag_create = self.api.tag_create(tags=message) for line in tag_create: self.obj = line tagid = self.getAtt("tagid") print ("tag_create---"+tagid) def tag_suggestions(self): tag_suggestions=self.api.tag_suggestions() for line in tag_suggestions: self.obj = line id = self.getAtt("id") value = self.getAtt("value") print ("tag_suggestions---"+ id +":"+ value) def tag_destroy(self,tag_id): tag_destroy=self.api.tag_destroy(tag_id) self.obj=tag_destroy result=self.getAtt("result") print ("tag_destroy---"+ result) def tag_destroy_batch(self,tag_ids): tag_destroy_batch=self.api.tag_destroy_batch(tag_ids) for line in tag_destroy_batch: self.obj = line tagid=self.getAtt("tagid") print ("tag_destroy_batch---"+ tagid)
def getTag(self,uid): self.auth.setToken(self.key,self.secret) api = API(self.auth) try: tag = api.tags(uid) #print tag string = "" for node in tag: for attr,value in node.__dict__.items(): if attr != '_api' and attr != 'id': #print type(value) string += value.encode('utf-8')+" " return string except Exception ,e: pass
class Setag(threading.Thread): """ 这个线程主要是用来搜索用户tag,如果满足tag要求就写入数据库中, """ def authorization(self): """ 开发者认证 """ auth = OAuthHandler(key.CONSUME_KEY, key.CONSUME_SECRET) auth.setToken(key.TOKEN, key.TOKEN_SECRET) self.api = API(auth) self.cursor = cursor def adduser(self, uid): """ 遍历uid用户的tag,满足条件加入数据库 """ try: fan = self.api.followers_ids(uid) fanuid = fan.ids for id in fanuid: tags = self.api.tags(id) tt = [] for t in tags: tagid = t.__getattribute__('id') value = t.__getattribute__(tagid) tt.append(value.encode('utf-8')) """ 获取用户tag与要求标签的交集 """ common = set(tt).intersection(set(systag)) if len(common) == 0: continue else: for t in common: """ 获取tag对应的tagid """ tagindex = tagdict[t] try: self.cursor.execute( "insert into taginfo(uid,tagid) values(%d,%d)" % (int(id), int(tagindex))) conn.commit() except: continue except: time.sleep(120) pass finally: time.sleep(60) """ 将uid用户的第一个粉丝uid传给adduser """ return self.adduser(fanuid[0]) def run(self): self.authorization() me = self.api.verify_credentials() """ 将我自己的uid给adduser """ self.adduser(me.id)