当前位置: 首页>>代码示例>>Python>>正文


Python API.comments方法代码示例

本文整理汇总了Python中weibopy.api.API.comments方法的典型用法代码示例。如果您正苦于以下问题:Python API.comments方法的具体用法?Python API.comments怎么用?Python API.comments使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在weibopy.api.API的用法示例。


在下文中一共展示了API.comments方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Test

# 需要导入模块: from weibopy.api import API [as 别名]
# 或者: from weibopy.api.API import comments [as 别名]
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 comments(self, id):
        comments = self.api.comments(id=id)
        for comment in comments:
            self.obj = comment
            mid = self.getAtt("id")
            text = self.getAtt("text")
            print("comments---"+ str(mid) +":"+ text)
    
    def comments_timeline(self):
        comments = self.api.comments_timeline()
        for comment in comments:
            self.obj = comment
            mid = self.getAtt("id")
            text = self.getAtt("text")
            print("comments_timeline---"+ str(mid) +":"+ text)
    
    def comments_by_me(self):
        comments = self.api.comments_by_me(count=5)
        mid = ''
        for comment in comments:
            self.obj = comment
            mid = self.getAtt("id")
            text = self.getAtt("text")
            created_at = self.getAtt("created_at")
            print('comments_by_me,id='+ str(mid) +',text='+ text+',created_at='+ str(created_at))
        return mid
    
    def comment(self, mid):
        comment = self.api.comment(id=mid, comment='commect-test-²âÊÔ--'+ str(time.time()))
        self.obj = comment
        mid = self.getAtt("id")
        text = self.getAtt("text")
        print("comment---"+ str(mid) +":"+ text)
    
    def comment_destroy (self, mid):
        comment = self.api.comment_destroy(mid)
        self.obj = comment
        mid = self.getAtt("id")
        text = self.getAtt("text")
        print("comment_destroy---"+ str(mid) +":"+ text)
开发者ID:chengjun,项目名称:Research,代码行数:67,代码来源:comment.py

示例2: crawl_video

# 需要导入模块: from weibopy.api import API [as 别名]
# 或者: from weibopy.api.API import comments [as 别名]
 def crawl_video(self):
     try:
         sina_key = KeyValue.objects.get(key='CRAWL_SINA_KEY').value
         sina_secret = KeyValue.objects.get(key='CRAWL_SINA_SECRET').value
         since_id = KeyValue.objects.get(key='CRAWL_SINA_SINCE_ID').value
     except:
         print('Error in getting necessary key-value\n')
         return 
     
     #Construct video source from database
     vss = Video_Source.objects.all()
     vs_dict = {}
     for vs in vss:
         sa = vs.sina_account
         if sa in vs_dict:
             vs_dict[sa] =  vs_dict[sa].append(vs.channel_id)
         else:
             vs_dict[sa] = [vs.channel_id]
     
     #print(str(vs_dict))
     
     auth = OAuthHandler(settings.SINA_CONSUMER_KEY, settings.SINA_CONSUMER_SECRET)
     auth.setToken(sina_key, sina_secret)
     api = API(auth)
     count_per_page = 200
     page = 1
     # Feature: 0:All 1,Original 2:Picture 3:Video 4:Music
     if since_id:
         timeline = api.friends_timeline(count=count_per_page,  since_id=since_id, page=page, feature=3)
     else:
         timeline = api.friends_timeline(count=count_per_page, page=page, feature=3)
     is_first_obj = True
     while timeline: #while the list user_timeline is not []
         for obj in timeline:
             try:
                 status_id = self.getAtt(obj, 'id')
                 if is_first_obj:
                     since_id_obj = KeyValue.objects.get(key='CRAWL_SINA_SINCE_ID')
                     since_id_obj.value = status_id
                     since_id_obj.save()
                     is_first_obj = False
                 text = self.getAtt(obj, 'text')
                 friend = self.getAtt(obj, 'user')
                 friend_id = self.getAtt(friend, 'id') #64 int
                 #screen_name = self.getAtt(friend, 'screen_name')  
                 snippet = ''
                 retweet = self.getAtt(obj, 'retweeted_status')
                 if retweet: #Discard the original text
                     snippet = text
                     text = self.getAtt(retweet, 'text')
                 date = self.getAtt(obj, "created_at") 
                 comments = api.comments(id=status_id)
                 comments_list = []
                 for comment in comments:
                     comments_list.append(self.getAtt(comment, 'text'))
                 snippet = ' '.join(comments_list)
                 urls = re.findall('http://t.cn/[a-zA-Z0-9]{5,8}', text)
                 
                 #process text before using it as title of item ,strip @ and urls
                 url_index = text.find('http://')
                 text = text[:url_index]
                 text = text.replace(u'【', ' ')
                 text = text.replace(u'】', ' ')
                 
                 for url in urls: #Typically there should only be one
                     parsed = urlparse.urlparse(url)
                     h = httplib.HTTPConnection(parsed.netloc)
                     h.request('HEAD', parsed.path)
                     response = h.getresponse()
                     if response.status / 100 == 3 and response.getheader('Location'): #continue checking redirect
                         url = response.getheader('Location')
                     if not is_video(url):
                         continue
                     msg_or_id = add_item(url, pre_name=text)
                     if isinstance(msg_or_id, int) or isinstance(msg_or_id, long):
                         item = Item.objects.get(pk=msg_or_id)
                         if date:
                             item.create_date = date
                         item.snippet = snippet
                         if friend_id in vs_dict:
                             item.channels = ','.join([str(x) for x in vs_dict[friend_id]])                           
                         item.save()
             except Exception, e: #Tolerate error in importing one weibo
                 continue 
         page += 1
         print 'page:' + str(page)
         if page == 4:
             break
         if since_id:
             timeline = api.friends_timeline(count=count_per_page,  since_id=since_id, page=page)
         else:
             timeline = api.friends_timeline(count=count_per_page, page=page)        
开发者ID:alexdiao,项目名称:3805,代码行数:94,代码来源:crawl_video.py


注:本文中的weibopy.api.API.comments方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。