2013-08-05 20 views
-1

作爲一個研究項目的一部分,我已經消耗從Twitter微博爲本地託管的MongoDB數據庫的腳本:如何將我的腳本變成遠程託管的應用程序?

import json 
import pymongo 
import tweepy 

consumer_key = "" 
consumer_secret = "" 
access_key = "" 
access_secret = "" 

auth = tweepy.OAuthHandler(consumer_key, consumer_secret) 
auth.set_access_token(access_key, access_secret) 
api = tweepy.API(auth) 


class CustomStreamListener(tweepy.StreamListener): 
    def __init__(self, api): 
     self.api = api 
     super(tweepy.StreamListener, self).__init__() 

     self.db = pymongo.MongoClient().test 

    def on_data(self, tweet): 
     self.db.tweets.insert(json.loads(tweet)) 

    def on_error(self, status_code): 
     return True # Don't kill the stream 

    def on_timeout(self): 
     return True # Don't kill the stream 


sapi = tweepy.streaming.Stream(auth, CustomStreamListener(api)) 
sapi.filter(track=['snowden']) 

爲了提高正常運行時間,我希望做兩件事情:1)遠程運行此腳本,以及ii)將消費的推文存儲在雲中。然而,對於編程的所有事物來說,這是一個全新的概念,我失去了爲了實現自己的目標應該做些什麼。我的下一步是什麼?正常運行時間的「最小阻力路徑」是什麼?

回答

2

Heroku是一個支持Python和MongoDB的雲平臺,我建議您使用它。 This link提供了有關如何執行此操作的工作參考。

這裏有另一對夫婦的鏈接來幫助你:

1)Python database WITHOUT using Django (for Heroku)

2)How can I use the mongolab add-on to Heroku from python?

希望這有助於! OK。

+0

OK。我是否需要爲我的腳本創建一個Github倉庫,然後將其部署到Heroku?到目前爲止,我一直在我的(mac)計算機上從終端直接運行我的腳本。 – user2161725

+0

不需要... Heroku內置了Git支持。查看此入門教程:https://devcenter.heroku.com/articles/python – 2013-08-05 17:21:21

+0

感謝您訪問Heroku入門教程的鏈接。按照教程中的步驟,我已經成功設置了一個乾淨的燒瓶環境,編寫了一個helloworld.py應用程序並將其部署到Heroku。到現在爲止還挺好。但是,我不知道如何修改我的源代碼(請參閱原始問題),以便它可以作爲Flask應用程序運行。有什麼想法嗎? – user2161725