0
Tweepy을 사용하는 다음 Python 스크립트에서 PyMongo을 사용하여 트윗을 가져 와서 MongoDB에 저장합니다. 이 스크립트를 실행할 때마다 MongoDB 인스턴스가 종료되고 스크립트가 오류로 중지됩니다. 스크립트가 종료 중이거나 Tweepy에서 오류가 발생하더라도 MongoDB의 실행 상태에 영향을 주어서는 안되기 때문에 왜 이런 일이 일어나는지 알 수 없습니다. 권한에 대한 질문에 stream.filter(locations=[-119.970703, 48.994636, -109.951172, 59.955010])
MongoDB 서비스 종료에 영향을 미치는 Tweepy 스크립트
스크립트
import tweepy, sys, json, traceback
from bson.json_util import loads as json_to_bson
from hashlib import sha1
from datetime import datetime
from pymongo import MongoClient
from time import sleep, strptime, mktime
client = MongoClient()
mode = None
class Stream(tweepy.StreamListener):
def on_status(self, data):
save(data)
def on_error(self, code):
pause()
def now():
return str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
def pause():
sys.stdout.flush()
sleep((60*15)+5)
def save(data):
bson = json_to_bson(json.dumps(data._json))
tweet_date = strptime(bson['created_at'], "%a %b %d %H:%M:%S +0000 %Y")
tweet_date = str(datetime.fromtimestamp(mktime(tweet_date)))
bson['created_at'] = tweet_date
bson['text_hash'] = sha1(bson['text'].encode('punycode')).hexdigest()
bson['collected_at'] = now()
bson['collection_type'] = mode
if client.grebe.tweets.find_one({'text_hash': bson['text_hash']}) == None:
client.grebe.tweets.insert_one(bson)
def api():
CONSUMER_KEY = 'key'
CONSUMER_SECRET = 'secret'
ACCESS_TOKEN_KEY = 'tokenkey'
ACCESS_TOKEN_SECRET = 'tokensecret'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET)
return tweepy.API(auth)
def main():
mystream = Stream()
stream = tweepy.Stream(api().auth, mystream)
stream.filter(locations=[-119.970703, 48.994636, -109.951172, 59.955010])
main()
오류
File "/usr/local/lib/python2.7/dist-packages/tweepy/streaming.py", line 445, in filter
self._start(async)
File "/usr/local/lib/python2.7/dist-packages/tweepy/streaming.py", line 361, in _start
self._run()
File "/usr/local/lib/python2.7/dist-packages/tweepy/streaming.py", line 294, in _run
raise exception
AutoReconnect: connection closed