-2

I am trying to put together a script that will watch twitter for certain keywords, take a picture, and then reply to the initial tweet with the picture. To keep everything straight, I want to use the initial tweet's unique ID as the filename of the picture. I think I am close, but I can't figure out how to make it work. Here is the code:

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'])
4

2 回答 2

1

你的CustomStreamListener类有一个__init__方法,它需要一个status参数,但在该行

sapi = tweepy.streaming.Stream(auth, CustomStreamListener())

您在CustomStreamListener不传递该参数的情况下创建一个实例,因此会引发错误

__init__() takes exactly 2 arguments (1 given)

这意味着__init__得到了self参数,但没有得到另一个参数 ( status)。

要解决这个问题,你必须传递一些东西作为status类实例化的参数!

于 2013-09-25T15:21:11.703 回答
0

关...

subprocess.Popen(['raspistill', '-o', '{0}.jpg'.format(new_tweet.id), '-t', '0'])

使用 string.format() 时,您需要在花括号内提供位置编号。

于 2013-09-25T15:05:28.133 回答