本文整理汇总了Python中weibopy.api.API.friends_timeline方法的典型用法代码示例。如果您正苦于以下问题:Python API.friends_timeline方法的具体用法?Python API.friends_timeline怎么用?Python API.friends_timeline使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类weibopy.api.API
的用法示例。
在下文中一共展示了API.friends_timeline方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Test
# 需要导入模块: from weibopy.api import API [as 别名]
# 或者: from weibopy.api.API import friends_timeline [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)
def basicAuth(self, source, username, password):
self.auth = BasicAuthHandler(username, password)
self.api = API(self.auth,source=source)
def friends_timeline(self):
timeline = self.api.friends_timeline(count=2, page=1)
for line in timeline:
self.obj = line
mid = self.getAtt("id")
text = self.getAtt("text")
print("friends_timeline---"+ str(mid) +":"+ text)
def comments_timeline(self):
timeline = self.api.comments_timeline(count=2, page=1)
for line in timeline:
self.obj = line
mid = self.getAtt("id")
text = self.getAtt("text")
print("comments_timeline---"+ str(mid) +":"+ text)
def user_timeline(self):
timeline = self.api.user_timeline(count=5, page=1)
for line in timeline:
self.obj = line
mid = self.getAtt("id")
text = self.getAtt("text")
created_at = self.getAtt("created_at")
print("user_timeline---"+ str(mid) +":"+ str(created_at)+":"+ text)
timeline = self.api.user_timeline(count=20, page=2)
def public_timeline(self):
timeline = self.api.public_timeline(count=2, page=1)
for line in timeline:
self.obj = line
mid = self.getAtt("id")
text = self.getAtt("text")
print("public_timeline---"+ str(mid) +":"+ text)
示例2: crawl_video
# 需要导入模块: from weibopy.api import API [as 别名]
# 或者: from weibopy.api.API import friends_timeline [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)