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


Python Stream.new_session方法代码示例

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


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

示例1: on_error

# 需要导入模块: from tweepy import Stream [as 别名]
# 或者: from tweepy.Stream import new_session [as 别名]
        ##when data has been pushed --> write it to disk
        try:
            d = json.loads(data)
            creation = d['created_at']
            id = d['id']
            text = d['text'].encode("UTF-8")
            name = d['user']['name'].encode("UTF_8")
            screen_name = d['user']['screen_name'].encode("UTF-8")
            lang = d['user']['lang']
            #print d['coordinates']
            row = creation, id, name, screen_name, lang, text
            csvWriter.writerow(row)
        except:
            pass
        return True


    def on_error(self, status_code):
        print status_code
        return True

    def on_timeout(self):
        print "Timeout..."
        return True


##start the stream
streamer = Stream(auth, tweetStreamer())
streamer.new_session()
streamer.filter(track=["Syria"])
开发者ID:scottsfarley1993,项目名称:twitterViz,代码行数:32,代码来源:twitterStreamer2.py

示例2: stream

# 需要导入模块: from tweepy import Stream [as 别名]
# 或者: from tweepy.Stream import new_session [as 别名]
def stream(terms):
    terms = ["Syria"]
    stream = Stream(auth, tweetStream())
    stream.new_session()
    stream.filter(track=terms, async=True) ##non-blocking --> kind of a nightmare, but the only way to still process the queue
开发者ID:scottsfarley1993,项目名称:twitterOnDemand,代码行数:7,代码来源:streamToDB.py

示例3: App

# 需要导入模块: from tweepy import Stream [as 别名]
# 或者: from tweepy.Stream import new_session [as 别名]
class App():
    def __init__(self):
        self.stream = None
        self.on = False
        self.streaming = False
        self.trackTerm = ""
        self.initTime = None
        self.timeSinceInit = 0
        self.totalCollectedTweets = 0
        self.numOutput = 0
        self.numInQueue = 0
        self.sessionQueue = None
        self.analytics = analytics()

    @cherrypy.expose
    def start(self):
        try:
            if self.on == False:
                self.sessionQueue = Queue.Queue()
                self.stream = Stream(auth, tweetStream(self.sessionQueue))
                self.stream.new_session()
                self.on = True
                self.initTime = datetime.datetime.now()
                currentTime = datetime.datetime.now()
                self.timeSinceInit = currentTime - self.initTime
                # user sessions
                if 'count' not in cherrypy.session:
                    cherrypy.session['count'] = 0
                cherrypy.session['count'] += 1
                return json.dumps({"success": True, 'data': None, "message": "New stream session initialized."})
            else:
                return json.dumps({"Success" : True, 'data':None, "message" : "Stream already started."})

        except Exception as e:
            self.on = False
            return json.dumps({"success": True, 'data': None, "message": "New stream session not started because " + str(e)})

    @cherrypy.expose
    def close(self):
        try:
            if self.on:
                self.stream.disconnect()
                self.on = False
                self.streaming = False
                self.sessionQueue = None
                self.analytics = analytics()
                return json.dumps({"success":True, 'data':None, "message":'Stream session terminated.'})
            else:
                return json.dumps({"success" : True, 'data':None, "message" : "Streaming already closed."})
        except Exception as e:
            return json.dumps({"success":False, 'data':None, "message":'Stream session not terminated because ' + str(e)})

    @cherrypy.expose
    def invisibleStream(self, toTrack, *args, **kwargs):
        try:
            if not self.streaming:
                self.stream.filter(track=[toTrack], async=True)
                self.streaming = True
                self.trackTerm = toTrack
                currentTime = datetime.datetime.now()
                self.timeSinceInit = currentTime - self.initTime
                return json.dumps({'success' :True, "data":None, "Message":"Stream is streaming."})
        except Exception as e:
            return json.dumps({"success" : False, "data":None, "message" : "Track stream failed because " + str(e)})

    @cherrypy.expose
    def trueStream(self, *args, **kwards):
        ##streams the connection as continuous json
        if not self.streaming:
            return json.dumps({"success" : False, "data":None, "message" : "Track stream failed because the application is not streaming."})
        try:
            def getTweetsFromQueue():
                t = 0
                while True:
                    t += 1
                    yield str(self.sessionQueue.get())
                    self.numOutput += 1
                self.stream.disconnect()
            return getTweetsFromQueue()
        except Exception as e:
            return json.dumps({"success" : False, "data":None, "message" : "Track stream failed because " + str(e)})

    @cherrypy.expose
    def pollStream(self):
        ## publishes all items currently in the queue as a json object
        # if not self.streaming:
        #     self.invisibleStart(toTrack) ##start streaming if not already
        conn = psycopg2.connect(connectString)
        cursor = conn.cursor()
        outArr = []
        allPlaces = []
        coords = []
        q = 0
        queueCopy = self.sessionQueue
        # for i in range(0, 100):
        #     queueCopy.put(i)
        self.totalCollectedTweets = self.numInQueue + self.numOutput
        i = 0
        while i < 6 and queueCopy.qsize() > 0:
            a =  queueCopy.get()
#.........这里部分代码省略.........
开发者ID:scottsfarley1993,项目名称:twitterOnDemand,代码行数:103,代码来源:central.py

示例4: streamFunction

# 需要导入模块: from tweepy import Stream [as 别名]
# 或者: from tweepy.Stream import new_session [as 别名]
def streamFunction(track):
    stream = Stream(auth, tweetStream())
    stream.new_session()
    stream.filter(track=[track])
    print "Running thread."
开发者ID:scottsfarley1993,项目名称:twitterOnDemand,代码行数:7,代码来源:multithreadedStream.py


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