您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何在使用Twitter Stream给出的流示例上暂停,终止,停止或关闭PycURL请求

5b51 2022/1/14 8:21:19 python 字数 4079 阅读 466 来源 www.jb51.cc/python

我目前正在使用twitter API流(http://stream.twitter.com/1/statuses/sample.json),因此我不断收到数据.我希望一旦我从中检索到X个对象就停止cURLing流(在示例中,我将10作为任意数字).您可以在下面的代码中查看我是如何尝试关闭连接的. curling.perform()下面的代码永远不会执行,因为

概述

我目前正在使用twitter API流(http://stream.twitter.com/1/statuses/sample.json),因此我不断收到数据.我希望一旦我从中检索到X个对象就停止cURLing流(在示例中,我将10作为任意数字).

您可以在下面的代码中查看我是如何尝试关闭连接的. curling.perform()下面的代码永远不会执行,因为它是一个连续的数据流.所以我试图在body_callback中关闭流,但是因为perform()当前正在运行,所以我无法调用close().

任何帮助,将不胜感激.

码:

# Imports
import pycurl # Used for doing cURL request
import base64 # Used to encode username and API Key
import json # Used to break down the json objects

# Settings to access stream and API
userName = 'twitter_username' # My username
password = 'twitter_password' # My API Key
apiURL = 'http://stream.twitter.com/1/statuses/sample.json' # the twitter api
tweets = [] # An array of Tweets

# Methods to do with the tweets array
def how_many_tweets():
    print 'Collected: ',len(tweets)
    return len(tweets)

class Tweet:
    def __init__(self):
        self.raw = ''
        self.id = ''
        self.content = ''

    def decode_json(self):
        return True

    def set_id(self):
        return True

    def set_content(self):
        return True

    def set_raw(self,data):
        self.raw = data

# Class to print out the stream as it comes from the API
class Stream:
    def __init__(self):
        self.tweetBeingRead =''

    def body_callback(self,buf):
        # This gets whole Tweets,and adds them to an array called tweets
        if(buf.startswith('{"in_reply_to_status_id_str"')): # This is the start of a tweet
            # Added Tweet to Global Array Tweets
            print 'Added:' # Priniting output to console
            print self.tweetBeingRead # Printing output to console
            theTweetBeingProcessed = Tweet() # Create a new Tweet Object
            theTweetBeingProcessed.set_raw(self.tweetBeingRead) # Set its raw value to tweetBeingRead
            tweets.append(theTweetBeingProcessed) # Add it to the global array of tweets
            # Start processing a new tweet
            self.tweet = buf # Start a new tweet from scratch
        else:
            self.tweetBeingRead = self.tweetBeingRead+buf
        if(how_many_tweets()>10):
            try:
                curling.close() # This is where the problem lays. I want to close the stream
            except Exception as CurlError:
                print ' Tried closing stream: ',CurlError

# Used to initiate the cURLing of the Data Sift streams
datastream = Stream()
curling = pycurl.Curl()
curling.setopt(curling.URL,apiURL)
curling.setopt(curling.HTTPHEADER,['Authorization: '+base64.b64encode(userName+":"+password)])
curling.setopt(curling.WRITEFUNCTION,datastream.body_callback)
curling.perform() # This is cURLing starts
print 'I cant reach here.'
curling.close() # This never gets called. :(

当您中止它时,整个传输将被视为已完成并且您的perform()调用将正确返回.

转移将在转移中止时返回错误.

总结

以上是编程之家为你收集整理的如何在使用Twitter Stream给出的流示例上暂停,终止,停止或关闭PycURL请求全部内容,希望文章能够帮你解决如何在使用Twitter Stream给出的流示例上暂停,终止,停止或关闭PycURL请求所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶