我正在嘗試放置一個腳本,該腳本會查看某些關鍵字的Twitter,拍攝一張照片,然後回覆圖片的初始推文。爲了保持一切正常,我想使用初始推文的唯一ID作爲圖片的文件名。我認爲我很接近,但我無法弄清楚如何使它工作。下面是代碼:使用推文ID作爲文件名
import sys
import tweepy
import time
import threading
import subprocess
consumer_key="X"
consumer_secret="X"
access_key = "X"
access_secret = "X"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
class Timer(threading.Thread):
def __init__(self, seconds):
self.runTime = seconds
threading.Thread.__init__(self)
def run(self):
time.sleep(self.runTime)
class CountDownTimer(Timer):
def run(self):
counter = self.runTime
for sec in range(self.runTime):
print counter
time.sleep(1.0)
counter -= 1
class CountDownExec(CountDownTimer):
def __init__(self, seconds, action):
self.action = action
CountDownTimer.__init__(self, seconds)
def run(self):
CountDownTimer.run(self)
self.action()
def takePicture():
new_tweet = CustomStreamListener(status_info)
subprocess.Popen(['raspistill', '-o', '{}.jpg'.format(new_tweet.id), '-t', '0'])
c = CountDownExec(5, takePicture)
class CustomStreamListener(tweepy.StreamListener):
def __init__(self,status):
self.id = status.id
def on_status(self, status):
print status.user.id
print status.user.screen_name
print status.id
print status.text
c.start()
def on_error(self, status_code):
print >> sys.stderr, 'Encountered error with status code:', status_code
return True # Don't kill the stream
def on_timeout(self):
print >> sys.stderr, 'Timeout...'
return True # Don't kill the stream
sapi = tweepy.streaming.Stream(auth, CustomStreamListener())
sapi.filter(track=['#hashtag @username'])
它在做什麼呢?是否有例外?它是否創建了一個不同名稱的文件?無名? –
我得到這個錯誤,我不知道如何通過CustomStreamListener()另一個參數。回溯(最近通話最後一個): 文件 「/home/pi/twitterwatcherwtimertakepicwrightname.py」 63行,在<模塊> SAPI = tweepy.streaming.Stream(AUTH,CustomStreamListener()) 類型錯誤:__init __()接受恰好有2個參數(1個給出) – user2815851
如果您在常見問題解答中錯過了它,當提問*總是*包括您預期發生的事情,以及發生了什麼事情。我們中的大多數人不是很精通;) –