2013-09-25 31 views
-2

我正在尝试放置一个脚本,该脚本会查看某些关键字的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']) 
+0

它在做什么呢?是否有例外?它是否创建了一个不同名称的文件?无名? –

+0

我得到这个错误,我不知道如何通过CustomStreamListener()另一个参数。回溯(最近通话最后一个): 文件 “/home/pi/twitterwatcherwtimertakepicwrightname.py” 63行,在<模块> SAPI = tweepy.streaming.Stream(AUTH,CustomStreamListener()) 类型错误:__init __()接受恰好有2个参数(1个给出) – user2815851

+0

如果您在常见问题解答中错过了它,当提问*总是*包括您预期发生的事情,以及发生了什么事情。我们中的大多数人不是很精通;) –

回答

1

CustomStreamListener类有一个__init__方法,它需要一个status说法,但在该行

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

你化妆的CustomStreamListener不经过这样的说法,因此引发错误

__init__() takes exactly 2 arguments (1 given) 

实例这意味着__init__得到了self的说法,但不是另一个(status)。

要解决这个问题,你必须传递一些信息作为status关于类的instatiation的参数!

+0

它看起来像我可能已经把自己置于我的头上......我怎么能通过状态参数,以便它可以按需要工作? – user2815851

+0

因为我没有使用'tweepy'我不能给你一个很好的答案,但是可能你可以删除整个'__init__'函数,因为你从不在类中使用'self.id'。或者如果你有东西要通过,例如''''你可以这样做:'sapi = tweepy.streaming.Stream(auth,CustomStreamListener(s))' – TobiMarg

0

关闭...

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

使用的String.Format()时,您需要在大括号内提供位置数。

+0

其实,不,你没有。 '>>>'{} {}'。format(3,4)'是完全有效的语法。位置或关键字参数的唯一必要性是用于复制或改变显示顺序,例如, ''{0} {1} {0}'。格式('x',23)' –

+0

我很困惑。我应该说,我使用Python 2.6。当我这样做时(>>>> {} {}'。格式(3,4)' 然后它会抛出 '追踪(最近呼叫最后): 文件“”,第1行,在 '{} {}'。format(3,4) ValueError:格式为零的字段名称我在文档中看到,从2.6开始,它添加了str.format(),甚至在3中。 *他们仍然在说以下行:'每个替换字段包含位置参数的数字索引或关键字参数的名称。“http://docs.python.org/2.6/library/stdtypes.html# str.format –

+0

这听起来像是它为原始用户(和你)工作,所以我假设它是我的用户错误。我哪里做错了? –

相关问题